Skip to content
Merged
Show file tree
Hide file tree
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
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
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
Loading