[ PROMPT_NODE_24044 ]
Cron Triggers 说明文档
[ SKILL_DOCUMENTATION ]
# Cloudflare Cron Triggers
使用 cron 表达式调度 Worker 执行。在 Cloudflare 全球网络利用率较低的时段运行。
## 关键特性
- **仅限 UTC 执行** - 所有调度均基于 UTC 时间运行
- **5 字段 cron 语法** - 支持 Quartz 调度器扩展 (L, W, #)
- **全球传播** - 部署有 15 分钟延迟
- **至少一次交付** - 可能出现极少数重复执行的情况
- **工作流集成** - 触发长时间运行的多步骤任务
- **绿色计算** - 可选的碳感知调度,在低碳排放时段运行
## Cron 语法
┌─────────── 分钟 (0-59)
│ ┌───────── 小时 (0-23)
│ │ ┌─────── 月份中的日期 (1-31)
│ │ │ ┌───── 月份 (1-12, JAN-DEC)
│ │ │ │ ┌─── 星期几 (1-7, SUN-SAT, 1=Sunday)
* * * * *
**特殊字符:** `*` (任意), `,` (列表), `-` (范围), `/` (步长), `L` (最后), `W` (工作日), `#` (第几个)
## 常见调度
bash
*/5 * * * * # 每 5 分钟
0 * * * * # 每小时
0 2 * * * # 每天 UTC 时间凌晨 2 点 (非高峰期)
0 9 * * MON-FRI # 工作日 UTC 时间上午 9 点
0 0 1 * * # 每月 1 号 UTC 时间午夜
0 9 L * * # 每月最后一天 UTC 时间上午 9 点
0 10 * * MON#2 # 每月第二个周一 UTC 时间上午 10 点
*/10 9-17 * * MON-FRI # 工作日 9am-5pm 每 10 分钟
## 快速开始
**wrangler.jsonc:**
c
{
"name": "my-cron-worker",
"triggers": {
"crons": ["*/5 * * * *", "0 2 * * *"]
}
}
**处理器 (Handler):**
typescript
export default {
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
): Promise {
console.log("Cron:", controller.cron);
console.log("Time:", new Date(controller.scheduledTime));
ctx.waitUntil(asyncTask(env)); // 非阻塞
},
};
**本地测试:**
bash
npx wrangler dev
curl "http://localhost:8787/__scheduled?cron=*/5+*+*+*+*"
## 限制
- **免费版:** 每个 Worker 3 个触发器,10ms CPU 时间
- **付费版:** 无限触发器,50ms CPU 时间
- **传播:** 全球部署需 15 分钟
- **时区:** 仅限 UTC
## 阅读顺序
**初次接触 cron 触发器?** 从这里开始:
1. 本 README - 概览与快速入门
2. [configuration.md](./configuration.md) - 设置你的第一个 cron 触发器
3. [api.md](./api.md) - 理解处理器 API
4. [patterns.md](./patterns.md) - 常见用例与示例
**遇到问题?** 跳转至 [gotchas.md](./gotchas.md)
## 本参考文档包含
- [configuration.md](./configuration.md) - wrangler 配置、环境特定调度、绿色计算
- [api.md](./api.md) - ScheduledController, noRetry