feat: init sproutclaw-cron 定时任务管理框架
- 每任务一目录结构,统一开关与日志 - cronctl CLI:enable/disable/toggle/status/run/sync-cron - 公共库 lib/shumengya_cron:runner/manager/notify/ssh - _template 示例任务(hello world + 复制模板) - webui 管理面板:FastAPI 后端 + 文档式响应式前端
This commit is contained in:
46
_template/run.py
Normal file
46
_template/run.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""
|
||||
Hello World 示例任务:每天 08:00 输出一句 hello world。
|
||||
|
||||
新建任务时复制整个 _template/ 目录为 <task-id>/,更新 schedule.cron 中的
|
||||
任务名,再替换下方业务逻辑。命名约定:<主机名>-<功能描述>。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
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 (
|
||||
TaskContext,
|
||||
acquire_cron_lock,
|
||||
task_is_disabled,
|
||||
task_logging,
|
||||
)
|
||||
|
||||
|
||||
def run(ctx: TaskContext) -> int:
|
||||
if task_is_disabled(ctx):
|
||||
return 0
|
||||
|
||||
with task_logging(ctx) as log:
|
||||
with acquire_cron_lock(ctx.lock_file, log=log) as locked:
|
||||
if not locked:
|
||||
return 0
|
||||
|
||||
log("start")
|
||||
log("hello world")
|
||||
log("end")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
import pathlib
|
||||
task_id = pathlib.Path(__file__).parent.name
|
||||
ctx = TaskContext.from_task_id(task_id)
|
||||
return run(ctx)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user