Diffinery

homeflare/celldpublic

celld/docs/services/workflows.md
123 lines · 6.4 KBRaw History
1# Workflows
2
3A Workflow is a durable function that runs as a named sequence of steps,
4sleeps, and events. An application uses a Workflow for work that must survive a
5failure or a long delay, such as an order pipeline, a staged import, or an
6export that retries. celld runs each instance as a cell, so the state of an
7instance lives in that cell's storage and moves with the cell. Read the
8[Cloudflare Workflows documentation](https://developers.cloudflare.com/workflows/build/workers-api/)
9for the standard API behavior.
10
11## Example
12
13The [Workflow example](../../examples/workflow) fetches a document and stores
14the result of a durable step.
15
16<!-- celld-example: workflow -->
17
18## Steps and replay
19
20A Workflow class extends `WorkflowEntrypoint` and implements
21`run(event, step)`. `event.payload` holds the parameters that `create()`
22supplied, and the `step` object supplies the durable operations. A value that
23`run()` returns becomes the `output` field of `status()`. Read the
24[Workers API documentation](https://developers.cloudflare.com/workflows/build/workers-api/)
25for the complete signature.
26
27celld does not stop `run()` in place and continue it later. Each time an
28instance makes progress, celld calls `run()` again from the first line. A step
29that already finished returns its stored result, and its callback does not run
30a second time. celld writes each result into the storage of the instance cell,
31under a key that joins the step name and the occurrence count of that name, so
32a step inside a loop keeps one record for each iteration.
33
34![A resume re-enters run from the top, the ledger in the instance cell answers for each finished step, and the first blocked step arms the cell alarm so the cell hibernates until the next replay](workflows-flow.svg)
35
36This design puts one rule on the application code: everything outside a step
37callback runs again on every replay. Put each subrequest, each side effect, and
38each value that must stay stable inside a `step.do()` callback. Cloudflare
39gives the same rule in the
40[rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/).
41
42celld ends an invocation when no step callback runs and no step can make
43progress. `run()` can also await work that is not a step. Upstream permits that
44await and does not make it durable, and celld accepts it for a short time. A
45replay cannot resume such an await, therefore celld fails the instance when it
46keeps `run()` pending for 60 seconds while no step runs and none waits.
47
48## Sleeping and waiting for an event
49
50`step.sleep(name, duration)` and `step.sleepUntil(name, timestamp)` stop the
51instance until a deadline. `step.waitForEvent(name, options)` stops it until
52`sendEvent()` delivers an event of the matching type, or until a timeout that
53is 24 hours by default. Read
54[sleeping and retrying](https://developers.cloudflare.com/workflows/build/sleeping-and-retrying/)
55and
56[events and parameters](https://developers.cloudflare.com/workflows/build/events-and-parameters/)
57for the durations and the event rules.
58
59celld computes the deadline once and commits it with the step record. A later
60replay reads the stored deadline instead of adding the duration to the current
61time, therefore a crash or a slow resume cannot move the wake further away.
62
63A waiting instance holds no isolate. celld arms the alarm of the instance cell
64at the earliest blocked deadline and returns, so the cell hibernates like any
65other [cell](durable-objects.md) when the alarm is further away than the
66near-alarm residency window. That window is one hour, and
67`CELLD_ALARM_RESIDENT_MS` changes it. The armed alarm has a durable wake entry
68in the fleet, and the node that owns the cell at the deadline runs the next
69replay.
70
71An event that arrives before the instance reaches its wait step is buffered,
72and a matching step then consumes the buffered events in arrival order. celld
73deletes the event and writes the step record in one SQLite commit, so an
74acknowledged event cannot disappear. celld compares the stored deadline before
75the buffer, therefore a late replay cannot accept an event that arrived after
76the step timed out.
77
78## Retries, failure, and a node that stops
79
80`step.do()` retries a failed callback. celld uses the upstream defaults when
81the call supplies no `retries` object: a limit of 5 retries, a delay of 10
82seconds, exponential backoff, and a timeout of 10 minutes for one attempt. A
83`NonRetryableError` stops the retries at once. When the retries run out, the
84step promise rejects and `run()` can catch it. An error that escapes `run()`
85moves the instance to the `errored` status.
86
87celld commits the time of the next attempt when an attempt fails, so a replay
88waits out the rest of the backoff instead of starting it again. A pending retry
89suspends the instance in the same way a sleep does, and `status()` reports
90`waiting` for both.
91
92A node that stops does not lose an instance. The instance is a cell, so it has
93the ownership, the fencing, and the replication that every cell has. celld
94leaves the alarm that started an invocation armed for the whole invocation, and
95it consumes that alarm only after the invocation returns cleanly. A node that
96dies in the middle therefore leaves an armed alarm, and the next owner replays
97`run()` over the step results that reached a replicated commit. A step whose
98result did not commit runs again, which is why a step callback must tolerate a
99second attempt.
100
101celld enforces no limit on the number of instances that run at once. The memory
102of the fleet and the resident-cell cap bound that number instead. A waiting
103instance costs no memory, so a fleet holds far more waiting instances than
104running ones.
105
106## Differences from Cloudflare
107
108- celld retains a successful or failed instance for 30 days by default. Each
109 duration in the `retention` option can be at most 30 days.
110- `locationHint` accepts the Cloudflare values, but fleet ownership selects the
111 cell location.
112- Non-step work cannot remain pending for more than 60 seconds.
113- A step result, an event payload, and the workflow parameters each have a
114 1 MiB limit.
115- Rollback, a sensitive step result, and a `ReadableStream` step result are
116 unavailable.
117- A `workflows` entry cannot carry `schedules`, `limits`, or a `script_name`
118 that names another script.
119- The Workflows REST API and the `wrangler workflows` commands are
120 unavailable. Drive an instance through the binding.
121
122The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists
123the runtime APIs and the unsupported services.