Diffinery

homeflare/celldpublic

celld/docs/services/cron-triggers.md
82 lines · 3.6 KBRaw History
1# Cron Triggers
2
3A Cron Trigger runs a Worker on a schedule. celld keeps the schedule of a script
4in 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/)
6for the API.
7
8## Example
9
10The [Cron Trigger example](../../examples/cron) logs the scheduled time once a
11minute.
12
13<!-- celld-example: cron -->
14
15## API
16
17A 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
27A project declares its schedules in a `triggers.crons` array. `celld deploy`
28fails on a malformed expression, and on `triggers.crons` without `main`. celld
29sets no limit on the number of expressions.
30
31celld reads the Cloudflare cron dialect. An expression has five fields: minute,
32hour, day of month, month, and day of week. The resolution is one minute, and
33the zone is UTC. Weekdays are
34[numbered 1 to 7 from Sunday](https://developers.cloudflare.com/workers/configuration/cron-triggers/),
35so `1-5` means Sunday to Thursday.
36
37Each field takes `*`, a value, an `a-b` range, an `a,b` list, and a `/n` step.
38The month and day-of-week fields also take three-letter names in any case, such
39as `JAN` and `MON`. The day-of-month field takes `L`, `L-<n>`, `LW`, `L-<n>W`,
40and `<d>W`, and the day-of-week field takes `<dow>L` and `<dow>#<n>`. When both
41day fields are restricted, they form a union, so `0 0 1 * MON` fires on the
42first day of each month and on every Monday. Only a literal `*` leaves a day
43field unrestricted.
44
45celld 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
47cannot 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
52the attempt, so a late run and a retry name the same minute. celld runs the
53handlers of one script one at a time and drains the `waitUntil()` work of each
54before the next. A handler that takes longer than the interval delays the next
55occurrence. Move long work into a [queue](queues.md) or a
56[Workflow](workflows.md).
57
58A handler that throws retries after 4 seconds, and the delay doubles after each
59further failure. celld gives up after six failures, or at the next occurrence,
60which never waits for a retry, so a schedule faster than the
61backoff never retries. `controller.noRetry()` stops the retries. An
62ownership move loses a pending retry.
63
64After downtime, celld runs one missed occurrence and skips the rest. Read the
65interval from `controller.scheduledTime` to catch up in application code.
66
67The node that owns the reserved cell runs the occurrence, and that node can
68change. A `triggers.crons` entry in a service-binding target never runs, and
69celld 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
81The [Cloudflare compatibility](../cloudflare-compat.md#services) page
82lists the runtime APIs and the unsupported services.