Skip to content

Commit d66be8f

Browse files
Merge branch 'production' into celso/workflows-examples
2 parents 79a7126 + ad43939 commit d66be8f

File tree

9 files changed

+43
-52
lines changed

9 files changed

+43
-52
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
/cloudflare-for-platforms/workers-for-platforms/ @irvinebroque @tanushree-sharma @angelampcosta @GregBrimble @cloudflare/pcx-technical-writing
132132
/src/content/docs/workers/observability/ @irvinebroque @mikenomitch @rohinlohe @cloudflare/pcx-technical-writing
133133
/src/content/docs/workers/static-assets @irvinebroque @tanushree-sharma @GregBrimble @WalshyDev @cloudflare/pcx-technical-writing
134+
/src/content/docs/workflows/ @elithrar @celso @sidharthachatterjee @cloudflare/pcx-technical-writing
134135

135136
# DDoS Protection
136137

src/content/docs/workflows/build/events-and-parameters.mdx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ Store state durably by returning it from your `step.do` callbacks.
2929

3030
```ts
3131
export default {
32-
async fetch(req: Request, env: Env) {
33-
let someEvent = { url: req.url, createdTimestamp: Date.now() }
34-
// Trigger our Workflow
35-
// Pass our event as the second parameter to the `create` method
36-
// on our Workflow binding.
37-
let instance = await env.MYWORKFLOW.create(await crypto.randomUUID(), someEvent);
38-
39-
return Response.json({
40-
id: instance.id,
41-
details: await instance.status(),
42-
});
43-
44-
return Response.json({ result });
45-
},
32+
async fetch(req: Request, env: Env) {
33+
let someEvent = { url: req.url, createdTimestamp: Date.now() }
34+
// Trigger our Workflow
35+
// Pass our event as the second parameter to the `create` method
36+
// on our Workflow binding.
37+
let instance = await env.MYWORKFLOW.create({
38+
id: await crypto.randomUUID(),
39+
params: someEvent
40+
});
41+
42+
return Response.json({
43+
id: instance.id,
44+
details: await instance.status(),
45+
});
46+
47+
return Response.json({ result });
48+
},
4649
};
4750
```
4851

src/content/docs/workflows/build/rules-of-workflows.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ check if they were already charged:
1919

