Skip to content

Commit

Permalink
workflows: fix event.payload references
Browse files Browse the repository at this point in the history
  • Loading branch information
elithrar committed Jan 14, 2025
1 parent 5941a14 commit ac099c7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/content/docs/workflows/build/events-and-parameters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type WorkflowEvent<T> = {
payload: T;
// The timestamp that the Workflow was triggered
timestamp: Date;
// ID of the associated instance
// ID of the current Workflow instance
instanceId: string;
};
```
Expand Down
15 changes: 8 additions & 7 deletions src/content/docs/workflows/build/rules-of-workflows.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ interface MyEvent {
export class MyWorkflow extends WorkflowEntrypoint {
async run(event: WorkflowEvent<MyEvent>, step: WorkflowStep) {
// 🔴 Bad: Mutating the event
// This will not be persisted across steps and `event.data` will
// This will not be persisted across steps and `event.payload` will
// take on its original value.
await step.do("bad step that mutates the incoming event", async () => {
let userData = await env.KV.get(event.user)
event.data = userData
let userData = await env.KV.get(event.payload.user)
event.payload = userData
})

// ✅ Good: persist data by returning it as state from your step
// Use that state in subsequent steps
let userData = await step.do("good step that returns state", async () => {
return await env.KV.get(event.user)
return await env.KV.get(event.payload.user)
})

let someOtherData = await step.do("following step that uses that state", async () => {
Expand All @@ -252,14 +252,15 @@ export class MyWorkflow extends WorkflowEntrypoint {
// 🔴 Bad: Naming the step non-deterministically prevents it from being cached
// This will cause the step to be re-run if subsequent steps fail.
await step.do(`step #1 running at: ${Date.now()}`, async () => {
let userData = await env.KV.get(event.user)
event.data = userData
let userData = await env.KV.get(event.payload.user)
// Do not mutate event.payload
event.payload = userData
})

// ✅ Good: give steps a deterministic name.
// Return dynamic values in your state, or log them instead.
let state = await step.do("fetch user data from KV", async () => {
let userData = await env.KV.get(event.user)
let userData = await env.KV.get(event.payload.user)
console.log(`fetched at ${Date.now}`)
return userData
})
Expand Down
4 changes: 2 additions & 2 deletions src/content/docs/workflows/build/sleeping-and-retrying.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ import { NonRetryableError } from 'cloudflare:workflows';
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
await step.do("some step", async () => {
if !(event.data) {
throw new NonRetryableError("event.data did not contain the expected payload")
if !(event.payload.data) {
throw new NonRetryableError("event.payload.data did not contain the expected payload")
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions src/content/docs/workflows/get-started/cli-quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ type Params = {
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`
// Can access params on `event.payload`

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',
Expand Down
4 changes: 1 addition & 3 deletions src/content/docs/workflows/get-started/guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class MyWorkflow extends WorkflowEntrypoint<Env, 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',
Expand Down Expand Up @@ -290,12 +289,11 @@ type Params = {
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`
// Can access params on `event.payload`

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',
Expand Down

0 comments on commit ac099c7

Please sign in to comment.