4.6.5. Retry Failed Steps

In this chapter, you’ll learn how to configure steps to allow retrial on failure.

Configure a Step’s Retrial#

By default, when an error occurs in a step, the step and the workflow fail, and the execution stops.

You can configure the step to retry on failure. The createStep function can accept a configuration object instead of the step’s name as a first parameter.

For example:

src/workflows/hello-world.ts
4} from "@medusajs/workflows-sdk"5
6const step1 = createStep(7  {8    name: "step-1",9    maxRetries: 2,10  },11  async () => {12    console.log("Executing step 1")13
14    throw new Error("Oops! Something happened.")15  }16)17
18type WorkflowOutput = {19  message: string20}21
22const myWorkflow = createWorkflow<23  {},24  WorkflowOutput25>("hello-world", function () {26  const str1 = step1()27
28  return {29    message: str1,30  }31})32
33export default myWorkflow

The step’s configuration object accepts a maxRetries property, which is a number indicating the number of times a step can be retried when it fails.

When you execute the above workflow, you’ll see the following result in the terminal:

1Executing step 12Executing step 13Executing step 14error:   Oops! Something happened.5Error: Oops! Something happened.

The first line indicates the first time the step was executed, and the next two lines indicate the times the step was retried. After that, the step and workflow fail.


Step Retry Intervals#

By default, a step is retried immediately after it fails.

To specify a wait time before a step is retried, pass a retryInterval property to the step's configuration object. Its value is a number of seconds to wait before retrying the step.

For example:

src/workflows/hello-world.ts
1const step1 = createStep(2  {3    name: "step-1",4    maxRetries: 2,5    retryInterval: 2, // 2 seconds6  },7  async () => {8    // ...9  }10)
Was this chapter helpful?