| 1 | # Cron Triggers |
| 2 | |
| 3 | A Cron Trigger runs a Worker on a schedule instead of on a request. An |
| 4 | application uses one for periodic work, such as a cleanup, a report, or a poll |
| 5 | of an upstream service. celld keeps the schedule of a script in one reserved |
| 6 | cell, so a fleet of many nodes runs each occurrence once. Read the |
| 7 | [Cloudflare Cron Triggers documentation](https://developers.cloudflare.com/workers/configuration/cron-triggers/) |
| 8 | for the standard API behavior. |
| 9 | |
| 10 | ## Example |
| 11 | |
| 12 | The [Cron Trigger example](../../examples/cron) logs the scheduled time once a |
| 13 | minute. |
| 14 | |
| 15 | <!-- celld-example: cron --> |
| 16 | |
| 17 | ## Configuring a schedule |
| 18 | |
| 19 | A project declares its schedules in a `triggers.crons` array, and each entry is |
| 20 | one cron expression. `celld deploy` parses every entry, therefore a malformed |
| 21 | expression stops the deployment instead of failing at the first occurrence. A |
| 22 | configuration that sets `triggers.crons` without `main` also fails, because a |
| 23 | Cron Trigger needs a `scheduled` handler to call. |
| 24 | |
| 25 | celld reads the Cloudflare cron dialect and not the POSIX one. An expression has |
| 26 | five fields: minute, hour, day of month, month, and day of week. The resolution |
| 27 | is one minute and the zone is UTC. Cloudflare |
| 28 | [numbers the weekdays 1 to 7 from Sunday](https://developers.cloudflare.com/workers/configuration/cron-triggers/), |
| 29 | so `1-5` means Sunday to Thursday here and not Monday to Friday. |
| 30 | |
| 31 | Each field takes `*`, a single value, an `a-b` range, an `a,b` list, and a `/n` |
| 32 | step on any of those. The month field and the day-of-week field also take the |
| 33 | three-letter names in any case, such as `JAN` and `MON`. celld supports the |
| 34 | non-standard day forms as well: the day-of-month field takes `L`, `L-<n>`, `LW`, |
| 35 | `L-<n>W`, and `<d>W`, and the day-of-week field takes `<dow>L` and `<dow>#<n>`. |
| 36 | |
| 37 | The two day fields form a union and not an intersection when both of them carry |
| 38 | a restriction, which is the ordinary cron rule. `0 0 1 * MON` therefore fires on |
| 39 | the first day of each month and on every Monday. Only a literal `*` leaves a day |
| 40 | field unrestricted, so `*/2` in the day-of-month field joins that union. |
| 41 | |
| 42 | celld refuses a small set of expressions that Cloudflare accepts, because the |
| 43 | upstream parser answers them in a way that nobody writes on purpose. A |
| 44 | descending range such as `SAT-SUN` wraps around the end of the field there and |
| 45 | takes one value too many at the low end, so celld stops the deployment instead |
| 46 | of shipping a schedule that fires on an unasked day. A `*` inside a list such as |
| 47 | `1,*` means the minimum of the field there and not every value, so celld refuses |
| 48 | that form and asks for the values. celld also bounds a step by the width of its |
| 49 | field, therefore `*/60` fails for the minute field. |
| 50 | |
| 51 | A script can declare as many expressions as it needs, because celld sets no |
| 52 | count limit. A node fires only the schedule of the deployment that it serves, so |
| 53 | a `triggers.crons` entry in a service-binding target never runs, and celld logs |
| 54 | a warning when it loads such a target. |
| 55 | |
| 56 | ## The node that runs a scheduled event |
| 57 | |
| 58 | celld gives each deployment that declares `triggers.crons` one reserved cell, |
| 59 | named from the class `.cron` and the script name. That cell carries the whole |
| 60 | schedule of the script. Exactly one node owns a cell at a time, which is the |
| 61 | rule that every [Durable Object](durable-objects.md) already follows, so the |
| 62 | reserved cell is what makes an occurrence run once for the fleet and not once |
| 63 | for each node. |
| 64 | |
| 65 | A cron cell has no client that can wake it, so every node calls the cell once |
| 66 | after its deployment loads. The ownership record in the fleet bucket accepts one |
| 67 | of those callers, and the other nodes route their call to that owner. celld adds |
| 68 | no election and reserves no cron node. The call is idempotent, because the same |
| 69 | schedule and the same clock give the same deadline, so which node wins does not |
| 70 | matter. |
| 71 | |
| 72 | The cell does nothing between two occurrences, so it hibernates like any other |
| 73 | cell and costs no isolate while it waits. A deployment that declares no |
| 74 | expression leaves nothing to arm, and the cell then deletes its alarm and |
| 75 | retires instead of waking forever. |
| 76 | |
| 77 | The cell holds one alarm row, therefore one deadline covers every expression of |
| 78 | the script. celld arms that row at the earliest next occurrence across the whole |
| 79 | list. When the alarm fires, celld finds every expression that matches that |
| 80 | minute and calls `scheduled(controller, env, ctx)` once for each one. The |
| 81 | invocations run one after another inside the cell, because a cell runs one event |
| 82 | at a time. |
| 83 | |
| 84 | `controller.cron` holds the expression text as the developer wrote it, and |
| 85 | `controller.scheduledTime` holds the occurrence in milliseconds. celld reports |
| 86 | the occurrence and never the instant the attempt started, so a late run and a |
| 87 | retry both name the minute that they were scheduled for. `controller.noRetry()` |
| 88 | and `ctx.waitUntil()` behave as the |
| 89 | [scheduled handler documentation](https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled/) |
| 90 | describes, and celld drains the `waitUntil()` work of one invocation before it |
| 91 | starts the next one. |
| 92 | |
| 93 |  |
| 94 | |
| 95 | Cloudflare runs a scheduled event on its own global network and selects the |
| 96 | location for you. celld makes no such placement promise. The occurrence runs on |
| 97 | the node that owns the reserved cell at the deadline, and that owner can change |
| 98 | when a node stops, when a node drains, or when idle rebalancing moves the cell. |
| 99 | |
| 100 | ## Failure, retry, and a missed occurrence |
| 101 | |
| 102 | A `scheduled` handler that throws leaves its expression owed. celld logs the |
| 103 | error and arms a backoff of 4 seconds, and each further failure of the same |
| 104 | occurrence doubles that delay. celld gives up on the occurrence after six |
| 105 | failures. A retry repeats only the expressions that failed, because the |
| 106 | expressions that succeeded already ran for that occurrence. |
| 107 | `controller.noRetry()` removes an expression from that set, so a handler that |
| 108 | knows the work is pointless ends the loop itself. |
| 109 | |
| 110 | The next occurrence and the backoff compete for the one alarm row, and the |
| 111 | earlier deadline wins. A retry therefore never delays a scheduled run and never |
| 112 | replaces one. A schedule that fires faster than the backoff never retries at |
| 113 | all, which costs little, because the next attempt is already seconds away. celld |
| 114 | holds the retry state in memory, so an eviction or an ownership move loses a |
| 115 | pending retry and never the next occurrence. |
| 116 | |
| 117 | celld runs one missed occurrence after downtime. A deadline that is already due |
| 118 | when the fleet returns is a late tick and not a wrong one, so celld leaves the |
| 119 | armed alarm in place and runs it. The re-arm then walks forward from the current |
| 120 | time, therefore the occurrences between the late one and now do not run. An |
| 121 | application that must process every interval reads the interval from |
| 122 | `controller.scheduledTime` and catches up in its own code. |
| 123 | |
| 124 | Two occurrences of one script never overlap. The reserved cell runs one event at |
| 125 | a time, so a handler that takes longer than the interval holds the cell and |
| 126 | delays the next occurrence. Move long work into a [queue](queues.md) or a |
| 127 | [Workflow](workflows.md) when the schedule must stay on time. |
| 128 | |
| 129 | ## Differences from Cloudflare |
| 130 | |
| 131 | - celld rejects a descending range such as `SAT-SUN` and a list that contains |
| 132 | `*`. |
| 133 | - celld runs one handler for each occurrence across the fleet. After downtime, |
| 134 | it runs one missed occurrence and skips the rest. |
| 135 | - celld serializes the handlers for one script. It retries a failure until the |
| 136 | next occurrence unless the handler calls `noRetry()`. |
| 137 | - A service-binding target cannot run its own Cron Triggers. |
| 138 | |
| 139 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page |
| 140 | lists the runtime APIs and the unsupported services. |