259 lines
6.5 KiB
Bash
259 lines
6.5 KiB
Bash
|
|
#!/bin/bash
|
|||
|
|
# inkos质量监控和服务脚本
|
|||
|
|
# 在inkos启动前检查配置,启动后监控输出质量
|
|||
|
|
|
|||
|
|
set -e
|
|||
|
|
|
|||
|
|
INKOS_DIR="/root/.openclaw/workspace/tomato-novel"
|
|||
|
|
BOOKS_DIR="$INKOS_DIR/books"
|
|||
|
|
CONFIG_FILE="$INKOS_DIR/inkos_format_config.json"
|
|||
|
|
MONITOR_SCRIPT="$INKOS_DIR/scripts/format_check_fix.py"
|
|||
|
|
LOG_DIR="$INKOS_DIR/logs"
|
|||
|
|
|
|||
|
|
# 创建日志目录
|
|||
|
|
mkdir -p "$LOG_DIR"
|
|||
|
|
|
|||
|
|
# 颜色定义
|
|||
|
|
RED='\033[0;31m'
|
|||
|
|
GREEN='\033[0;32m'
|
|||
|
|
YELLOW='033[1;33m'
|
|||
|
|
BLUE='\033[0;34m'
|
|||
|
|
NC='\033[0m' # No Color
|
|||
|
|
|
|||
|
|
print_info() {
|
|||
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
print_success() {
|
|||
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
print_warning() {
|
|||
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
print_error() {
|
|||
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 检查inkos是否已安装
|
|||
|
|
check_inkos_installation() {
|
|||
|
|
if command -v inkos &> /dev/null; then
|
|||
|
|
print_success "inkos已安装"
|
|||
|
|
inkos --version
|
|||
|
|
return 0
|
|||
|
|
else
|
|||
|
|
print_error "inkos未安装"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 检查配置
|
|||
|
|
check_configuration() {
|
|||
|
|
print_info "检查配置..."
|
|||
|
|
|
|||
|
|
if [ -f "$CONFIG_FILE" ]; then
|
|||
|
|
print_success "格式配置文件存在: $CONFIG_FILE"
|
|||
|
|
|
|||
|
|
# 验证配置文件格式
|
|||
|
|
if python3 -m json.tool "$CONFIG_FILE" > /dev/null 2>&1; then
|
|||
|
|
print_success "配置文件格式正确"
|
|||
|
|
else
|
|||
|
|
print_error "配置文件格式错误"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
else
|
|||
|
|
print_error "配置文件不存在: $CONFIG_FILE"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
if [ -f "$MONITOR_SCRIPT" ]; then
|
|||
|
|
print_success "监控脚本存在: $MONITOR_SCRIPT"
|
|||
|
|
else
|
|||
|
|
print_error "监控脚本不存在: $MONITOR_SCRIPT"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 检查项目目录
|
|||
|
|
check_project_directories() {
|
|||
|
|
print_info "检查项目目录..."
|
|||
|
|
|
|||
|
|
if [ -d "$INKOS_DIR" ]; then
|
|||
|
|
print_success "inkos项目目录存在: $INKOS_DIR"
|
|||
|
|
else
|
|||
|
|
print_error "inkos项目目录不存在: $INKOS_DIR"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
if [ -d "$BOOKS_DIR" ]; then
|
|||
|
|
print_success "书籍目录存在: $BOOKS_DIR"
|
|||
|
|
else
|
|||
|
|
print_error "书籍目录不存在: $BOOKS_DIR"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 修复现有章节
|
|||
|
|
fix_existing_chapters() {
|
|||
|
|
print_info "修复现有章节格式..."
|
|||
|
|
|
|||
|
|
# 为每个书籍目录执行修复
|
|||
|
|
for book_dir in "$BOOKS_DIR"/*; do
|
|||
|
|
if [ -d "$book_dir" ] && [ -d "$book_dir/chapters" ]; then
|
|||
|
|
book_name=$(basename "$book_dir")
|
|||
|
|
print_info "修复书籍: $book_name"
|
|||
|
|
|
|||
|
|
# 检查当前书籍是否有第2-14章
|
|||
|
|
chapters_dir="$book_dir/chapters"
|
|||
|
|
if [ -d "$chapters_dir" ]; then
|
|||
|
|
python3 "$MONITOR_SCRIPT" fix --directory "$chapters_dir" 2>&1 | tee -a "$LOG_DIR/fix_${book_name}_$(date +%Y%m%d).log"
|
|||
|
|
fi
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
|
|||
|
|
print_success "现有章节修复完成"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 启动质量监控
|
|||
|
|
start_quality_monitor() {
|
|||
|
|
print_info "启动质量监控服务..."
|
|||
|
|
|
|||
|
|
# 启动监控进程
|
|||
|
|
python3 "$MONITOR_SCRIPT" monitor --directory "$BOOKS_DIR" > "$LOG_DIR/monitor_$(date +%Y%m%d_%H%M%S).log" 2>&1 &
|
|||
|
|
MONITOR_PID=$!
|
|||
|
|
|
|||
|
|
echo $MONITOR_PID > "$LOG_DIR/quality_monitor.pid"
|
|||
|
|
print_success "质量监控服务已启动 (PID: $MONITOR_PID)"
|
|||
|
|
|
|||
|
|
# 输出监控信息
|
|||
|
|
print_info "监控服务日志: $LOG_DIR/monitor_*.log"
|
|||
|
|
print_info "监控PID文件: $LOG_DIR/quality_monitor.pid"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 启动inkos
|
|||
|
|
start_inkos() {
|
|||
|
|
print_info "启动inkos..."
|
|||
|
|
|
|||
|
|
# 切换到项目目录
|
|||
|
|
cd "$INKOS_DIR"
|
|||
|
|
|
|||
|
|
# 启动inkos
|
|||
|
|
print_info "正在启动inkos守护进程..."
|
|||
|
|
inkos up 2>&1 | tee -a "$LOG_DIR/inkos_startup_$(date +%Y%m%d_%H%M%S).log" &
|
|||
|
|
INKOS_PID=$!
|
|||
|
|
|
|||
|
|
echo $INKOS_PID > "$LOG_DIR/inkos.pid"
|
|||
|
|
print_success "inkos守护进程已启动 (PID: $INKOS_PID)"
|
|||
|
|
|
|||
|
|
# 等待inkos启动完成
|
|||
|
|
sleep 10
|
|||
|
|
|
|||
|
|
# 检查inkos状态
|
|||
|
|
inkos status
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 显示状态
|
|||
|
|
show_status() {
|
|||
|
|
print_info "系统状态:"
|
|||
|
|
|
|||
|
|
# 检查监控服务
|
|||
|
|
if [ -f "$LOG_DIR/quality_monitor.pid" ] && ps -p $(cat "$LOG_DIR/quality_monitor.pid") > /dev/null 2>&1; then
|
|||
|
|
print_success "质量监控服务运行中 (PID: $(cat $LOG_DIR/quality_monitor.pid))"
|
|||
|
|
else
|
|||
|
|
print_warning "质量监控服务未运行"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
# 检查inkos进程
|
|||
|
|
if [ -f "$LOG_DIR/inkos.pid" ] && ps -p $(cat "$LOG_DIR/inkos.pid") > /dev/null 2>&1; then
|
|||
|
|
print_success "inkos守护进程运行中 (PID: $(cat $LOG_DIR/inkos.pid))"
|
|||
|
|
inkos status
|
|||
|
|
else
|
|||
|
|
print_warning "inkos守护进程未运行"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
# 显示最近日志
|
|||
|
|
print_info "最近日志文件:"
|
|||
|
|
ls -la "$LOG_DIR"/*.log | head -5
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 停止服务
|
|||
|
|
stop_services() {
|
|||
|
|
print_info "停止服务..."
|
|||
|
|
|
|||
|
|
# 停止inkos
|
|||
|
|
if [ -f "$LOG_DIR/inkos.pid" ]; then
|
|||
|
|
pid=$(cat "$LOG_DIR/inkos.pid")
|
|||
|
|
if ps -p "$pid" > /dev/null 2>&1; then
|
|||
|
|
kill "$pid"
|
|||
|
|
print_success "inkos守护进程已停止(PID:$pid)"
|
|||
|
|
fi
|
|||
|
|
rm -f "$LOG_DIR/inkos.pid"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
# 停止质量监控
|
|||
|
|
if [ -f "$LOG_DIR/quality_monitor.pid" ]; then
|
|||
|
|
pid=$(cat "$LOG_DIR/quality_monitor.pid")
|
|||
|
|
if ps -p "$pid" > /dev/null 2>&1; then
|
|||
|
|
kill "$pid"
|
|||
|
|
print_success "质量监控服务已停止(PID:$pid)"
|
|||
|
|
fi
|
|||
|
|
rm -f "$LOG_DIR/quality_monitor.pid"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
print_success "所有服务已停止"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 主函数
|
|||
|
|
main() {
|
|||
|
|
print_info "inkos质量监控系统启动..."
|
|||
|
|
|
|||
|
|
echo "=============================================="
|
|||
|
|
|
|||
|
|
case "${1:-start}" in
|
|||
|
|
start)
|
|||
|
|
check_inkos_installation
|
|||
|
|
check_configuration
|
|||
|
|
check_project_directories
|
|||
|
|
fix_existing_chapters
|
|||
|
|
start_quality_monitor
|
|||
|
|
start_inkos
|
|||
|
|
print_success"系统启动完成"
|
|||
|
|
show_status
|
|||
|
|
;;
|
|||
|
|
|
|||
|
|
stop)
|
|||
|
|
stop_services
|
|||
|
|
;;
|
|||
|
|
|
|||
|
|
restart)
|
|||
|
|
stop_services
|
|||
|
|
sleep 2
|
|||
|
|
check_inkos_installation
|
|||
|
|
check_configuration
|
|||
|
|
check_project_directories
|
|||
|
|
start_quality_monitor
|
|||
|
|
start_inkos
|
|||
|
|
print_success"系统重启完成"
|
|||
|
|
show_status
|
|||
|
|
;;
|
|||
|
|
|
|||
|
|
status)
|
|||
|
|
show_status
|
|||
|
|
;;
|
|||
|
|
|
|||
|
|
fix)
|
|||
|
|
fix_existing_chapters
|
|||
|
|
;;
|
|||
|
|
|
|||
|
|
*)
|
|||
|
|
echo "用法:$0 {start|stop|restart|status|fix}"
|
|||
|
|
exit 1
|
|||
|
|
;;
|
|||
|
|
esac
|
|||
|
|
|
|||
|
|
echo"=============================================="
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 运行主函数
|
|||
|
|
main"$@"
|