| 1 | # Workflows |
| 2 | |
| 3 | A Workflow is a durable function that runs as a named sequence of steps, |
| 4 | sleeps, and events. An application uses a Workflow for work that must survive a |
| 5 | failure or a long delay, such as an order pipeline, a staged import, or an |
| 6 | export that retries. celld runs each instance as a cell, so the state of an |
| 7 | instance 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/) |
| 9 | for the standard API behavior. |
| 10 | |
| 11 | ## Example |
| 12 | |
| 13 | The [Workflow example](../../examples/workflow) fetches a document and stores |
| 14 | the result of a durable step. |
| 15 | |
| 16 | <!-- celld-example: workflow --> |
| 17 | |
| 18 | ## Steps and replay |
| 19 | |
| 20 | A Workflow class extends `WorkflowEntrypoint` and implements |
| 21 | `run(event, step)`. `event.payload` holds the parameters that `create()` |
| 22 | supplied, 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/) |
| 25 | for the complete signature. |
| 26 | |
| 27 | celld does not stop `run()` in place and continue it later. Each time an |
| 28 | instance makes progress, celld calls `run()` again from the first line. A step |
| 29 | that already finished returns its stored result, and its callback does not run |
| 30 | a second time. celld writes each result into the storage of the instance cell, |
| 31 | under a key that joins the step name and the occurrence count of that name, so |
| 32 | a step inside a loop keeps one record for each iteration. |
| 33 | |
| 34 |  |
| 35 | |
| 36 | This design puts one rule on the application code: everything outside a step |
| 37 | callback runs again on every replay. Put each subrequest, each side effect, and |
| 38 | each value that must stay stable inside a `step.do()` callback. Cloudflare |
| 39 | gives the same rule in the |
| 40 | [rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/). |
| 41 | |
| 42 | celld ends an invocation when no step callback runs and no step can make |
| 43 | progress. `run()` can also await work that is not a step. Upstream permits that |
| 44 | await and does not make it durable, and celld accepts it for a short time. A |
| 45 | replay cannot resume such an await, therefore celld fails the instance when it |
| 46 | keeps `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 |
| 51 | instance until a deadline. `step.waitForEvent(name, options)` stops it until |
| 52 | `sendEvent()` delivers an event of the matching type, or until a timeout that |
| 53 | is 24 hours by default. Read |
| 54 | [sleeping and retrying](https://developers.cloudflare.com/workflows/build/sleeping-and-retrying/) |
| 55 | and |
| 56 | [events and parameters](https://developers.cloudflare.com/workflows/build/events-and-parameters/) |
| 57 | for the durations and the event rules. |
| 58 | |
| 59 | celld computes the deadline once and commits it with the step record. A later |
| 60 | replay reads the stored deadline instead of adding the duration to the current |
| 61 | time, therefore a crash or a slow resume cannot move the wake further away. |
| 62 | |
| 63 | A waiting instance holds no isolate. celld arms the alarm of the instance cell |
| 64 | at the earliest blocked deadline and returns, so the cell hibernates like any |
| 65 | other [cell](durable-objects.md) when the alarm is further away than the |
| 66 | near-alarm residency window. That window is one hour, and |
| 67 | `CELLD_ALARM_RESIDENT_MS` changes it. The armed alarm has a durable wake entry |
| 68 | in the fleet, and the node that owns the cell at the deadline runs the next |
| 69 | replay. |
| 70 | |
| 71 | An event that arrives before the instance reaches its wait step is buffered, |
| 72 | and a matching step then consumes the buffered events in arrival order. celld |
| 73 | deletes the event and writes the step record in one SQLite commit, so an |
| 74 | acknowledged event cannot disappear. celld compares the stored deadline before |
| 75 | the buffer, therefore a late replay cannot accept an event that arrived after |
| 76 | the 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 |
| 81 | the call supplies no `retries` object: a limit of 5 retries, a delay of 10 |
| 82 | seconds, 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 |
| 84 | step promise rejects and `run()` can catch it. An error that escapes `run()` |
| 85 | moves the instance to the `errored` status. |
| 86 | |
| 87 | celld commits the time of the next attempt when an attempt fails, so a replay |
| 88 | waits out the rest of the backoff instead of starting it again. A pending retry |
| 89 | suspends the instance in the same way a sleep does, and `status()` reports |
| 90 | `waiting` for both. |
| 91 | |
| 92 | A node that stops does not lose an instance. The instance is a cell, so it has |
| 93 | the ownership, the fencing, and the replication that every cell has. celld |
| 94 | leaves the alarm that started an invocation armed for the whole invocation, and |
| 95 | it consumes that alarm only after the invocation returns cleanly. A node that |
| 96 | dies 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 |
| 98 | result did not commit runs again, which is why a step callback must tolerate a |
| 99 | second attempt. |
| 100 | |
| 101 | celld enforces no limit on the number of instances that run at once. The memory |
| 102 | of the fleet and the resident-cell cap bound that number instead. A waiting |
| 103 | instance costs no memory, so a fleet holds far more waiting instances than |
| 104 | running 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 | |
| 122 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists |
| 123 | the runtime APIs and the unsupported services. |