You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
3. Install [`Node.js`](https://nodejs.org/en/). Use a Node version manager like [Volta](https://volta.sh/) or [nvm](https://github.com/nvm-sh/nvm) to avoid permission issues and change Node.js versions. [Wrangler](/workers/wrangler/install-and-update/) requires a Node version of `16.17.0` or later.
34
+
<Renderfile="prereqs"product="workers" />
39
35
40
36
## 1. Define your Workflow
41
37
@@ -83,7 +79,7 @@ A Workflow definition:
83
79
2. Has at least one or more calls to `step.do` that encapsulates the logic of your Workflow.
84
80
3. Allows steps to return (optional) state, allowing a Workflow to continue execution even if subsequent steps fail, without having to re-run all previous steps.
85
81
86
-
A single Worker application can contain multiple Workflow definitions, as long as each Workflow has a unique class name. This can be useful for code re-use or to define Workflows are related to each other conceptually.
82
+
A single Worker application can contain multiple Workflow definitions, as long as each Workflow has a unique class name. This can be useful for code re-use or to define Workflows which are related to each other conceptually.
87
83
88
84
Each Workflow is otherwise entirely independent: a Worker that defines multiple Workflows is no different from a set of Workers that define one Workflow each.
89
85
@@ -93,11 +89,11 @@ Each `step` in a Workflow is an independently retriable function.
93
89
94
90
A `step` is what makes a Workflow powerful, as you can encapsulate errors and persist state as your Workflow progresses from step to step, avoiding your application from having to start from scratch on failure and ultimately build more reliable applications.
95
91
96
-
* A step can execute code (`step.do`) or sleep a Workflow (`step.sleep`)
92
+
* A step can execute code (`step.do`) or sleep a Workflow (`step.sleep`).
97
93
* If a step fails (throws an exception), it will be automatically be retried based on your retry logic.
98
-
*Ia step succeeds, any state it returns will be persisted within the Workflow.
94
+
*If a step succeeds, any state it returns will be persisted within the Workflow.
99
95
100
-
At it's most basic, a step looks like this:
96
+
At its most basic, a step looks like this:
101
97
102
98
```ts title="src/index.ts"
103
99
// Import the Workflow definition
@@ -123,7 +119,7 @@ Each call to `step.do` accepts three arguments:
123
119
124
120
1. (Required) A step name, which identifies the step in logs and telemetry
125
121
2. (Required) A callback function that contains the code to run for your step, and any state you want the Workflow to persist
126
-
3. (Optional) A `StepConfig` that defines the retry configuration (max retries, delay, and backoff algorithm) for the step.
122
+
3. (Optional) A `StepConfig` that defines the retry configuration (max retries, delay, and backoff algorithm) for the step
127
123
128
124
When trying to decide whether to break code up into more than one step, a good rule of thumb is to ask "do I want _all_ of this code to run again if just one part of it fails?". In many cases, you do _not_ want to repeatedly call an API if the following data processing stage fails, or if you get an error when attempting to send a completion or welcome email.
129
125
@@ -134,13 +130,13 @@ For example, each of the below tasks is ideally encapsulated in its own step, so
134
130
* Querying a D1 database or a database via [Hyperdrive](/hyperdrive/)
135
131
* Calling a third-party API
136
132
137
-
If a subsequent step fails, your Workflow can retry from that step, using any state returned from a previous step. This can also help you avoid unnecessarily querying a database or calling an paid API repeatedly for data you've already fetched.
133
+
If a subsequent step fails, your Workflow can retry from that step, using any state returned from a previous step. This can also help you avoid unnecessarily querying a database or calling an paid API repeatedly for data you have already fetched.
138
134
139
135
:::note
140
136
141
137
The term "Durable Execution" is widely used to describe this programming model.
142
138
143
-
"Durable" describes the ability of the program (application) to implicitly persist state without you having to write to an external store or serialize program state manual
139
+
"Durable" describes the ability of the program (application) to implicitly persist state without you having to manually write to an external store or serialize program state.
If you have changed the name of the Workflow in your wrangler commands, the JavaScript class name, or the name of the project you created, ensure that you update the values above to match.
169
+
If you have changed the name of the Workflow in your Wrangler commands, the JavaScript class name, or the name of the project you created, ensure that you update the values above to match the changes.
A Worker with a valid Workflow definition will be automatically registered by Workflows. You can list your current Workflows using wrangler:
335
+
A Worker with a valid Workflow definition will be automatically registered by Workflows. You can list your current Workflows using Wrangler:
340
336
341
337
```sh
342
338
npx wrangler workflows list
@@ -355,16 +351,16 @@ Showing last 1 workflow:
355
351
With your Workflow deployed, you can now run it.
356
352
357
353
1. A Workflow can run in parallel: each unique invocation of a Workflow is an _instance_ of that Workflow.
358
-
2. An instance will run to completion (success or failure)
354
+
2. An instance will run to completion (success or failure).
359
355
3. Deploying newer versions of a Workflow will cause all instances after that point to run the newest Workflow code.
360
356
361
357
:::note
362
358
363
-
Because Workflows can be long running, it's possible to have running instances that represent different versions of your Workflow code over time.
359
+
Because Workflows can be long running, it is possible to have running instances that represent different versions of your Workflow code over time.
364
360
365
361
:::
366
362
367
-
To trigger our Workflow, we will use the `wrangler` CLI and pass in an optional `--payload`. The `payload` will be passed to your Workflow's `run` method handler as an `Event`
363
+
To trigger our Workflow, we will use the `wrangler` CLI and pass in an optional `--payload`. The `payload` will be passed to your Workflow's `run` method handler as an `Event`.
@@ -447,7 +443,7 @@ From the output above, we can inspect:
447
443
* The status (success, failure, running) of each step
448
444
* Any state emitted by the step
449
445
* Any `sleep` state, including when the Workflow will wake up
450
-
* Retries associated with each step.
446
+
* Retries associated with each step
451
447
* Errors, including exception messages
452
448
453
449
:::note
@@ -480,8 +476,8 @@ Re-deploying the Workers script containing your Workflow code will re-create the
480
476
481
477
## Next steps
482
478
483
-
* Learn more about [how events are passed to a Workflow](/workflows/build/events-and-parameters/)
484
-
*Binding to and triggering Workflow instances using the [Workers API](/workflows/build/workers-api/)
485
-
*The[Rules of Workflows](/workflows/build/rules-of-workflows/) and best practices for building applications using Workflows.
479
+
* Learn more about [how events are passed to a Workflow](/workflows/build/events-and-parameters/).
480
+
*Learn more about binding to and triggering Workflow instances using the [Workers API](/workflows/build/workers-api/).
481
+
*Learn more about the[Rules of Workflows](/workflows/build/rules-of-workflows/) and best practices for building applications using Workflows.
486
482
487
483
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the [Cloudflare Developers community on Discord](https://discord.cloudflare.com).
0 commit comments