Diffinery

homeflare/celldpublic

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