Skip to content

Commit e858bb1

Browse files
committed
naming steps
1 parent 925dd82 commit e858bb1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/content/docs/workflows/build/rules-of-workflows.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,26 @@ export class MyWorkflow extends WorkflowEntrypoint {
224224
// Will always be the same if this step is retried
225225
})
226226
```
227+
228+
### Name steps deterministically
229+
230+
Dynamically naming a step will prevent it from being cached, and cause the step to be re-run unnecessarily. Step names act as the "cache key" in your Workflow.
231+
232+
```ts
233+
export class MyWorkflow extends WorkflowEntrypoint {
234+
async run(event: WorkflowEvent, step: WorkflowStep) {
235+
// 🔴 Bad: Dynamically naming the step prevents it from being cached
236+
// This will cause the step to be re-run if subsequent steps fail.
237+
await step.do(`step #1 running at: ${Date.now}`, async () => {
238+
let userData = await env.KV.get(event.user)
239+
event.data = userData
240+
})
241+
242+
// ✅ Good: give steps a deterministic name.
243+
// Return dynamic values in your state, or log them instead.
244+
let state = await step.do("fetch user data from KV", async () => {
245+
let userData = await env.KV.get(event.user)
246+
console.log(`fetched at ${Date.now})
247+
return userData
248+
})
249+
```

0 commit comments

Comments
 (0)