|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Retry Failed Steps`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you’ll learn how to configure steps to allow retry on failure. |
|
8
|
|
|
9
|
## What is a Step Retry? |
|
10
|
|
|
11
|
A step retry is a mechanism that allows a step to be retried automatically when it fails. This is useful for handling transient errors, such as network issues or temporary unavailability of a service. |
|
12
|
|
|
13
|
By default, when a step fails, the workflow execution stops, and the workflow is marked as failed. However, you can configure a step to retry on failure. |
|
14
|
|
|
15
|
When a step fails, you can configure the workflow engine to automatically retry the step a specified number of times before marking the workflow as failed. This can help improve the reliability and resilience of your workflows. |
|
16
|
|
|
17
|
You can also configure the interval between retries, awllowing you to wait for a certain period before attempting the step again. This is useful when the failure is due to a temporary issue that may resolve itself after some time. |
|
18
|
|
|
19
|
For example, if a step captures a payment, you may want to retry it daily until the payment is successful or the maximum number of retries is reached. |
|
20
|
|
|
21
|
--- |
|
22
|
|
|
23
|
## Configure a Step’s Retry |
|
24
|
|
|
25
|
By default, when an error occurs in a step, the step and the workflow fail, and the execution stops. |
|
26
|
|
|
27
|
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. |
|
28
|
|
|
29
|
For example: |
|
30
|
|
|
31
|
```ts title="src/workflows/hello-world.ts" highlights={[["11"]]} collapsibleLines="1-7" expandButtonLabel="Show Imports" |
|
32
|
import { |
|
33
|
createStep, |
|
34
|
createWorkflow, |
|
35
|
WorkflowResponse, |
|
36
|
} from "@medusajs/framework/workflows-sdk" |
|
37
|
import { MedusaError } from "@medusajs/framework/utils" |
|
38
|
|
|
39
|
const step1 = createStep( |
|
40
|
{ |
|
41
|
name: "step-1", |
|
42
|
maxRetries: 2, |
|
43
|
}, |
|
44
|
async () => { |
|
45
|
console.log("Executing step 1") |
|
46
|
|
|
47
|
throw new MedusaError( |
|
48
|
MedusaError.Types.UNEXPECTED_STATE, |
|
49
|
"Oops! Something happened." |
|
50
|
) |
|
51
|
} |
|
52
|
) |
|
53
|
|
|
54
|
const myWorkflow = createWorkflow( |
|
55
|
"hello-world", |
|
56
|
function () { |
|
57
|
const str1 = step1() |
|
58
|
|
|
59
|
return new WorkflowResponse({ |
|
60
|
message: str1, |
|
61
|
}) |
|
62
|
}) |
|
63
|
|
|
64
|
export default myWorkflow |
|
65
|
``` |
|
66
|
|
|
67
|
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. |
|
68
|
|
|
69
|
When you execute the above workflow, you’ll see the following result in the terminal: |
|
70
|
|
|
71
|
```bash |
|
72
|
Executing step 1 |
|
73
|
Executing step 1 |
|
74
|
Executing step 1 |
|
75
|
error: Oops! Something happened. |
|
76
|
Error: Oops! Something happened. |
|
77
|
``` |
|
78
|
|
|
79
|
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. |
|
80
|
|
|
81
|
### Disable Automatic Retries |
|
82
|
|
|
83
|
By default, a step configured with the `maxRetries` property will be retried automatically when it fails. |
|
84
|
|
|
85
|
You can disable automatic retries by setting the `autoRetry` property to `false` in the step's configuration object. Then, when the step fails, its status will be set to temporary failure, and you'll need to manually retry it using the [Workflow Engine Module Service](!resources!/infrastructure-modules/workflow-engine). |
|
86
|
|
|
87
|
<Note> |
|
88
|
|
|
89
|
The `autoRetry` property is available since [Medusa v2.10.2](https://github.com/medusajs/medusa/releases/tag/v2.10.2). |
|
90
|
|
|
91
|
</Note> |
|
92
|
|
|
93
|
For example, to disable automatic retries: |
|
94
|
|
|
95
|
```ts title="src/workflows/hello-world.ts" highlights={[["5"]]} |
|
96
|
const step1 = createStep( |
|
97
|
{ |
|
98
|
name: "step-1", |
|
99
|
maxRetries: 2, |
|
100
|
autoRetry: false, // Disable automatic retries |
|
101
|
}, |
|
102
|
async () => { |
|
103
|
console.log("Executing step 1") |
|
104
|
|
|
105
|
throw new MedusaError( |
|
106
|
MedusaError.Types.UNEXPECTED_STATE, |
|
107
|
"Oops! Something happened." |
|
108
|
) |
|
109
|
} |
|
110
|
) |
|
111
|
``` |
|
112
|
|
|
113
|
This step will not be retried automatically when it fails. Instead, you'll need to manually retry it, as explained in the [Manually Retry a Step](#manually-retry-a-step) section. |
|
114
|
|
|
115
|
--- |
|
116
|
|
|
117
|
## Step Retry Intervals |
|
118
|
|
|
119
|
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. |
|
120
|
|
|
121
|
For example: |
|
122
|
|
|
123
|
```ts title="src/workflows/hello-world.ts" highlights={[["5"]]} |
|
124
|
const step1 = createStep( |
|
125
|
{ |
|
126
|
name: "step-1", |
|
127
|
maxRetries: 2, |
|
128
|
retryInterval: 2, // 2 seconds |
|
129
|
}, |
|
130
|
async () => { |
|
131
|
console.log("Executing step 1") |
|
132
|
|
|
133
|
throw new MedusaError( |
|
134
|
MedusaError.Types.UNEXPECTED_STATE, |
|
135
|
"Oops! Something happened." |
|
136
|
) |
|
137
|
} |
|
138
|
) |
|
139
|
``` |
|
140
|
|
|
141
|
In this example, if the step fails, it will be retried after two seconds. |
|
142
|
|
|
143
|
### Maximum Retry Interval |
|
144
|
|
|
145
|
The `retryInterval` property's maximum value is [Number.MAX_SAFE_INTEGER](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER). So, you can set a very long wait time before the step is retried, allowing you to retry steps after a long period. |
|
146
|
|
|
147
|
For example, to retry a step after a day: |
|
148
|
|
|
149
|
```ts title="src/workflows/hello-world.ts" highlights={[["5"]]} |
|
150
|
const step1 = createStep( |
|
151
|
{ |
|
152
|
name: "step-1", |
|
153
|
maxRetries: 2, |
|
154
|
retryInterval: 86400, // 1 day |
|
155
|
}, |
|
156
|
async () => { |
|
157
|
console.log("Executing step 1") |
|
158
|
|
|
159
|
throw new MedusaError( |
|
160
|
MedusaError.Types.UNEXPECTED_STATE, |
|
161
|
"Oops! Something happened." |
|
162
|
) |
|
163
|
} |
|
164
|
) |
|
165
|
``` |
|
166
|
|
|
167
|
In this example, if the step fails, it will be retried after `86400` seconds (one day). |
|
168
|
|
|
169
|
### Interval Changes Workflow to Long-Running |
|
170
|
|
|
171
|
By setting `retryInterval` on a step, a workflow that uses that step becomes a [long-running workflow](../long-running-workflow/page.mdx) that runs asynchronously in the background. This is useful when creating workflows that may fail and should run for a long time until they succeed, such as waiting for a payment to be captured or a shipment to be delivered. |
|
172
|
|
|
173
|
However, since the long-running workflow runs in the background, you won't receive its result or errors immediately when you execute the workflow. |
|
174
|
|
|
175
|
Instead, you must subscribe to the workflow's execution using the Workflow Engine Module's service. Learn more about this in the [Long-Running Workflows](../long-running-workflow/page.mdx#access-long-running-workflow-status-and-result) chapter. |
|
176
|
|
|
177
|
--- |
|
178
|
|
|
179
|
## Manually Retry a Step |
|
180
|
|
|
181
|
In some cases, you may need to manually retry a step. For example: |
|
182
|
|
|
183
|
- If a step's `autoRetry` property is set to `false`. |
|
184
|
- If the machine running the Medusa application in development or [worker mode](../../../configurations/medusa-config/page.mdx#workermode) dies or shuts down. |
|
185
|
- If the step takes longer than expected to complete. |
|
186
|
|
|
187
|
To retry a step manually, resolve the Workflow Engine Module's service from the Medusa container and call its `retryStep` method. |
|
188
|
|
|
189
|
<Note> |
|
190
|
|
|
191
|
The `retryStep` method is available since [Medusa v2.10.2](https://github.com/medusajs/medusa/releases/tag/v2.10.2). |
|
192
|
|
|
193
|
</Note> |
|
194
|
|
|
195
|
For example, to do it in an API route: |
|
196
|
|
|
197
|
export const retryHighlights = [ |
|
198
|
["11", "workflowEngine", "Resolve the Workflow Engine Module's service from the Medusa container."], |
|
199
|
["15", "retryStep", "Retry the step."], |
|
200
|
] |
|
201
|
|
|
202
|
```ts title="src/api/retry-step/route.ts" highlights={retryHighlights} |
|
203
|
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" |
|
204
|
import { |
|
205
|
TransactionHandlerType, |
|
206
|
Modules, |
|
207
|
} from "@medusajs/framework/utils" |
|
208
|
|
|
209
|
export async function GET( |
|
210
|
req: MedusaRequest, |
|
211
|
res: MedusaResponse |
|
212
|
) { |
|
213
|
const workflowEngine = req.scope.resolve( |
|
214
|
Modules.WORKFLOW_ENGINE |
|
215
|
) |
|
216
|
|
|
217
|
workflowEngine.retryStep({ |
|
218
|
idempotencyKey: { |
|
219
|
action: TransactionHandlerType.INVOKE, |
|
220
|
transactionId: req.validatedQuery.transaction_id as string, |
|
221
|
stepId: "step-1", |
|
222
|
workflowId: "hello-world", |
|
223
|
}, |
|
224
|
}) |
|
225
|
|
|
226
|
res.json({ message: "Step retry initiated" }) |
|
227
|
} |
|
228
|
``` |
|
229
|
|
|
230
|
When you send a request to the API route, the workflow execution will resume, retrying the specified step. |
|
231
|
|
|
232
|
Learn more about the `retryStep` method in the [Workflow Engine Module Service reference](!resources!/infrastructure-modules/workflow-engine/how-to-use#retryStep). |