feat: 多语言任务、WebUI 增强与 Agent MCP 集成

重构 lib 为扁平模块并支持 Windows schtasks;新增 JS/Bash/PowerShell 模板、WebUI 调度编辑,以及 Cursor Skill 与 MCP 工具供 Agent 管理定时任务。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-24 21:45:25 +08:00
parent 6c2db2dfa3
commit c10cacd5c6
59 changed files with 3989 additions and 800 deletions

View File

@@ -1,7 +1,18 @@
# 定时任务模板 / 示例任务
# 定时任务模板 / 示例任务Python
`_template` 本身是可管理的真实任务:每天 08:00 输出 `hello world`
新建任务时复制本目录,改任务名与业务逻辑即可。
`_template`默认可管理的 Python hello world 任务(每天 08:00
新建 Python 任务时复制本目录,改任务名与业务逻辑即可。
## 多语言模板
| 目录 | 语言 | 入口 |
|---|---|---|
| `_template` | **Python默认** | `run.py` |
| `_template-javascript` | JavaScript | `run.js` |
| `_template-bash` | Bash | `run.sh` |
| `_template-powershell` | PowerShell | `run.ps1` |
非 Python 任务只需编写入口脚本disable / 锁 / 日志由 `cronctl run` 负责。
## 新建步骤
@@ -11,23 +22,22 @@ CRON_ROOT="/shumengya/project/agent/sproutclaw-cron"
cp -a "$CRON_ROOT/_template" "$CRON_ROOT/$TASK_ID"
sed -i "s/_template/$TASK_ID/g" "$CRON_ROOT/$TASK_ID/schedule.cron"
chmod +x "$CRON_ROOT/$TASK_ID/switch.sh"
# 编辑 run.py替换 hello world 为实际业务逻辑
# 编辑 schedule.cron调整 cron 表达式与注释
python3 "$CRON_ROOT/cronctl.py" status "$TASK_ID"
python3 "$CRON_ROOT/cronctl.py" run "$TASK_ID" # 手动试跑
python3 "$CRON_ROOT/cronctl.py" enable "$TASK_ID" # 开启并同步 /etc/cron.d/
python3 "$CRON_ROOT/cronctl.py" run "$TASK_ID"
python3 "$CRON_ROOT/cronctl.py" enable "$TASK_ID"
```
## 必须文件
| 文件 | 说明 |
|---|---|
| `run.py` | 任务入口,导出 `run(ctx) -> int` |
| `schedule.cron` | 系统 cron 配置cron 行须调用 `cronctl.py run <task-id>` |
| `switch.sh` | 可选,代理 `cronctl` 开关 |
| `task.json` | 可选;声明 `runtime``entry` |
| `run.py` | Python 任务入口,导出 `run(ctx) -> int` |
| `schedule.cron` | cron 配置,须调用 `cronctl.py run <task-id>` |
## run.py 约定
@@ -35,7 +45,7 @@ python3 "$CRON_ROOT/cronctl.py" enable "$TASK_ID" # 开启并同步 /etc/c
2. 使用 `task_logging(ctx)` 写日志到 `logs/<task-id>.log`
3. 使用 `acquire_cron_lock(ctx.lock_file, log=log)` 防止并发重入
4. 业务逻辑写在 `log("start")``log("end")` 之间
5. 需要汇总通知时使用 `shumengya_cron.notify.send_task_summary`
5. 需要汇总通知时使用 `notify.send_task_summary`
## schedule.cron 约定

View File

@@ -11,7 +11,7 @@ import sys as _sys, pathlib as _pl
_sys.path.insert(0, str(_pl.Path(__file__).resolve().parents[1] / "lib"))
del _sys, _pl
from shumengya_cron.runner import (
from runner import (
TaskContext,
acquire_cron_lock,
task_is_disabled,

View File

@@ -1,8 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
task_id="$(basename "$(dirname "$0")")"
action="${1:-toggle}"
shift || true
exec python3 /shumengya/project/agent/sproutclaw-cron/cronctl.py "$action" "$task_id" "$@"

8
_template/task.json Normal file
View File

@@ -0,0 +1,8 @@
{
"runtime": "python",
"entry": "run.py",
"tags": [
"Python",
"模板"
]
}