Skip to content

Commit 7f20b89

Browse files
elithrarharshil1712
authored andcommitted
fix code (#17761)
1 parent 3f89e17 commit 7f20b89

File tree

1 file changed

+62
-18
lines changed
  • src/content/docs/workflows/get-started

1 file changed

+62
-18
lines changed

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

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,67 @@ This will create a new folder called `workflows-starter`.
4646
Open the `src/index.ts` file in your text editor. This file contains the following code, which is the most basic instance of a Workflow definition:
4747

4848
```ts title="src/index.ts"
49-
// Import the Workflow definition
50-
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers"
49+
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers';
5150

52-
type Params = {}
51+
type Env = {
52+
// Add your bindings here, e.g. Workers KV, D1, Workers AI, etc.
53+
MY_WORKFLOW: Workflow;
54+
};
5355

54-
// Create your own class that implements a Workflow
55-
export class MyWorkflow extends WorkflowEntrypoint {
56-
// Define a run() method
57-
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
58-
// Define one or more steps that optionally return state.
59-
const state = await step.do('my first step', async () => {
60-
return {}
56+
// User-defined params passed to your workflow
57+
type Params = {
58+
email: string;
59+
metadata: Record<string, string>;
60+
};
61+
62+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
63+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
64+
// Can access bindings on `this.env`
65+
// Can access params on `event.params`
66+
67+
const files = await step.do('my first step', async () => {
68+
// Fetch a list of files from $SOME_SERVICE
69+
return {
70+
inputParams: event,
71+
files: [
72+
'doc_7392_rev3.pdf',
73+
'report_x29_final.pdf',
74+
'memo_2024_05_12.pdf',
75+
'file_089_update.pdf',
76+
'proj_alpha_v2.pdf',
77+
'data_analysis_q2.pdf',
78+
'notes_meeting_52.pdf',
79+
'summary_fy24_draft.pdf',
80+
],
81+
};
82+
});
83+
84+
const apiResponse = await step.do('some other step', async () => {
85+
let resp = await fetch('https://api.cloudflare.com/client/v4/ips');
86+
return await resp.json<any>();
6187
});
6288

6389
await step.sleep('wait on something', '1 minute');
6490

6591
await step.do(
6692
'make a call to write that could maybe, just might, fail',
93+
// Define a retry strategy
94+
{
95+
retries: {
96+
limit: 5,
97+
delay: '5 second',
98+
backoff: 'exponential',
99+
},
100+
timeout: '15 minutes',
101+
},
67102
async () => {
68-
// Do stuff here, with access to 'state' from the previous step
103+
// Do stuff here, with access to the state from our previous steps
69104
if (Math.random() > 0.5) {
70105
throw new Error('API call to $STORAGE_SYSTEM failed');
71106
}
72-
}
107+
},
73108
);
74-
}
109+
}
75110
}
76111
```
77112

@@ -104,7 +139,7 @@ import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:work
104139
type Params = {}
105140

106141
// Create your own class that implements a Workflow
107-
export class MyWorkflow extends WorkflowEntrypoint {
142+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
108143
// Define a run() method
109144
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
110145
// Define one or more steps that optionally return state.
@@ -188,13 +223,18 @@ We have a very basic Workflow definition, but now need to provide a way to call
188223
Return to the `src/index.ts` file we created in the previous step and add a `fetch` handler that _binds_ to our Workflow. This binding allows us to create new Workflow instances, fetch the status of an existing Workflow, pause and/or terminate a Workflow.
189224

190225
```ts title="src/index.ts"
191-
// This can be in the same file as our Workflow definition
226+
// This is in the same file as your Workflow definition
192227

193228
export default {
194229
async fetch(req: Request, env: Env): Promise<Response> {
195-
let id = new URL(req.url).searchParams.get('instanceId');
230+
let url = new URL(req.url);
231+
232+
if (url.pathname.startsWith('/favicon')) {
233+
return Response.json({}, { status: 404 });
234+
}
196235

197236
// Get the status of an existing instance, if provided
237+
let id = url.searchParams.get('instanceId');
198238
if (id) {
199239
let instance = await env.MY_WORKFLOW.get(id);
200240
return Response.json({
@@ -210,7 +250,6 @@ export default {
210250
});
211251
},
212252
};
213-
;
214253
```
215254

216255
The code here exposes a HTTP endpoint that generates a random ID and runs the Workflow, returning the ID and the Workflow status. It also accepts an optional `instanceId` query parameter that retrieves the status of a Workflow instance by its ID.
@@ -297,9 +336,14 @@ export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
297336

298337
export default {
299338
async fetch(req: Request, env: Env): Promise<Response> {
300-
let id = new URL(req.url).searchParams.get('instanceId');
339+
let url = new URL(req.url);
340+
341+
if (url.pathname.startsWith('/favicon')) {
342+
return Response.json({}, { status: 404 });
343+
}
301344

302345
// Get the status of an existing instance, if provided
346+
let id = url.searchParams.get('instanceId');
303347
if (id) {
304348
let instance = await env.MY_WORKFLOW.get(id);
305349
return Response.json({

0 commit comments

Comments
 (0)