Skip to content
Merged

fix code #17761

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 62 additions & 18 deletions src/content/docs/workflows/get-started/guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,67 @@ This will create a new folder called `workflows-starter`.
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:

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

type Params = {}
type Env = {
// Add your bindings here, e.g. Workers KV, D1, Workers AI, etc.
MY_WORKFLOW: Workflow;
};

// Create your own class that implements a Workflow
export class MyWorkflow extends WorkflowEntrypoint {
// Define a run() method
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
// Define one or more steps that optionally return state.
const state = await step.do('my first step', async () => {
return {}
// User-defined params passed to your workflow
type Params = {
email: string;
metadata: Record<string, string>;
};

export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
// Can access bindings on `this.env`
// Can access params on `event.params`

const files = await step.do('my first step', async () => {
// Fetch a list of files from $SOME_SERVICE
return {
inputParams: event,
files: [
'doc_7392_rev3.pdf',
'report_x29_final.pdf',
'memo_2024_05_12.pdf',
'file_089_update.pdf',
'proj_alpha_v2.pdf',
'data_analysis_q2.pdf',
'notes_meeting_52.pdf',
'summary_fy24_draft.pdf',
],
};
});

const apiResponse = await step.do('some other step', async () => {
let resp = await fetch('https://api.cloudflare.com/client/v4/ips');
return await resp.json<any>();
});

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

await step.do(
'make a call to write that could maybe, just might, fail',
// Define a retry strategy
{
retries: {
limit: 5,
delay: '5 second',
backoff: 'exponential',
},
timeout: '15 minutes',
},
async () => {
// Do stuff here, with access to 'state' from the previous step
// Do stuff here, with access to the state from our previous steps
if (Math.random() > 0.5) {
throw new Error('API call to $STORAGE_SYSTEM failed');
}
}
},
);
}
}
}
```

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

// Create your own class that implements a Workflow
export class MyWorkflow extends WorkflowEntrypoint {
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
// Define a run() method
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
// Define one or more steps that optionally return state.
Expand Down Expand Up @@ -188,13 +223,18 @@ We have a very basic Workflow definition, but now need to provide a way to call
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.

```ts title="src/index.ts"
// This can be in the same file as our Workflow definition
// This is in the same file as your Workflow definition

export default {
async fetch(req: Request, env: Env): Promise<Response> {
let id = new URL(req.url).searchParams.get('instanceId');
let url = new URL(req.url);

if (url.pathname.startsWith('/favicon')) {
return Response.json({}, { status: 404 });
}

// Get the status of an existing instance, if provided
let id = url.searchParams.get('instanceId');
if (id) {
let instance = await env.MY_WORKFLOW.get(id);
return Response.json({
Expand All @@ -210,7 +250,6 @@ export default {
});
},
};
;
```

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.
Expand Down Expand Up @@ -297,9 +336,14 @@ export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {

export default {
async fetch(req: Request, env: Env): Promise<Response> {
let id = new URL(req.url).searchParams.get('instanceId');
let url = new URL(req.url);

if (url.pathname.startsWith('/favicon')) {
return Response.json({}, { status: 404 });
}

// Get the status of an existing instance, if provided
let id = url.searchParams.get('instanceId');
if (id) {
let instance = await env.MY_WORKFLOW.get(id);
return Response.json({
Expand Down
Loading