Configure
Add aworkflows array to the wrangler.json file:
wrangler.json
name, binding, and class_name keys. Deploy refuses the schedules, limits, and script_name keys.
A Workflow needs a main entry module. Export the Workflow class from the index.js file.
Define a Workflow
A Workflow class extendsWorkflowEntrypoint and implements a run method. The following example sends a welcome email and waits one day. Then it waits up to seven days for the user to verify their email address:
index.js
run method, the following values are available:
this.env: the bindings of your App.event.payload: the parameters passed when the instance was created.event.instanceId: the ID of the instance.
step object has the following methods:
step.do(name, config, callback): runs the callback and saves its result. Theconfigargument is optional. By default, a step gets five retries with a 10-second delay and exponential backoff, and each attempt has a 10-minute timeout.step.sleep(name, duration): pauses the instance for a duration, such as"1 day".step.sleepUntil(name, date): pauses the instance until a date.step.waitForEvent(name, { type, timeout }): waits until something callsinstance.sendEvent({ type, payload })with a matchingtype. The default timeout is 24 hours. A timeout rejects the promise.
NonRetryableError from the step callback.
Write replay-safe steps
Therun method starts again from the top each time an instance continues. Finished steps return their saved results, but code outside steps runs again on each replay. Follow these rules:
- Put each side effect and each value that changes inside a
step.docall. This includes network calls, writes, emails,Date.now(),crypto.randomUUID(), andMath.random(). - Use stable step names. Don’t put times or random values in names. In a loop, put the item ID in the step name.
- Make each step callback idempotent, because a callback can run more than one time. Send
${event.instanceId}:STEP_NAMEas an idempotency key to external services, whereSTEP_NAMEis the name of the step. - Return results that are serializable and no larger than 1 MiB. A step can’t return a
ReadableStream. Keep large data in R2 and return the key. - An
awaitoutside a step must finish within 60 seconds. For long waits, usestep.sleeporstep.waitForEvent. - celld doesn’t roll back finished steps. Write each compensation, such as a refund, as its own step in a
try...catchblock.
Manage instances
Create an instance with the binding, and get it again by its ID:onboarding-${userId}. A webhook route can then get the instance and send it an event.
Calling create with an ID that already exists throws. This behavior prevents duplicate runs.
The output value returned by the status method is the return value of the run method.
Choose between Workflows and other tools
The following table compares Workflows with the other background features:Limits
The following table lists the limits of Workflows:Differences from Cloudflare
Keep the following celld behavior in mind when you port a Workflow from Cloudflare:- Deploy refuses the
schedules,limits, andscript_namekeys. - A step can’t return a
ReadableStream. - There’s no REST API or CLI for Workflow instances. To let people see instances, add an administrator route to your App that calls the
getandstatusmethods. - celld keeps finished instances for 30 days.