encore-cron
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEncore Cron Jobs
Encore 定时任务
Instructions
指令
A declaration in Encore.ts ties a schedule to an existing endpoint. The endpoint runs at the chosen cadence. Declare the at package level — not inside a function.
CronJobapi(...)CronJobtypescript
import { CronJob } from "encore.dev/cron";
import { api } from "encore.dev/api";
// 1. The endpoint to call (typically internal: expose: false)
export const aggregateDailyOrders = api(
{ expose: false },
async (): Promise<void> => {
// Aggregation logic
}
);
// 2. Package-level cron declaration
const _ = new CronJob("aggregate-daily-orders", {
title: "Aggregate orders for the previous day",
schedule: "0 2 * * *", // 02:00 UTC every day
endpoint: aggregateDailyOrders,
});Encore.ts 中的 声明会将一个调度计划与现有的 端点关联起来。该端点会按照设定的频率运行。请在 package 层级声明 ,不要将其放在函数内部。
CronJobapi(...)CronJobtypescript
import { CronJob } from "encore.dev/cron";
import { api } from "encore.dev/api";
// 1. The endpoint to call (typically internal: expose: false)
export const aggregateDailyOrders = api(
{ expose: false },
async (): Promise<void> => {
// Aggregation logic
}
);
// 2. Package-level cron declaration
const _ = new CronJob("aggregate-daily-orders", {
title: "Aggregate orders for the previous day",
schedule: "0 2 * * *", // 02:00 UTC every day
endpoint: aggregateDailyOrders,
});Schedule Formats
调度格式
| Field | Example | Description |
|---|---|---|
| | Simple interval. Must divide 24h evenly — |
| | Standard cron expression (5 fields, UTC). |
| 字段 | 示例 | 说明 |
|---|---|---|
| | 简单间隔。必须能够整除 24 小时—— |
| | 标准 cron 表达式(5 个字段,使用 UTC)。 |
Common cron expressions
常见 cron 表达式
| Cron | Meaning |
|---|---|
| Every hour, on the hour |
| Daily at 02:00 UTC |
| Weekly on Sunday at midnight UTC |
| 04:00 UTC on the 15th of each month |
| Cron | 含义 |
|---|---|
| 每小时整点执行 |
| 每天 UTC 02:00 执行 |
| 每周日 UTC 午夜执行 |
| 每月 15 日 UTC 04:00 执行 |
Important behaviour
重要行为
- Cron jobs do not execute when running locally with . Only deployed environments fire crons.
encore run - The cron endpoint should be so it can't be triggered externally — only the cron scheduler should call it.
expose: false - All times in are UTC. Convert from local time when designing the schedule.
schedule - The endpoint must already exist at module load — declare it before the .
CronJob
- **使用 在本地运行时,Cron 任务不会执行。**只有已部署的环境才会触发 cron。
encore run - Cron 端点应设置为 ,以防止从外部触发——只能由 cron 调度器调用。
expose: false - 中的所有时间均使用 UTC。设计调度计划时,需要从本地时间转换。
schedule - 端点必须已存在于模块加载阶段——请先声明端点,再声明 。
CronJob
Guidelines
指南
- Use for "run on a regular interval" (must divide 24h).
every - Use for specific times of day or days of week.
schedule - Keep endpoint logic idempotent: a cron may fire late or be retried in a redeploy window.
- For event-driven background work (not time-driven), use the skill instead.
encore-pubsub
- 对于“按固定间隔运行”的任务,使用 (必须能够整除 24 小时)。
every - 对于每天或每周的特定时间,使用 。
schedule - 保持端点逻辑具备幂等性:cron 可能会延迟触发,也可能在重新部署期间重试。
- 对于事件驱动的后台工作(非时间驱动),请改用 skill。
encore-pubsub