Skip to content

Scheduling

Zeroback provides two mechanisms for running functions in the future: the Scheduler for one-off scheduled calls, and Cron Jobs for recurring schedules.

The scheduler is available as ctx.scheduler in mutations and actions. It lets you schedule a function to run at a specific time or after a delay.

Backed by SQLite + Cloudflare Durable Object Alarms.

scheduler.runAfter(delayMs, fnName, args?)

Section titled “scheduler.runAfter(delayMs, fnName, args?)”

Schedule a function to run after a delay.

scheduler.runAfter(
delayMs: number,
fnName: string,
args?: unknown
): Promise<string>
ParameterTypeDefaultDescription
delayMsnumberDelay in milliseconds
fnNamestringFunction to call (e.g., "tasks:create")
argsunknownundefinedArguments to pass to the function

Returns: A job ID (string) that can be used to cancel the scheduled job.

export const scheduleReminder = mutation({
args: { taskId: v.string(), delayMs: v.number() },
handler: async (ctx, args) => {
const jobId = await ctx.scheduler.runAfter(
args.delayMs,
"notifications:sendReminder",
{ taskId: args.taskId }
);
return jobId;
},
});

Schedule a function to run at a specific time.

scheduler.runAt(
timestamp: number,
fnName: string,
args?: unknown
): Promise<string>
ParameterTypeDefaultDescription
timestampnumberUnix timestamp in milliseconds
fnNamestringFunction to call
argsunknownundefinedArguments to pass

Returns: A job ID.

export const scheduleAtTime = mutation({
args: { taskId: v.string(), runAt: v.number() },
handler: async (ctx, args) => {
return await ctx.scheduler.runAt(
args.runAt,
"tasks:processTask",
{ taskId: args.taskId }
);
},
});

Cancel a previously scheduled job.

scheduler.cancel(id: string): Promise<void>
ParameterTypeDescription
idstringThe job ID returned by runAfter or runAt
export const cancelJob = mutation({
args: { jobId: v.string() },
handler: async (ctx, args) => {
await ctx.scheduler.cancel(args.jobId);
},
});

Cron jobs run functions on recurring schedules. See Functions > Cron Jobs for the full API reference.

Quick example:

zeroback/crons.ts
import { cronJobs } from "@zeroback/server";
const crons = cronJobs();
crons.interval("cleanup", { minutes: 30 }, "tasks:cleanupDone");
crons.daily("digest", { hourUTC: 9 }, "email:sendDigest");
crons.cron("nightly", "0 3 * * *", "jobs:nightly");
export default crons;