hook_runner.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/bin/bash
  2. # workflow_engine/hook_runner.sh
  3. # 文件事件 Hook - 监听状态文件变更并触发调度
  4. #
  5. # 依赖: inotify-tools (apt install inotify-tools)
  6. # 用法: ./hook_runner.sh
  7. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  8. STATE_FILE="$SCRIPT_DIR/state/current_step.json"
  9. RUNNER="$SCRIPT_DIR/runner.py"
  10. echo "[HOOK] 启动监听: $STATE_FILE"
  11. echo "[HOOK] 按 Ctrl+C 停止"
  12. # 检查依赖
  13. if ! command -v inotifywait &> /dev/null; then
  14. echo "[ERROR] 需要安装 inotify-tools: sudo apt install inotify-tools"
  15. exit 1
  16. fi
  17. # 确保状态文件存在
  18. mkdir -p "$(dirname "$STATE_FILE")"
  19. [ -f "$STATE_FILE" ] || echo '{"status":"idle"}' > "$STATE_FILE"
  20. # 监听文件修改事件
  21. inotifywait -m -e modify "$STATE_FILE" 2>/dev/null | while read -r directory event filename; do
  22. echo "[HOOK] $(date '+%H:%M:%S') 检测到状态变更"
  23. # 读取 target_step
  24. target=$(python3 -c "import json; print(json.load(open('$STATE_FILE')).get('target_step',''))" 2>/dev/null)
  25. if [ "$target" = "done" ]; then
  26. echo "[HOOK] 工作流已完成"
  27. elif [ -n "$target" ] && [ "$target" != "null" ]; then
  28. echo "[HOOK] 触发调度 -> $target"
  29. python3 "$RUNNER" dispatch
  30. fi
  31. done