Skip to content

定时任务工具

PRX 提供 9 个定时任务和调度工具,构成完整的时间驱动自动化体系。从传统的 cron 表达式到自然语言时间描述,从简单的周期执行到带依赖链的复杂工作流,这组工具让 Agent 能够在无人值守的情况下执行定期任务。

定时任务工具分为两个层次:基础 cron 工具(cron_add/list/remove/update/run/runs)提供标准的 cron 任务管理;高级调度工具(schedule/xin)提供自然语言调度和依赖链执行。两者在 all_tools() 模式下始终可用。

Xin 调度引擎是 PRX 的高级调度器,支持任务之间的依赖关系、条件执行、失败重试和并发控制。它适用于需要多步骤协作的复杂自动化场景。

配置

config.toml 中配置定时任务系统:

toml
[cron]
enabled = true
storage = "sqlite"            # 任务存储后端:"memory" | "sqlite" | "file"
storage_path = "~/.prx/cron.db"

# 执行配置
max_concurrent_jobs = 5       # 最大并发执行任务数
default_timeout_secs = 300    # 默认任务超时(5分钟)
retry_on_failure = false      # 失败是否自动重试
max_retries = 3               # 最大重试次数

# Xin 调度引擎
[cron.xin]
enabled = true
dag_max_depth = 10            # 依赖链最大深度

工具策略控制:

toml
[security.tool_policy.groups]
automation = "allow"           # 组级别允许自动化工具

[security.tool_policy.tools]
cron_add = "supervised"        # 添加定时任务需审批
cron_remove = "supervised"     # 删除定时任务需审批
cron_run = "allow"             # 手动触发允许

使用方法

cron_add — 添加定时任务

json
{
  "tool": "cron_add",
  "arguments": {
    "name": "daily_report",
    "schedule": "0 9 * * *",
    "command": "生成今日工作报告并发送到 Telegram",
    "description": "每天早上 9 点生成并发送工作日报",
    "enabled": true
  }
}

Cron 表达式快速参考:

* * * * *
│ │ │ │ │
│ │ │ │ └── 星期(0-7,0和7都是周日)
│ │ │ └──── 月份(1-12)
│ │ └────── 日(1-31)
│ └──────── 小时(0-23)
└────────── 分钟(0-59)

常用表达式:
  0 9 * * *        每天 9:00
  0 */2 * * *      每 2 小时
  0 9 * * 1-5      工作日 9:00
  */5 * * * *      每 5 分钟
  0 0 1 * *        每月 1 号 0:00

cron_list — 列出定时任务

json
{
  "tool": "cron_list",
  "arguments": {}
}

返回:

json
{
  "jobs": [
    {
      "id": "job_001",
      "name": "daily_report",
      "schedule": "0 9 * * *",
      "description": "每天早上 9 点生成并发送工作日报",
      "enabled": true,
      "last_run": "2024-01-15T09:00:02Z",
      "next_run": "2024-01-16T09:00:00Z",
      "status": "idle"
    }
  ]
}

cron_remove — 删除定时任务

json
{
  "tool": "cron_remove",
  "arguments": {
    "id": "job_001"
  }
}

cron_update — 更新定时任务

json
{
  "tool": "cron_update",
  "arguments": {
    "id": "job_001",
    "schedule": "0 8 * * 1-5",
    "description": "工作日早上 8 点发送日报"
  }
}

cron_run — 手动触发

json
{
  "tool": "cron_run",
  "arguments": {
    "id": "job_001"
  }
}

cron_runs — 查看执行历史

json
{
  "tool": "cron_runs",
  "arguments": {
    "id": "job_001",
    "limit": 10
  }
}

返回:

