#!/bin/bash # 管理定时任务 → JSON 输出 read -r input ACTION=$(echo "$input" | python3 -c "import sys,json; print(json.load(sys.stdin).get('action',''))" 2>/dev/null) NAME=$(echo "$input" | python3 -c "import sys,json; print(json.load(sys.stdin).get('name',''))" 2>/dev/null) CMD=$(echo "$input" | python3 -c "import sys,json; print(json.load(sys.stdin).get('command',''))" 2>/dev/null) INTERVAL=$(echo "$input" | python3 -c "import sys,json; print(json.load(sys.stdin).get('interval_seconds',3600))" 2>/dev/null) DESC=$(echo "$input" | python3 -c "import sys,json; print(json.load(sys.stdin).get('description',''))" 2>/dev/null) DB_URL="${DATABASE_URL}" [ -z "$DB_URL" ] && echo '{"error":"数据库未配置"}' && exit 0 [ -z "$NAME" ] && echo '{"error":"请提供 name 参数"}' && exit 0 case "$ACTION" in add) [ -z "$CMD" ] && echo '{"error":"add 需要 command 参数"}' && exit 0 psql "$DB_URL" -c " INSERT INTO scheduled_tasks (id, name, description, command, interval_seconds, enabled, next_run_at) VALUES (gen_random_uuid(), '$NAME', '$DESC', '$CMD', $INTERVAL, true, NOW() + interval '$INTERVAL seconds') ON CONFLICT (name) DO UPDATE SET command=EXCLUDED.command, interval_seconds=EXCLUDED.interval_seconds, enabled=true, description=EXCLUDED.description " 2>/dev/null && echo '{"ok":true,"action":"add","name":"'"$NAME"'"}' || echo '{"error":"添加失败"}' ;; delete) psql "$DB_URL" -c "DELETE FROM scheduled_tasks WHERE name = '$NAME'" 2>/dev/null echo '{"ok":true,"action":"delete","name":"'"$NAME"'"}' ;; enable) psql "$DB_URL" -c "UPDATE scheduled_tasks SET enabled = true WHERE name = '$NAME'" 2>/dev/null echo '{"ok":true,"action":"enable","name":"'"$NAME"'"}' ;; disable) psql "$DB_URL" -c "UPDATE scheduled_tasks SET enabled = false WHERE name = '$NAME'" 2>/dev/null echo '{"ok":true,"action":"disable","name":"'"$NAME"'"}' ;; *) echo '{"error":"未知操作: '"$ACTION"'(支持: add, delete, enable, disable)"}' exit 1 ;; esac