2020
```ts
2121
export class MyWorkflow extends WorkflowEntrypoint {
22-
async run(event: WorkflowEvent, step: WorkflowStep) {
22+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
2323
const customer_id = 123456;
2424
// ✅ Good: Non-idempotent API/Binding calls are always done **after** checking if the operation is
2525
// still needed.
@@ -69,7 +69,7 @@ You can also think of it as a transaction, or a unit of work.
6969

7070
```ts
7171
export class MyWorkflow extends WorkflowEntrypoint {
72-
async run(event: WorkflowEvent, step: WorkflowStep) {
72+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
7373
// ✅ Good: Unrelated API/Binding calls are self-contained, so that in case one of them fails
7474
// it can retry them individually. It also has an extra advantage: you can control retry or
7575
// timeout policies for each granular step - you might not to want to overload http.cat in
@@ -94,7 +94,7 @@ Otherwise, your entire Workflow might not be as durable as you might think, and
9494

9595
```ts
9696
export class MyWorkflow extends WorkflowEntrypoint {
97-
async run(event: WorkflowEvent, step: WorkflowStep) {
97+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
9898
// 🔴 Bad: you're calling two seperate services from within the same step. This might cause
9999
// some extra calls to the first service in case the second one fails, and in some cases, makes
100100
// the step non-idempotent altogether
@@ -120,7 +120,7 @@ function getRandomInt(min, max) {
120120
}
121121

122122
export class MyWorkflow extends WorkflowEntrypoint {
123-
async run(event: WorkflowEvent, step: WorkflowStep) {
123+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
124124
// 🔴 Bad: `imageList` will be not persisted across engine's lifetimes. Which means that after hibernation,
125125
// `imageList` will be empty again, even though the following two steps have already ran.
126126
const imageList: string[] = [];
@@ -163,7 +163,7 @@ function getRandomInt(min, max) {
163163
}
164164

165165
export class MyWorkflow extends WorkflowEntrypoint {
166-
async run(event: WorkflowEvent, step: WorkflowStep) {
166+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
167167
// ✅ Good: imageList state is exclusively comprised of step returns - this means that in the event of
168168
// multiple engine lifetimes, imageList will be built accordingly
169169
const imageList: string[] = await Promise.all([
@@ -231,7 +231,7 @@ Dynamically naming a step will prevent it from being cached, and cause the step
231231
232232
```ts
233233
export class MyWorkflow extends WorkflowEntrypoint {
234-
async run(event: WorkflowEvent, step: WorkflowStep) {
234+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
235235
// 🔴 Bad: Dynamically naming the step prevents it from being cached
236236
// This will cause the step to be re-run if subsequent steps fail.
237237
await step.do(`step #1 running at: ${Date.now}`, async () => {

src/content/docs/workflows/build/sleeping-and-retrying.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ This can be useful when you detect a terminal (permanent) error from an upstream
9595
9696
```ts
9797
// Import the NonRetryableError definition
98-
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent, NonRetryableError } from 'cloudflare:workers';
98+
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers';
99+
import { NonRetryableError } from 'cloudflare:workflows';
99100
100101
// In your step code:
101102
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {

src/content/docs/workflows/build/trigger-workflows.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To bind to a Workflow from your Workers code, you need to define a [binding](/wo
3434
```toml title="wrangler.toml"
3535
name = "workflows-tutorial"
3636
main = "src/index.ts"
37-
compatibility_date = "2024-10-15"
37+
compatibility_date = "2024-10-22"
3838

3939
[[workflows]]
4040
# The name of the Workflow
@@ -80,7 +80,7 @@ export default {
8080
// Else, create a new instance of our Workflow, passing in any (optional) params
8181
// and return the ID.
8282
const newId = await crypto.randomUUID();
83-
let instance = await env.MY_WORKFLOW.create(newId, {});
83+
let instance = await env.MY_WORKFLOW.create({ id: newId });
8484
return Response.json({
8585
id: instance.id,
8686
details: await instance.status(),

src/content/docs/workflows/build/workers-api.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ For example, to bind to a Workflow called `workflows-starter` and to make it ava
111111
#:schema node_modules/wrangler/config-schema.json
112112
name = "workflows-starter"
113113
main = "src/index.ts"
114-
compatibility_date = "2024-10-16"
114+
compatibility_date = "2024-10-22"
115115

116116
[[workflows]]
117117
# name of your workflow

src/content/docs/workflows/examples/index.mdx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,3 @@ import { GlossaryTooltip, ListExamples } from "~/components"
1515
Explore the following <GlossaryTooltip term="code example">examples</GlossaryTooltip> for Workflows.
1616

1717
<ListExamples directory="workflows/examples/" />
18-
19-
---
20-
type: overview
21-
hideChildren: false
22-
pcx_content_type: navigation
23-
title: Examples
24-
sidebar:
25-
order: 6
26-
group:
27-
hideIndex: true
28-
29-
---
30-
31-
import { GlossaryTooltip, ListExamples } from "~/components"
32-
33-
Explore the following <GlossaryTooltip term="code example">examples</GlossaryTooltip> for Workflows.
34-
35-
<ListExamples directory="workflows/examples/" />

src/content/docs/workflows/get-started/cli-quick-start.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ You can run a Workflow via the `wrangler` CLI, via a Worker binding, or via the
161161

162162
```sh
163163
# Trigger a Workflow from the CLI, and pass (optional) parameters as an event to the Workflow.
164-
npx wrangler@latest workflows trigger workflows-tutorial --params={"hello":"world"}
164+
npx wrangler@latest workflows trigger workflows-tutorial --params={"email": "[email protected]", "metadata": {"id": "1"}}
165165
```
166166

167167
Refer to the [events and parameters documentation](/workflows/build/events-and-parameters/) to understand how events are passed to Workflows.

src/content/docs/workflows/get-started/guide.mdx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ Open the `src/index.ts` file in your text editor. This file contains the followi
4949
// Import the Workflow definition
5050
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers"
5151

52+
type Params = {}
53+
5254
// Create your own class that implements a Workflow
53-
export class MyWorkflow implements WorkflowEntrypoint {
55+
export class MyWorkflow extends WorkflowEntrypoint {
5456
// Define a run() method
55-
async run(event: WorkflowEvent, step: WorkflowStep) {
57+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
5658
// Define one or more steps that optionally return state.
5759
const state = await step.do('my first step', async () => {
5860
return {}
@@ -99,10 +101,12 @@ At its most basic, a step looks like this:
99101
// Import the Workflow definition
100102
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers"
101103

104+
type Params = {}
105+
102106
// Create your own class that implements a Workflow
103-
export class MyWorkflow implements WorkflowEntrypoint {
107+
export class MyWorkflow extends WorkflowEntrypoint {
104108
// Define a run() method
105-
async run(event: WorkflowEvent, step: WorkflowStep) {
109+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
106110
// Define one or more steps that optionally return state.
107111
let state = step.do("my first step", async () => {
108112

@@ -125,9 +129,9 @@ When trying to decide whether to break code up into more than one step, a good r
125129

126130
For example, each of the below tasks is ideally encapsulated in its own step, so that any failure — such as a file not existing, a third-party API being down or rate limited — does not cause your entire program to fail.
127131

128-
* Reading or writing files from R2
132+
* Reading or writing files from [R2](/r2/)
129133
* Running an AI task using [Workers AI](/workers-ai/)
130-
* Querying a D1 database or a database via [Hyperdrive](/hyperdrive/)
134+
* Querying a [D1 database](/d1/) or a database via [Hyperdrive](/hyperdrive/)
131135
* Calling a third-party API
132136

133137
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.
@@ -150,7 +154,7 @@ Open the `wrangler.toml` file at the root of your `workflows-starter` folder, wh
150154
#:schema node_modules/wrangler/config-schema.json
151155
name = "workflows-starter"
152156
main = "src/index.ts"
153-
compatibility_date = "2024-10-23"
157+
compatibility_date = "2024-10-22"
154158

155159
[[workflows]]
156160
# name of your workflow
@@ -159,7 +163,7 @@ name = "workflows-starter"
159163
binding = "MY_WORKFLOW"
160164
# this is class that extends the Workflow class in src/index.ts
161165
class_name = "MyWorkflow"
162-
# script_name is required during for the beta.
166+
# script_name is required during the beta.
163167
# Must match the "name" of your Worker at the top of wrangler.toml
164168
script_name = "workflows-starter"
165169
```
@@ -328,7 +332,7 @@ Your worker has access to the following bindings:
328332
- MY_WORKFLOW: MyWorkflow (defined in workflows-starter)
329333
Uploaded workflows-starter (2.53 sec)
330334
Deployed workflows-starter triggers (1.12 sec)
331-
https://workflows-starter.silverlock.workers.dev
335+
https://workflows-starter.YOUR_WORKERS_SUBDOMAIN.workers.dev
332336
workflow: workflows-starter
333337
```
334338

0 commit comments

Comments
 (0)