| 1 | # Cron Triggers |
| 2 | |
| 3 | A Cron Trigger runs a Worker on a schedule. celld keeps the schedule of a script |
| 4 | in one reserved cell, so a fleet runs each occurrence once. Read the |
| 5 | [Cloudflare Cron Triggers documentation](https://developers.cloudflare.com/workers/configuration/cron-triggers/) |
| 6 | for the API. |
| 7 | |
| 8 | ## Example |
| 9 | |
| 10 | The [Cron Trigger example](../../examples/cron) logs the scheduled time once a |
| 11 | minute. |
| 12 | |
| 13 | <!-- celld-example: cron --> |
| 14 | |
| 15 | ## API |
| 16 | |
| 17 | A Worker exports a `scheduled(controller, env, ctx)` handler. |
| 18 | |
| 19 | - `controller.cron` holds the expression text. |
| 20 | - `controller.scheduledTime` holds the occurrence in milliseconds. |
| 21 | - `controller.noRetry()` stops the retries of a failed occurrence. |
| 22 | - `ctx.waitUntil()` extends the invocation. celld drains this work before the |
| 23 | next invocation. |
| 24 | |
| 25 | ## Schedules |
| 26 | |
| 27 | A project declares its schedules in a `triggers.crons` array. `celld deploy` |
| 28 | fails on a malformed expression, and on `triggers.crons` without `main`. celld |
| 29 | sets no limit on the number of expressions. |
| 30 | |
| 31 | celld reads the Cloudflare cron dialect. An expression has five fields: minute, |
| 32 | hour, day of month, month, and day of week. The resolution is one minute, and |
| 33 | the zone is UTC. Weekdays are |
| 34 | [numbered 1 to 7 from Sunday](https://developers.cloudflare.com/workers/configuration/cron-triggers/), |
| 35 | so `1-5` means Sunday to Thursday. |
| 36 | |
| 37 | Each field takes `*`, a value, an `a-b` range, an `a,b` list, and a `/n` step. |
| 38 | The month and day-of-week fields also take three-letter names in any case, such |
| 39 | as `JAN` and `MON`. The day-of-month field takes `L`, `L-<n>`, `LW`, `L-<n>W`, |
| 40 | and `<d>W`, and the day-of-week field takes `<dow>L` and `<dow>#<n>`. When both |
| 41 | day fields are restricted, they form a union, so `0 0 1 * MON` fires on the |
| 42 | first day of each month and on every Monday. Only a literal `*` leaves a day |
| 43 | field unrestricted. |
| 44 | |
| 45 | celld refuses a descending range such as `SAT-SUN` and a list that contains |
| 46 | `*`, such as `1,*`. Cloudflare accepts both with surprising results. A step |
| 47 | cannot exceed the width of its field, so `*/60` fails in the minute field. |
| 48 | |
| 49 | ## Execution |
| 50 | |
| 51 | `controller.scheduledTime` holds the scheduled occurrence and not the start of |
| 52 | the attempt, so a late run and a retry name the same minute. celld runs the |
| 53 | handlers of one script one at a time and drains the `waitUntil()` work of each |
| 54 | before the next. A handler that takes longer than the interval delays the next |
| 55 | occurrence. Move long work into a [queue](queues.md) or a |
| 56 | [Workflow](workflows.md). |
| 57 | |
| 58 | A handler that throws retries after 4 seconds, and the delay doubles after each |
| 59 | further failure. celld gives up after six failures, or at the next occurrence, |
| 60 | which never waits for a retry, so a schedule faster than the |
| 61 | backoff never retries. `controller.noRetry()` stops the retries. An |
| 62 | ownership move loses a pending retry. |
| 63 | |
| 64 | After downtime, celld runs one missed occurrence and skips the rest. Read the |
| 65 | interval from `controller.scheduledTime` to catch up in application code. |
| 66 | |
| 67 | The node that owns the reserved cell runs the occurrence, and that node can |
| 68 | change. A `triggers.crons` entry in a service-binding target never runs, and |
| 69 | celld logs a warning when it loads such a target. |
| 70 | |
| 71 | ## Differences from Cloudflare |
| 72 | |
| 73 | - celld rejects a descending range such as `SAT-SUN` and a list that contains |
| 74 | `*`. |
| 75 | - celld runs one handler for each occurrence across the fleet. After downtime, |
| 76 | it runs one missed occurrence and skips the rest. |
| 77 | - celld serializes the handlers for one script. It retries a failure until the |
| 78 | next occurrence unless the handler calls `noRetry()`. |
| 79 | - A service-binding target cannot run its own Cron Triggers. |
| 80 | |
| 81 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page |
| 82 | lists the runtime APIs and the unsupported services. |