| 1 | # Workflows |
| 2 | |
| 3 | A Workflow is a durable function that runs as a named sequence of steps, |
| 4 | sleeps, and events. celld runs each instance as a cell. Read the |
| 5 | [Cloudflare Workflows documentation](https://developers.cloudflare.com/workflows/build/workers-api/) |
| 6 | for the API. |
| 7 | |
| 8 | ## Example |
| 9 | |
| 10 | The [Workflow example](../../examples/workflow) fetches a document and stores |
| 11 | the result of a durable step. |
| 12 | |
| 13 | <!-- celld-example: workflow --> |
| 14 | |
| 15 | ## API |
| 16 | |
| 17 | - A class extends `WorkflowEntrypoint` and implements `run(event, step)`. |
| 18 | `event.payload` holds the parameters, and the return value becomes the |
| 19 | `output` field of `status()`. |
| 20 | - `step.do(name, callback)` runs a durable step, and it retries the callback on |
| 21 | failure. |
| 22 | - `step.sleep(name, duration)` and `step.sleepUntil(name, timestamp)` stop the |
| 23 | instance until a deadline. |
| 24 | - `step.waitForEvent(name, options)` stops the instance until a matching event. |
| 25 | - `env.MY_WORKFLOW.create()`, `createBatch()`, and `get(id)` return an |
| 26 | instance. `deleteBatch()` deletes several instances. |
| 27 | - An instance has `status()`, `sendEvent()`, `pause()`, `resume()`, |
| 28 | `restart()`, `terminate()`, and `delete()`. |
| 29 | |
| 30 | ## Replay |
| 31 | |
| 32 | Each time an instance makes progress, celld calls `run()` again from the first |
| 33 | line. A finished step returns its stored result and does not run its callback |
| 34 | again. All code outside a step callback therefore runs again on every replay. |
| 35 | Put each subrequest, each side effect, and each value that must stay stable |
| 36 | inside a `step.do()` callback, as the |
| 37 | [rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/) |
| 38 | require. A step whose result did not commit before a node failure runs again, |
| 39 | so a step callback must tolerate a second attempt. |
| 40 | |
| 41 | `run()` can await work that is not a step, but a replay cannot resume that |
| 42 | await. celld fails the instance when such work keeps `run()` pending for 60 |
| 43 | seconds while no step runs or waits. |
| 44 | |
| 45 | ## Sleeps, events, and retries |
| 46 | |
| 47 | `step.waitForEvent()` times out after 24 hours by default. An event that |
| 48 | arrives before the instance reaches its wait step is buffered. A sleep, a wait, |
| 49 | and a pending retry each store their deadline, so a crash or a slow replay |
| 50 | cannot move the deadline. `status()` reports `waiting` for all three. |
| 51 | |
| 52 | A waiting instance holds no isolate. Its cell hibernates when the next deadline |
| 53 | is further away than the near-alarm residency window. That window is one hour, |
| 54 | and `CELLD_ALARM_RESIDENT_MS` changes it. |
| 55 | |
| 56 | `step.do()` uses the Cloudflare retry defaults when the call supplies no |
| 57 | `retries` object: 5 retries, a delay of 10 seconds, exponential backoff, and a |
| 58 | timeout of 10 minutes for one attempt. |
| 59 | |
| 60 | celld sets no limit on the number of concurrent instances. Fleet memory and the |
| 61 | resident-cell cap bound it. |
| 62 | |
| 63 | ## Differences from Cloudflare |
| 64 | |
| 65 | - celld retains a successful or failed instance for 30 days by default. Each |
| 66 | duration in the `retention` option can be at most 30 days. |
| 67 | - `locationHint` accepts the Cloudflare values, but fleet ownership selects the |
| 68 | cell location. |
| 69 | - Non-step work cannot remain pending for more than 60 seconds. |
| 70 | - A step result, an event payload, and the workflow parameters each have a |
| 71 | 1 MiB limit. |
| 72 | - Rollback, a sensitive step result, and a `ReadableStream` step result are |
| 73 | unavailable. |
| 74 | - A `workflows` entry cannot carry `schedules`, `limits`, or a `script_name` |
| 75 | that names another script. |
| 76 | - The Workflows REST API and the `wrangler workflows` commands are |
| 77 | unavailable. Drive an instance through the binding. |
| 78 | |
| 79 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists |
| 80 | the runtime APIs and the unsupported services. |