概述
Shannon 使用 Temporal 的原生 Schedule API 支持定期任务执行。创建基于 cron 的调度,在指定间隔自动执行任务。功能特性
- 基于 Cron 的调度,支持时区设置
- 资源限制,防止滥用
- 预算控制,每次执行独立计费
- 执行历史,带成本追踪
- 暂停/恢复/删除操作
- 多租户隔离,用户/租户所有权
创建调度
创建新的定期任务调度。请求体
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
name | string | 是 | 调度名称(最多 255 字符) |
description | string | 否 | 可选描述 |
cron_expression | string | 是 | Cron 调度(5 字段格式) |
timezone | string | 否 | IANA 时区(默认:UTC) |
task_query | string | 是 | 要执行的查询 |
task_context | object | 否 | 任务的附加上下文 |
max_budget_per_run_usd | number | 否 | 每次执行的最大预算(默认:10.0) |
timeout_seconds | integer | 否 | 执行超时(默认:600) |
响应
列出调度
列出调度,支持分页和过滤。查询参数
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
page | integer | 1 | 页码 |
page_size | integer | 50 | 每页条数(最大 100) |
status | string | - | 按状态过滤(ACTIVE, PAUSED) |
响应
获取调度详情
获取特定调度的详细信息。响应
更新调度
更新现有调度。请求体
所有字段均为可选。仅更新提供的字段。| 字段 | 类型 | 描述 |
|---|---|---|
name | string | 新调度名称 |
description | string | 新描述 |
cron_expression | string | 新 cron 调度 |
timezone | string | 新时区 |
task_query | string | 新任务查询 |
task_context | object | 新上下文 |
max_budget_per_run_usd | number | 新预算限制 |
timeout_seconds | integer | 新超时时间 |
获取执行历史
获取调度的执行历史。响应
响应字段
| 字段 | 类型 | 描述 |
|---|---|---|
runs | array | 调度执行记录列表 |
runs[].workflow_id | string | 此次执行的工作流 ID |
runs[].query | string | 执行的查询 |
runs[].status | string | 执行状态(如 COMPLETED、FAILED) |
runs[].result | string | 执行结果(为空时省略) |
runs[].error_message | string | 失败时的错误信息(为空时省略) |
runs[].model_used | string | 使用的 LLM 模型(为空时省略) |
runs[].provider | string | LLM 提供商(为空时省略) |
runs[].total_tokens | integer | 消耗的总 token 数 |
runs[].total_cost_usd | number | 总费用(美元) |
runs[].duration_ms | integer | 执行时长(毫秒,为空时省略) |
runs[].triggered_at | string | 触发时间(ISO 8601) |
runs[].started_at | string | 开始执行时间(为空时省略) |
runs[].completed_at | string | 执行完成时间(为空时省略) |
total_count | integer | 执行记录总数 |
page | integer | 当前页码 |
page_size | integer | 每页条数 |
暂停调度
暂停调度以停止后续执行。响应
恢复调度
恢复已暂停的调度。响应
删除调度
永久删除调度。响应
Cron 表达式格式
使用标准 cron 语法(5 个字段):常用示例
| 表达式 | 描述 |
|---|---|
0 9 * * * | 每天上午 9:00 |
0 */4 * * * | 每 4 小时 |
0 0 * * 1 | 每周一午夜 |
30 8 1 * * | 每月 1 日上午 8:30 |
0 12 * * 1-5 | 工作日中午 |
0 0 1,15 * * | 每月 1 日和 15 日 |
资源限制
Shannon 强制执行资源限制以防止滥用:| 限制 | 默认值 | 环境变量 |
|---|---|---|
| 每用户最大调度数 | 50 | SCHEDULE_MAX_PER_USER |
| 最小间隔 | 60 分钟 | SCHEDULE_MIN_INTERVAL_MINS |
| 每次运行最大预算 | $10.00 | SCHEDULE_MAX_BUDGET_USD |
错误响应
| 状态码 | 错误 | 描述 |
|---|---|---|
| 400 | Invalid cron expression | Cron 语法错误 |
| 400 | Interval too short | 低于最小间隔 |
| 403 | Schedule limit reached | 超出每用户最大调度数 |
| 404 | Schedule not found | 无效的调度 ID |
| 409 | Schedule already paused | 无法暂停已暂停的调度 |