json
{
  "runs": [
    {
      "run_id": "run_abc123",
      "started_at": "2024-01-15T09:00:02Z",
      "finished_at": "2024-01-15T09:00:45Z",
      "status": "success",
      "duration_ms": 43000
    },
    {
      "run_id": "run_def456",
      "started_at": "2024-01-14T09:00:01Z",
      "finished_at": "2024-01-14T09:01:30Z",
      "status": "failed",
      "error": "网络超时"
    }
  ]
}

schedule — 自然语言调度

json
{
  "tool": "schedule",
  "arguments": {
    "task": "检查 GitHub 仓库的新 issue 并分类",
    "when": "every 30 minutes during business hours",
    "recurring": true
  }
}

支持的自然语言表达:

  • in 5 minutes — 5 分钟后执行
  • tomorrow at 9am — 明天早上 9 点
  • every hour — 每小时
  • every weekday at 8:30 — 工作日 8:30
  • every 30 minutes during business hours — 工作时间内每 30 分钟

xin — Xin 调度引擎

json
{
  "tool": "xin",
  "arguments": {
    "action": "create_dag",
    "dag": {
      "name": "release_pipeline",
      "tasks": [
        {
          "id": "test",
          "command": "cargo test",
          "timeout": 300
        },
        {
          "id": "build",
          "command": "cargo build --release",
          "depends_on": ["test"],
          "timeout": 600
        },
        {
          "id": "deploy",
          "command": "发布到生产环境",
          "depends_on": ["build"],
          "condition": "build.exit_code == 0"
        }
      ]
    }
  }
}

参数

cron_add 参数

参数类型必填默认值说明
namestring任务名称
schedulestringCron 表达式
commandstring要执行的命令或任务描述
descriptionstring任务说明
enabledbooleantrue是否启用
timeoutinteger配置值超时秒数

cron_update 参数

参数类型必填说明
idstring任务 ID
schedulestring新的 cron 表达式
commandstring新的命令
descriptionstring新的说明
enabledboolean是否启用

cron_remove / cron_run 参数

参数类型必填说明
idstring任务 ID

cron_runs 参数

参数类型必填默认值说明
idstring任务 ID
limitinteger20返回记录数上限

schedule 参数

参数类型必填默认值说明
taskstring任务描述
whenstring自然语言时间表达
recurringbooleanfalse是否为周期性任务

xin 参数

参数类型必填说明
actionstring操作类型:create_dagrun_dagstatuscancel
dagobject条件DAG 定义(create_dag 时必填)
dag_idstring条件DAG ID(run_dagstatuscancel 时必填)

Xin 调度引擎

Xin 是 PRX 内置的高级调度引擎,支持有向无环图(DAG)任务编排:

核心特性

  • 依赖链 — 任务可以声明对其他任务的依赖,自动按拓扑序执行
  • 条件执行 — 任务可以根据前置任务的结果决定是否执行
  • 并发控制 — 无依赖关系的任务自动并行执行
  • 失败重试 — 支持配置重试次数和退避策略
  • 超时控制 — 每个任务独立的超时设置

DAG 执行流程

test ─┐
      ├─→ build ─→ deploy
lint ─┘           ↑
                  │ (condition: build.success)

在上述 DAG 中:

  1. testlint 并行执行(无依赖)
  2. build 等待 testlint 都完成
  3. deploy 仅在 build 成功时执行

安全性

任务权限

定时任务以 PRX Agent 的权限执行,继承 Agent 的工具策略和沙箱配置。建议:

  • cron_addcron_remove 设置 supervised 策略
  • 限制定时任务可使用的工具范围
  • 定期审查已注册的定时任务列表

超时保护

所有定时任务都有超时限制,防止任务卡死或无限运行:

toml
[cron]
default_timeout_secs = 300   # 默认 5 分钟

并发限制

max_concurrent_jobs 防止过多任务同时执行导致资源耗尽。超过限制的任务会排队等待。

审计日志

所有定时任务操作(创建、删除、执行、失败)都会记录在审计日志中,方便追溯和问题排查。

相关文档

Released under the Apache-2.0 License.