A Worker is great at handling an inbound request, but production systems also need work that runs without a request — a nightly refresh of a cache, a webhook fan-out that absorbs a burst, an email digest that fires at 09:00 every weekday. Cloudflare’s answer is two-shaped: cron triggers invoke a Worker on a schedule, and Queues let a producer enqueue messages that one or more consumer Workers drain with batching and retries. This article wires both correctly, including the parts that bite — idempotency, retry policy, dead-letter routing, and how to actually see what’s happening at runtime.
How to verify
A cron-triggered Worker shows its schedule in wrangler.toml and the API:
curl -s "https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/workers/scripts/my-job/schedules" \
-H "Authorization: Bearer ${CF_API_TOKEN}" | jq '.result'
wrangler tail my-job --format pretty
For Queues, the producer and consumer are separate Workers; list and inspect them:
wrangler queues list
wrangler queues consumer list my-queue
wrangler tail my-queue-consumer
A quick smoke-test from the producer Worker exercises the round trip — enqueue a test message, watch the consumer tail print it within a second or two, confirm the queue depth returns to zero.
What’s happening
A standard Worker runs in response to a fetch event from an HTTP request. Cron triggers add a second entry point: the runtime fires a scheduled event at the configured times, and a Worker exposes a scheduled handler alongside (or instead of) its fetch handler. The cron expression follows POSIX five-field format (* * * * * = minute, hour, day-of-month, month, day-of-week); intervals down to once per minute are supported, and a single Worker can declare multiple crons that all hit the same handler — the event.cron property tells you which one fired. Cron Workers are subject to the same CPU and wall-clock limits as fetch handlers; long jobs need to be split or moved to Queues.
Queues decouple the producer from the consumer. A producer Worker calls env.MY_QUEUE.send(payload) (or sendBatch) and returns immediately — the message is durably enqueued. A consumer Worker is bound to that queue and receives messages in batches (configurable batch size and a max wait time); the consumer’s queue handler processes the batch and, by acknowledging or retrying, drives the queue forward. If a message fails repeatedly, it lands in a dead-letter queue (DLQ) so the main flow keeps moving and you can inspect the bad payloads out of band.
The procedure
-
Declare cron triggers in
wrangler.toml. Add a[triggers]block with one or more cron expressions:name = "my-job" main = "src/index.ts" compatibility_date = "2026-05-01" [triggers] crons = ["*/15 * * * *", "0 9 * * 1-5"]The first runs every fifteen minutes; the second runs at 09:00 UTC Monday through Friday. Crons run in UTC — convert deliberately for local time.
-
Implement the scheduled handler. Export a
scheduledmethod alongside (or instead of)fetch:export default { async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) { if (event.cron === '0 9 * * 1-5') { ctx.waitUntil(sendDailyDigest(env)); } else { ctx.waitUntil(refreshCache(env)); } }, };ctx.waitUntil()keeps the Worker alive until the promise settles. Always wrap async work in it — otherwise the runtime may cut you off when the synchronous body returns. -
Design for idempotency. Cron triggers and queue retries both mean your handler can run twice for the same logical work. Make the handler safe to repeat: write with
INSERT … ON CONFLICT, key external API calls with an idempotency token, check “did this job run already in the last 15 minutes” before doing the work. This is the single biggest source of bugs in scheduled Workers. -
Create a queue and bind a producer.
wrangler queues create my-queuethen in the producer’swrangler.toml:[[queues.producers]] queue = "my-queue" binding = "Q"Send a message:
await env.Q.send({ kind: 'invalidate', key: 'page:home' });. Batch withsendBatch([...])when you have many at once. -
Bind a consumer with retry and DLQ policy. In the consumer Worker’s
wrangler.toml:[[queues.consumers]] queue = "my-queue" max_batch_size = 25 max_batch_timeout = 10 max_retries = 3 dead_letter_queue = "my-queue-dlq"Then implement the
queuehandler:export default { async queue(batch: MessageBatch<Job>, env: Env) { for (const msg of batch.messages) { try { await processOne(msg.body, env); msg.ack(); } catch (err) { msg.retry(); } } }, };ack()removes the message;retry()requeues it with exponential backoff up tomax_retries. After exhausting retries it goes to the DLQ. -
Observe.
wrangler tailstreams stdout/stderr from production in real time — leave it running while you smoke-test. For longer-running visibility, ship logs to Cloudflare Logpush or a third-party sink; the in-dashboard logs are best-effort sampling and not authoritative for high-volume jobs.
For Workers basics if you haven’t deployed one yet, the first-script article covers the setup path.
Common pitfalls
- Forgetting
ctx.waitUntil()in a scheduled handler — your async work gets cut off when the synchronous body returns and the job appears to “succeed” with nothing actually done. - Cron expressions are UTC; a job set for
0 9 * * *runs at 09:00 UTC, not 09:00 local — a four- or five-hour surprise depending on the zone. - A non-idempotent handler will eventually corrupt state because retries and overlapping cron invocations are both real, not theoretical.
max_batch_sizeandmax_batch_timeoutare a tradeoff — large batches throughput-favour, small batches latency-favour; pick deliberately and revisit when traffic shape changes.- A DLQ with no alarm on it is the same as no DLQ — wire a metric or a tail filter that pages someone when the DLQ depth is non-zero for more than a few minutes.
For workloads where the cron jobs and Queues are part of the operating model — the digest emails actually fire, the DLQ doesn’t quietly fill up, the retry policy matches the downstream’s tolerance — Stack Harbor runs the schedule and the observability around it as part of the managed-operations practice, so the background work isn’t background to anyone.