Skip to content

Scheduled tasks

A task is a handler that runs on a schedule instead of in answer to an interaction, such as a daily reminder or a nightly cleanup. Register it on the app with a name and a cron expression:

app.task("standup", "0 13 * * 1-5", async (c) => {
await c.send(process.env.STANDUP_CHANNEL_ID!, "Standup in 15 minutes!");
});

On Cloudflare, Vercel, Netlify, and AWS, discord0 deploy writes every task into the host’s scheduler, and the host calls the handler when the schedule fires. The Node.js and Bun servers run the schedule themselves. The app itself starts no timers.

The expression has five fields: minute, hour, day of the month, month, and day of the week (0 is Sunday, 6 is Saturday). Each field takes numbers, *, lists (1,15), ranges (1-5), and steps (*/15, 0-30/10). Names such as MON, nicknames such as @daily, and L, W, #, and ? aren’t accepted, because not every host supports them. As in standard cron, when both day fields are set, the task runs on days that match either.

Schedules run in UTC unless you set a time zone:

app.task(
{ name: "standup", expression: "0 9 * * 1-5", timezone: "America/Toronto" },
handler,
);

Cloudflare, Vercel, and Netlify schedule in UTC only, so deploying a task with another time zone there fails. A fixed UTC offset would drift by an hour across daylight saving time, so write the expression in UTC instead, or use an adapter that honors time zones: AWS, Node.js, or Bun.

Task names are 1 to 64 lowercase letters, digits, hyphens, and underscores, and start with a letter or digit. Each name is unique across the app and its mounted sub-apps.

The handler receives c:

Member Description
c.task The task’s name, expression, and timezone.
c.scheduledTime When the host scheduled this run, or the current time when it has none.
c.send(id, msg) Posts a message to a channel as the bot and returns it.

A handler that throws fails the run. A hosted scheduler records the failure as it does for any other scheduled job, and the Node.js and Bun servers log it.

Run a task once, now, with the variables in your local .env and .env.local. The run posts to Discord for real:

Terminal window
npx discord0 task standup

app.runTask(name) does the same from code, which helps in tests and with a scheduler of your own. It resolves when the handler finishes and rejects if it throws. app.tasks lists every task’s schedule.

Adapter Scheduler Time zones Limits
Cloudflare Cron Triggers in wrangler.json, handled by the Worker’s scheduled() UTC only Workers Free: 5 triggers per account and 10 ms of CPU per run. Paid: 1,000 triggers.
Vercel Cron Jobs in vercel.json, calling /api/tasks UTC only Production deployments only. Hobby: at most once a day, at any minute of the scheduled hour.
Netlify One scheduled function per task UTC only Published deploys only. Each run stops after 30 seconds.
AWS EventBridge Scheduler, invoking a tasks function Yes The function stops after 300 seconds. Scheduler bills per invocation.
Node.js In the server process Yes Runs only while the server is up.
Bun Bun.cron in the server process Yes Runs only while the server is up.

On Cloudflare, tasks with the same schedule share one Cron Trigger, which runs them all. Trigger changes can take up to 15 minutes to apply. A deploy prints each task with notes like these for its host.

On Vercel, set CRON_SECRET in the project. Vercel sends it with each cron request, and the task route refuses requests without it. While CRON_SECRET is unset, the route answers 503 and no task runs.

The Node.js and Bun servers run tasks in process, so every running copy of the server runs every task. If you run more than one, set DISCORD0_TASKS=off in all but one, or pass tasks: false to serve(). If a run is still going when its next time comes, the server skips that time instead of starting a second run.

Removing a task and deploying again removes its schedule from the host.