Cron Triggers in Cloudflare Workers, explained
A Cloudflare Workers Cron Trigger is one cron expression in wrangler.toml and one scheduled() export; there's no separate scheduler service to configure. We run five unrelated daily jobs off exactly one of these, in the Worker behind corsproxy.dev, and the handler that fires them is about a dozen lines long. This post walks through how the wiring works, the one line that's easy to leave out, and how we test cron logic without waiting a day for it to fire.
The config is the whole scheduler
A Cron Trigger is a [triggers] block in wrangler.toml and a scheduled export on the same Worker that already has a fetch export. Ours is one line:
[triggers]
crons = ["0 3 * * *"]
That's a standard five-field cron expression, evaluated in UTC, with no timezone setting anywhere to override it; 03:00 UTC is just the slot we picked, not a platform default. The crons field takes an array, so a single Worker can carry several independent schedules; a scheduled() handler can read event.cron to tell them apart. We only have the one entry, so our handler doesn't look at event.cron at all, and the parameter is named with a leading underscore in the source to say so.
One handler, five jobs
Everything that has to happen once a day on corsproxy.dev shares the same trigger: deleting request logs past their retention window, permanently purging data for accounts whose deletion grace period has passed, the inactivity sweep that warns and then retires unused free accounts, cleaning up expired dedupe records for admin alerts, and a scan that flags accounts repeatedly hitting our target-safety blocks so we can look at them by hand. None of these depend on each other, and none of them need to run more often than daily, so there was no reason to stand up five separate triggers, or a queue, to get five things done. The pattern is a dispatcher: the scheduled() handler itself does nothing but kick off every job and wait for all of them.
export default {
async scheduled(event, env, ctx) {
ctx.waitUntil(runDailyJobs(env));
}
};
async function runDailyJobs(env) {
const [purged, swept, flagged] = await Promise.all([
purgeOldRows(env.DB),
sweepInactiveAccounts(env),
flagAbuse(env)
]);
console.log('daily jobs done', { purged, swept, flagged });
}
That's the shape, simplified down to three jobs and generic names; the real handler wires up five. It has a sharp edge worth knowing about before you copy it: Promise.all runs the jobs concurrently, but if any one of them throws, the combined promise rejects, and the success branch, which in ours logs one summary line with a count from each job, never runs at all. The jobs themselves still finish in the background; you just lose the single combined log line that would have told you so. None of our five jobs catches its own errors before they reach the dispatcher, so right now a failure in one job is indistinguishable, from the daily summary log alone, from a failure in all five. The fix is for each job to catch and log its own outcome instead of letting a rejection propagate up into the shared Promise.all, which we haven't done yet because none of the five has actually failed in production so far.
ctx.waitUntil isn't optional
The scheduled export takes the same three arguments as fetch: the event, the environment bindings, and an ExecutionContext with a waitUntil method. That third argument is the part that's easy to skip, because the handler above would still type-check and run locally without it. The problem shows up in production: once the function you handed to the runtime as your handler returns, the Worker invocation is free to be torn down, and anything you started without passing its promise to ctx.waitUntil can simply stop mid-flight. scheduled() is declared async, but notice it never awaits the Promise.all directly; it hands that promise to ctx.waitUntil and returns right away. The handler finishing fast isn't a sign anything is wrong. waitUntil is what tells the runtime to keep the invocation alive until the background work, a round of D1 queries and KV scans, actually resolves, rather than stopping as soon as the handler returns.
Testing it without waiting a day
We don't trigger the Worker's actual scheduled() export to test this logic, live or otherwise. Every one of the five jobs is an ordinary exported async function that takes its bindings as plain arguments, so our test suite calls those functions directly, against fake KV and D1 stand-ins, the same way we'd test any other function. A test can set a user's last-activity timestamp to 38 days ago, call the inactivity sweep, and assert on what it did, without any cron, any wall-clock wait, or any deployed Worker in the loop. scheduled() itself stays untested by design: it's about a dozen lines of plumbing with nothing in it worth a unit test, and the five functions it calls carry their own coverage.
What we'd reach for instead, past five jobs
A single daily trigger stops being the right shape once the jobs stop being daily-and-independent. Something that needs to run on a schedule per user or per resource, rather than once for the whole account base, fits a Durable Object's own alarm better than a sweep over every row. Work that needs to retry on failure, or fan out to many small tasks instead of a handful of big ones, fits a queue better than a function inside Promise.all. We haven't needed either yet: five jobs, a shared daily cadence, and data small enough to sweep in a single run is still squarely in "one cron trigger" territory.