Skip to content

Commit 95b5be0

Browse files
committed
update guide
1 parent fdbc17b commit 95b5be0

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ Specifically:
8989

9090
## 2. Deploy a Workflow
9191

92-
Workflows are deployed via [`wrangler](/workers/wrangler/install-and-update/), which is installed when you first ran `npm create cloudflare`, above.
93-
94-
Deploying a Workflow is one
92+
Workflows are deployed via [`wrangler](/workers/wrangler/install-and-update/), which is installed when you first ran `npm create cloudflare`, above. Workflows are Workers scripts, and deployed the same way.
9593

9694
```sh
9795
npx wrangler@latest deploy

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ At it's most basic, a step looks like this:
9393

9494
```ts title="src/index.ts"
9595
// Import the Workflow definition
96-
import { Workflow, WorkflowEvent, WorkflowStep } from "cloudflare:workflows"
96+
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workflows"
9797

9898
// Create your own class that implements a Workflow
99-
export class MyFirstWorkflow implements Workflow {
99+
export class MyWorkflow implements WorkflowEntrypoint {
100100
// Define a run() method
101101
async run(event: WorkflowEvent, step: WorkflowStep) {
102102
// Define one or more steps that optionally return state.
@@ -149,7 +149,7 @@ compatibility_date = "2024-08-20"
149149

150150
[[workflows]]
151151
# The name of the Workflow
152-
name = "my-first-workflow"
152+
name = "workflows-tutorial"
153153
# The binding name, which must be a valid JavaScript variable name. This will
154154
# be how you call (run) your Workflow from your other Workers handlers or
155155
# scripts.
@@ -158,7 +158,7 @@ binding = "MY_WORKFLOW"
158158
# Must match the "name" of your Worker at the top of wrangler.toml
159159
script_name = "workflows-tutorial"
160160
# Must match the class defined in your code that extends the Workflow class
161-
class_name = "MyFirstWorkflow"
161+
class_name = "MyWorkflow"
162162
```
163163

164164
:::note
@@ -178,27 +178,46 @@ We have a very basic Workflow definition, but now need to provide a way to call
178178
3. A schedule via [Cron Trigger](/workers/configuration/cron-triggers/)
179179
4. Via the [Workflows REST API](TODO) or [wrangler CLI](/workers/wrangler/commands/#workflows)
180180

181-
Return to the `src/index.ts` file we created in the previous step and add a `fetch` handler that _binds_ to our Workflow.
181+
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.
182182

183183
```ts title="src/index.ts"
184+
// This can be in the same file as our Workflow definition
185+
184186
export default {
185187
async fetch(req: Request, env: Env) {
186-
let result = env.MY_WORKFLOW.run()
188+
//
189+
const instanceId = new URL(req.url).searchParams.get("instanceId")
190+
191+
// If an ?instanceId=<id> query parameter is provided, fetch the status
192+
// of an existing Workflow by its ID.
193+
if (instanceId) {
194+
let instance = await env.MYWORKFLOW.get(id);
195+
return Response.json({
196+
status: await instance.status(),
197+
});
198+
}
199+
200+
// Else, create a new instance of our Workflow, passing in any (optional) params
201+
// and return the ID.
202+
const newId = await crypto.randomUUID();
203+
let instance = await env.MYWORKFLOW.create(newId, {});
204+
return Response.json({
205+
id: instance.id,
206+
details: await instance.status(),
207+
});
208+
187209
return Response.json({ result });
188210
},
189211
};
190212
```
191213

192-
TODO:
214+
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.
193215

194-
* This is example exposes a HTTP endpoint and runs the Workflow without any parameters
195-
* In a production application, you might choose to put authentication in front of your endpoint so that only authorized users can run a Workflow
196-
* Alternatively, you could pass messages to a Workflow [from a Queue consumer](/queues/reference/how-queues-works/#consumers) in order to allow for long-running tasks.
216+
:::note
197217

218+
In a production application, you might choose to put authentication in front of your endpoint so that only authorized users can run a Workflow. Alternatively, you could pass messages to a Workflow [from a Queue consumer](/queues/reference/how-queues-works/#consumers) in order to allow for long-running tasks.
198219

199-
```ts
200-
let result = env.MY_WORKFLOW.run({params: {}})
201-
```
220+
:::
202221

203222
## 6. Deploy your Workflow
204223

0 commit comments

Comments
 (0)