Skip to content

Commit cf2a3c8

Browse files
committed
flesh out examples
1 parent 097e4ca commit cf2a3c8

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,39 @@ A running Workflow can wait for an event (or events) by calling `step.waitForEve
7474
```ts
7575
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
7676
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
77-
// TODO
78-
//
79-
// TODO
77+
// Other steps in your Workflow
78+
let event = await step.waitForEvent<IncomingStripeWebhook>("receive invoice paid webhook from Stripe", { type: "stripe-webhook", timeout: "1 hour" })
79+
// Rest of your Workflow
8080
}
8181
}
8282
```
8383

8484
</TypeScriptExample>
8585

86+
TODO
87+
8688
### Sending events to running workflows
8789

8890
TODO
8991

9092
<TypeScriptExample>
9193

9294
```ts
93-
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
94-
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
95-
// TODO
96-
//
97-
// TODO
98-
}
99-
}
95+
export default {
96+
async fetch(req: Request, env: Env) {
97+
const instanceId = new URL(req.url).searchParams.get("instanceId")
98+
const webhookPayload = await req.json<Payload>()
99+
100+
let instance = await env.MY_WORKFLOW.get(instanceId);
101+
// Send our event, with `type` matching the event type defined in
102+
// our step.waitForEvent call
103+
await instance.sendEvent({type: "stripe-webhook", payload: webhookPayload})
104+
105+
return Response.json({
106+
status: await instance.status(),
107+
});
108+
},
109+
};
100110
```
101111

102112
</TypeScriptExample>

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,21 @@ More information about the limits imposed on Workflow can be found in the [Workf
112112
* `name` - the name of the step.
113113
* `options` - an object with properties for `type`, which determines which event type this `waitForEvent` call will match on when calling `instance.sendEvent`, and an optional `timeout` property, which defines how long the `waitForEvent` call will block for before throwing a timeout exception. The default timeout is 24 hours.
114114

115-
Review the documentation on [events and parameters](/workflows/build/events-and-parameters/) to learn how to send events to a running Workflow instance.
115+
<TypeScriptExample>
116+
117+
```ts
118+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
119+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
120+
// Other steps in your Workflow
121+
let event = await step.waitForEvent<IncomingStripeWebhook>("receive invoice paid webhook from Stripe", { type: "stripe-webhook", timeout: "1 hour" })
122+
// Rest of your Workflow
123+
}
124+
}
125+
```
116126

127+
</TypeScriptExample>
128+
129+
Review the documentation on [events and parameters](/workflows/build/events-and-parameters/) to learn how to send events to a running Workflow instance.
117130

118131
## WorkflowStepConfig
119132

@@ -414,28 +427,39 @@ Terminate a Workflow instance.
414427

415428
### sendEvent
416429

417-
[Send an event]() to a running Workflow instance.
430+
[Send an event](/workflows/build/events-and-parameters/) to a running Workflow instance.
418431

419432
* <code>sendEvent(): Promise&lt;void&gt;</code>
420433

421-
* `options` - the event `type` and `payload` to send to the Workflow instance. The `type` must match the `type` in
434+
* `options` - the event `type` and `payload` to send to the Workflow instance. The `type` must match the `type` in the corresponding `waitForEvent` call in your Workflow.
422435

423436
Return `void` on success; throws an exception if the Workflow is not running or is an errored state.
424437

425438
<TypeScriptExample>
426439

427440
```ts
428-
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
429-
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
430-
// TODO
431-
//
432-
// TODO
433-
}
434-
}
441+
export default {
442+
async fetch(req: Request, env: Env) {
443+
const instanceId = new URL(req.url).searchParams.get("instanceId")
444+
const webhookPayload = await req.json<Payload>()
445+
446+
let instance = await env.MY_WORKFLOW.get(instanceId);
447+
// Send our event, with `type` matching the event type defined in
448+
// our step.waitForEvent call
449+
await instance.sendEvent({type: "stripe-webhook", payload: webhookPayload})
450+
451+
return Response.json({
452+
status: await instance.status(),
453+
});
454+
},
455+
};
435456
```
436457

437458
</TypeScriptExample>
438459

460+
You can call `sendEvent` multiple times, setting the value of the `type` property to match the specific `waitForEvent` calls in your Workflow.
461+
462+
This allows you to wait for multiple events at once, or use `Promise.race` to wait for multiple events and allow the first event to progress the Workflow.
439463

440464
### InstanceStatus
441465

0 commit comments

Comments
 (0)