Skip to content

Commit 6e18a08

Browse files
committed
use GitHubCode component
1 parent 09196dc commit 6e18a08

File tree

2 files changed

+55
-344
lines changed

2 files changed

+55
-344
lines changed

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

Lines changed: 21 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar:
77

88
---
99

10-
import { Render, PackageManagers } from "~/components"
10+
import { GitHubCode, Render, PackageManagers } from "~/components"
1111

1212
:::note
1313

@@ -49,91 +49,12 @@ This will create a new folder called `workflows-tutorial`, which contains two fi
4949

5050
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:
5151

52-
```ts title="src/index.ts"
53-
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers';
54-
55-
type Env = {
56-
// Add your bindings here, e.g. Workers KV, D1, Workers AI, etc.
57-
MY_WORKFLOW: Workflow;
58-
};
59-
60-
// User-defined params passed to your workflow
61-
type Params = {
62-
email: string;
63-
metadata: Record<string, string>;
64-
};
65-
66-
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
67-
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
68-
// Can access bindings on `this.env`
69-
// Can access params on `event.params`
70-
71-
const files = await step.do('my first step', async () => {
72-
// Fetch a list of files from $SOME_SERVICE
73-
return {
74-
inputParams: event,
75-
files: [
76-
'doc_7392_rev3.pdf',
77-
'report_x29_final.pdf',
78-
'memo_2024_05_12.pdf',
79-
'file_089_update.pdf',
80-
'proj_alpha_v2.pdf',
81-
'data_analysis_q2.pdf',
82-
'notes_meeting_52.pdf',
83-
'summary_fy24_draft.pdf',
84-
],
85-
};
86-
});
87-
88-
const apiResponse = await step.do('some other step', async () => {
89-
let resp = await fetch('https://api.cloudflare.com/client/v4/ips');
90-
return await resp.json<any>();
91-
});
92-
93-
await step.sleep('wait on something', '1 minute');
94-
95-
await step.do(
96-
'make a call to write that could maybe, just might, fail',
97-
// Define a retry strategy
98-
{
99-
retries: {
100-
limit: 5,
101-
delay: '5 second',
102-
backoff: 'exponential',
103-
},
104-
timeout: '15 minutes',
105-
},
106-
async () => {
107-
// Do stuff here, with access to the state from our previous steps
108-
if (Math.random() > 0.5) {
109-
throw new Error('API call to $STORAGE_SYSTEM failed');
110-
}
111-
},
112-
);
113-
}
114-
}
115-
116-
export default {
117-
async fetch(req: Request, env: Env): Promise<Response> {
118-
let id = new URL(req.url).searchParams.get('instanceId');
119-
120-
// Get the status of an existing instance, if provided
121-
if (id) {
122-
let instance = await env.MY_WORKFLOW.get(id);
123-
return Response.json({
124-
status: await instance.status(),
125-
});
126-
}
127-
128-
// Spawn a new instance and return the ID and status
129-
let instance = await env.MY_WORKFLOW.create();
130-
return Response.json({
131-
id: instance.id,
132-
details: await instance.status(),
133-
});
134-
},
135-
};
136-
```
52+
<GitHubCode
53+
repo="cloudflare/workflows-starer"
54+
file="src/index.ts"
55+
commit="5b9c174fd1df3718e02544e19b8a83f3b93741ef"
56+
lang="ts"
57+
/>
13758

13859
Specifically, the code above:
13960

@@ -172,15 +93,13 @@ You can [bind to a Workflow](/workers/runtime-apis/bindings/#what-is-a-binding)
17293

17394
To bind a Workflow to a Worker, you need to define a `[[workflows]]` binding in your `wrangler.toml` configuration:
17495

175-
```toml title="wrangler.toml"
176-
[[workflows]]
177-
# name of your workflow
178-
name = "workflows-starter"
179-
# binding name env.MY_WORKFLOW
180-
binding = "MY_WORKFLOW"
181-
# this is class that extends the Workflow class in src/index.ts
182-
class_name = "MyWorkflow"
183-
```
96+
<GitHubCode
97+
repo="cloudflare/workflows-starer"
98+
file="wrangler.toml"
99+
commit="5b9c174fd1df3718e02544e19b8a83f3b93741ef"
100+
tag="workflows-binding-wrangler.toml"
101+
lang="toml"
102+
/>
184103

185104
You can then invoke the methods on this binding directly from your Worker script's `env` parameter. The `Workflow` type has methods for:
186105

@@ -190,36 +109,13 @@ You can then invoke the methods on this binding directly from your Worker script
190109

191110
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:
192111

193-
```ts title="src/index.ts"
194-
// Import the Workflow definition
195-
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent} from 'cloudflare:workers';
196-
197-
interface Env {
198-
// Matches the binding definition in your wrangler.toml
199-
MY_WORKFLOW: Workflow;
200-
}
201-
202-
export default {
203-
async fetch(req: Request, env: Env): Promise<Response> {
204-
let id = new URL(req.url).searchParams.get('instanceId');
205-
206-
// Get the status of an existing instance, if provided
207-
if (id) {
208-
let instance = await env.MY_WORKFLOW.get(id);
209-
return Response.json({
210-
status: await instance.status(),
211-
});
212-
}
213-
214-
// Spawn a new instance and return the ID and status
215-
let instance = await env.MY_WORKFLOW.create();
216-
return Response.json({
217-
id: instance.id,
218-
details: await instance.status(),
219-
});
220-
},
221-
};
222-
```
112+
<GitHubCode
113+
repo="cloudflare/workflows-starer"
114+
file="src/index.ts"
115+
commit="5b9c174fd1df3718e02544e19b8a83f3b93741ef"
116+
tag="workflows-fetch-handler"
117+
lang="ts"
118+
/>
223119

224120
Refer to the [triggering Workflows](/workflows/build/trigger-workflows/) documentation for how to trigger a Workflow from other Workers' handler functions.
225121

0 commit comments

Comments
 (0)