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
Copy file name to clipboardExpand all lines: src/content/docs/workflows/build/rules-of-steps.mdx
+176-5Lines changed: 176 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,193 @@ title: Rules of Steps
3
3
pcx_content_type: concept
4
4
sidebar:
5
5
order: 10
6
-
7
6
---
8
7
9
8
A Workflow step is self-contained, individually retriable component of a Workflow. Steps may emit (optional) state that allows a Workflow to persist and continue from that step, even if a Workflow fails due to a network or infrastructure issue. A Workflow is comprised of one or more steps.
10
9
10
+
This is a small guidebook on how to build more resilient and correct Workflows.
11
+
12
+
### Ensure API/Binding calls are idempotent
13
+
14
+
Because a step might be retried multiple times, your steps should (ideally) be idempotent. For context, idempotency is a logical property where the operation (in this case a step),
15
+
can be applied multiple times without changing the result beyond the intial application.
16
+
17
+
As an example, let's assume you have a Workflow that charges your customers and you really don't want to charge them twice by accident, before charging them, you should
Guaranteeing idempotency might be optional in your specific use-case and implementaion, although we recommend it to always try to guarantee it.
59
+
60
+
:::
61
+
11
62
### Make your steps granular
12
63
13
-
TODO - step is a transaction, should be a self-contained logic. If you have multiple API calls, seperate them into their own steps.
64
+
Steps should be as self-contained as possible, this allows your own logic to be more durable in case of failures in third-party APIs, network errors, and so on.
65
+
You can also think of it as a transaction, or a unit of work.
14
66
15
-
### Ensure API calls are idempotent
67
+
- ✅ Minimize the number of API/binding calls per step (unless you need multiple calls to prove idempotency).
Sometimes, our Engine will hibernate and lose all in-memory state - this will happen when engine detects that there's no pending work and can hibernate
111
+
until it needs to wake-up (because of a sleep, retry or event). This means that you can't do something like this:
112
+
113
+
```ts
114
+
function getRandomInt(min, max) {
115
+
const minCeiled =Math.ceil(min);
116
+
const maxFloored =Math.floor(max);
117
+
returnMath.floor(Math.random() * (maxFloored-minCeiled) +minCeiled); // The maximum is exclusive and the minimum is inclusive
Refer to the [events and parameters documentation](/workflows/build/events-and-parameters/) to understand how events are passed to Workflows.
121
+
119
122
### Worker binding
120
123
121
124
TODO - trigger from a Worker
122
125
126
+
```toml title="wrangler.toml"
127
+
[[workflows]]
128
+
# name of your workflow
129
+
name = "workflows-starter"
130
+
# binding name env.MYWORKFLOW
131
+
binding = "MY_WORKFLOW"
132
+
# this is class that extends the Workflow class in src/index.ts
133
+
class_name = "MyWorkflow"
134
+
# script_name is required during for the beta.
135
+
# Must match the "name" of your Worker at the top of wrangler.toml
136
+
script_name = "workflows-starter"
137
+
```
138
+
139
+
You can then invoke the methods on this binding directly from your Worker script. The `Workflow` type has methods for:
140
+
141
+
*`create(newId, params)` - creating (triggering) a new instance of the Workflow, with optional
142
+
*`get(id)`- retrieve a Workflow instance by ID
143
+
*`status()` - get the current status of a unique Workflow instance.
144
+
145
+
For example, the following Worker will fetch the status of an existing Workflow instance by ID (if supplied), else it will create a new Workflow instance and return its ID:
Copy file name to clipboardExpand all lines: src/content/docs/workflows/get-started/guide.mdx
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -164,7 +164,7 @@ binding = "MY_WORKFLOW"
164
164
# this is class that extends the Workflow class in src/index.ts
165
165
class_name = "MyWorkflow"
166
166
# script_name is required during for the beta.
167
-
# Must match the "name" of your Worker at the top of wrangler.toml
167
+
# Must match the "name" of your Worker at the top of wrangler.toml
168
168
script_name = "workflows-starter"
169
169
```
170
170
@@ -228,9 +228,15 @@ In a production application, you might choose to put authentication in front of
228
228
229
229
### Review your Workflow code
230
230
231
+
:::note
232
+
233
+
This is the full contents of the `src/index.ts` file pulled down when you used the `cloudflare/workflows-starter` template at the beginning of this guide.
234
+
235
+
:::
236
+
231
237
Before you deploy, you can review the full Workflows code and the `fetch` handler that will allow you to trigger your Workflow over HTTP:
0 commit comments