Crons
Crons match the clock and run across servers.
A cron defines work on a schedule.
export const const sendDigest: Cron<{
scope: {
kind: "system";
job: string;
actor: null;
};
}>
sendDigest = const cron: <{
readonly kind: "system";
readonly job: "tasks.sendDigest";
readonly actor: null;
}>(def: CronDef<{
scope: {
kind: "system";
job: string;
actor: null;
};
}, {
readonly kind: "system";
readonly job: "tasks.sendDigest";
readonly actor: null;
}>) => Cron<{
scope: {
kind: "system";
job: string;
actor: null;
};
}>
Work on a schedule, as `scope` (a cron has no caller): a handler, like a listener's, run when the clock says. Export
it from a module and call `registry.jobs.tick()` every minute (or from your platform's cron trigger). Must be
idempotent: a retry runs the whole handler again.
`cron({ schedule: "0 9 * * MON", scope: { kind: "system", job: "reports.weekly" }, async handler(ctx) { await ctx.run(send) } })`.cron({
schedule: stringFive fields: minute hour day-of-month month day-of-week. `"*\/15 * * * *"`, `"0 9 * * MON"`.schedule: "0 8 * * MON-FRI",
timezone?: string | undefinedIANA name, like `"Europe/Madrid"`. Default UTC. Matched on the wall clock, like Laravel: mind DST at 1 to 3 am.timezone: "Europe/Madrid",
scope: {
readonly kind: "system";
readonly job: "tasks.sendDigest";
readonly actor: null;
}
Who it runs as: a cron has no caller. Every function the handler runs checks its policy against it.scope: { kind: "system"kind: "system", job: "tasks.sendDigest"job: "tasks.sendDigest", actor: nullactor: null },
async handler: (ctx: CronCtx<{
scope: {
kind: "system";
job: string;
actor: null;
};
}, {
readonly kind: "system";
readonly job: "tasks.sendDigest";
readonly actor: null;
}>) => Promise<unknown>
The work, like a listener's handler: an action's ctx, plus `ctx.cron` (`scheduledAt`, `attempt`).handler(ctx: CronCtx<{
scope: {
kind: "system";
job: string;
actor: null;
};
}, {
readonly kind: "system";
readonly job: "tasks.sendDigest";
readonly actor: null;
}>
ctx) {
await function emailDigest(tasks: string[]): Promise<void>emailDigest(await ctx: CronCtx<{
scope: {
kind: "system";
job: string;
actor: null;
};
}, {
readonly kind: "system";
readonly job: "tasks.sendDigest";
readonly actor: null;
}>
ctx.ActionCtx<{ scope: { kind: "system"; job: string; actor: null; }; }, { readonly kind: "system"; readonly job: "tasks.sendDigest"; readonly actor: null; }>.run: <Fn<"query", undefined, string[], false, {
scope: {
kind: "system";
job: string;
actor: null;
};
}, undefined, undefined>>(fn: Fn<"query", undefined, string[], false, {
scope: {
kind: "system";
job: string;
actor: null;
};
}, undefined, undefined>, input?: undefined, options?: RunOptions<{
kind: "system";
job: string;
actor: null;
}> | undefined) => Promise<string[]>
Call any function: `await ctx.run(bookings.confirmPaid, { bookingId })`. Each mutation you run commits on its own.run(const list: Fn<"query", undefined, string[], false, {
scope: {
kind: "system";
job: string;
actor: null;
};
}, undefined, undefined>
list));
},
});
- Match (1).
registry.jobs.tick(at)selects crons due that minute. It defaults to the registry clock. Missed minutes stay missed. - Queue (2). Export each cron. The tick queues job ID
<cron id>@<minute>. - Scope. Declare a system scope. Each
ctx.runchecks policy. The cron reads its scope from code. Follow-up jobs usejobs.scope. - Context.
ctx.croncontains the ID, scheduled minute, and attempt. Retries keep the minute. Hooks seekind: "cron"andmeta. - Failure. A retry repeats the handler. Make it idempotent. Put changes that must commit together in one mutation. The
failedhandler receivesctx.cron.
Ticks and duplicate runs
Use Node or Cloudflare. On other hosts, schedule tick(at) each minute and sweep stored jobs.
Migrate cronRunsTable alongside jobsTable. The table keeps tick records for one day to deduplicate runs. Without it, duplicate ticks queue duplicate runs. Runs from different minutes can overlap.
Schedule syntax
Use minute, hour, day of month, month, and day of week. Use lists, ranges, steps, and names like JAN and MON. Sunday is 0 or 7. If both day fields are restricted, either can match. The parser rejects seconds, aliases, L, W, and #.
Use an IANA timezone. The default is UTC. A skipped local minute never matches. A repeated local minute matches twice.
Manual runs and tests
In production, run a cron now with registry.jobs.invoke.
await registry.jobs.invoke(tasks.sendDigest);
- Manual run. Pass a registered cron or ID to
invokeand use{ at }to pick a minute. The normal tick stays in place. Handoff errors reach the caller.
In tests, tick the clock with t.jobs.tick.
await t.jobs.tick("2026-12-01T07:00:00Z");
- Tick test.
t.jobs.tick(at)runs due crons inline and throws on failure. Omitatto use the test clock. - One cron.
t.jobs.perform(tasks.sendDigest, { at })runs it regardless of schedule.