Raw
1 ---
2 sidebar_label: "Use Workflow Engine Module"
3 tags:
4 - workflow engine
5 - server
6 - how to
7 ---
8
9 import { TypeList } from "docs-ui"
10
11 export const metadata = {
12 title: `How to Use the Workflow Engine Module`,
13 }
14
15 # {metadata.title}
16
17 In this document, you’ll learn about the different methods in the Workflow Engine Module's service and how to use them.
18
19 ---
20
21 ## Resolve Workflow Engine Module's Service
22
23 In your workflow's step, you can resolve the Workflow Engine Module's service from the Medusa container:
24
25 ```ts
26 import { Modules } from "@medusajs/framework/utils"
27 import { createStep } from "@medusajs/framework/workflows-sdk"
28
29 const step1 = createStep(
30 "step-1",
31 async ({}, { container }) => {
32 const workflowEngineModuleService = container.resolve(
33 Modules.WORKFLOW_ENGINE
34 )
35
36 // TODO use workflowEngineModuleService
37 }
38 )
39 ```
40
41 This will resolve the service of the configured Workflow Engine Module, which is the [In-Memory Workflow Engine Module](../in-memory/page.mdx) by default.
42
43 You can then use the Workflow Engine Module's service's methods in the step. The rest of this guide details these methods.
44
45 ---
46
47 ## retryStep
48
49 This method retries a step that has temporary failed, such as a step that has `autoRetry` set to `false` or when the machine running the Medusa application shuts down. Learn more about it in the [Retry Failed Steps](!docs!/learn/fundamentals/workflows/retry-failed-steps) guide.
50
51 <Note>
52
53 This method is available since [Medusa v2.10.2](https://github.com/medusajs/medusa/releases/tag/v2.10.2).
54
55 </Note>
56
57 ### Example
58
59 ```ts
60 // other imports...
61 import {
62 TransactionHandlerType,
63 } from "@medusajs/framework/utils"
64
65 workflowEngine.retryStep({
66 idempotencyKey: {
67 action: TransactionHandlerType.INVOKE,
68 transactionId,
69 stepId: "step-1",
70 workflowId: "hello-world",
71 },
72 })
73 ```
74
75 ### Parameters
76
77 <TypeList types={[
78 {
79 "name": "idempotencyKey",
80 "type": "`object`",
81 "description": "The details of the step to retry.",
82 "optional": false,
83 "defaultValue": "",
84 "expandable": false,
85 "children": [
86 {
87 "name": "action",
88 "type": "`invoke` | `compensate`",
89 "description": "If the step's compensation function is running, use `compensate`. Otherwise, use `invoke`.",
90 "optional": false,
91 "defaultValue": "",
92 "expandable": false,
93 "children": []
94 },
95 {
96 "name": "transactionId",
97 "type": "`string`",
98 "description": "The ID of the workflow execution's transaction.",
99 "optional": false,
100 "defaultValue": "",
101 "expandable": false,
102 "children": []
103 },
104 {
105 "name": "stepId",
106 "type": "`string`",
107 "description": "The ID of the step to change its status. This is the first parameter passed to `createStep` when creating the step.",
108 "optional": false,
109 "defaultValue": "",
110 "expandable": false,
111 "children": []
112 },
113 {
114 "name": "workflowId",
115 "type": "`string`",
116 "description": "The ID of the workflow. This is the first parameter passed to `createWorkflow` when creating the workflow.",
117 "optional": false,
118 "defaultValue": "",
119 "expandable": false,
120 "children": []
121 }
122 ]
123 },
124 {
125 "name": "options",
126 "type": "`object`",
127 "description": "Options to pass to the step.",
128 "optional": true,
129 "defaultValue": "",
130 "expandable": false,
131 "children": [
132 {
133 "name": "container",
134 "type": "`Container`",
135 "description": "An instance of the Medusa container.",
136 "optional": false,
137 "defaultValue": "",
138 "expandable": false,
139 "children": []
140 }
141 ]
142 }
143 ]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="retryStep"/>
144
145 ---
146
147 ## setStepSuccess
148
149 This method sets an async step in a currently-executing [long-running workflow](!docs!/learn/fundamentals/workflows/long-running-workflow) as successful. The workflow will then continue to the next step.
150
151 ### Example
152
153 ```ts
154 // other imports...
155 import {
156 TransactionHandlerType,
157 } from "@medusajs/framework/utils"
158
159 await workflowEngineModuleService.setStepSuccess({
160 idempotencyKey: {
161 action: TransactionHandlerType.INVOKE,
162 transactionId,
163 stepId: "step-2",
164 workflowId: "hello-world",
165 },
166 stepResponse: new StepResponse("Done!"),
167 })
168 ```
169
170 ### Parameters
171
172 <TypeList types={[
173 {
174 "name": "idempotencyKey",
175 "type": "`object`",
176 "description": "The details of the step to set as successful.",
177 "optional": false,
178 "defaultValue": "",
179 "expandable": false,
180 "children": [
181 {
182 "name": "action",
183 "type": "`invoke` | `compensate`",
184 "description": "If the step's compensation function is running, use `compensate`. Otherwise, use `invoke`.",
185 "optional": false,
186 "defaultValue": "",
187 "expandable": false,
188 "children": []
189 },
190 {
191 "name": "transactionId",
192 "type": "`string`",
193 "description": "The ID of the workflow execution's transaction.",
194 "optional": false,
195 "defaultValue": "",
196 "expandable": false,
197 "children": []
198 },
199 {
200 "name": "stepId",
201 "type": "`string`",
202 "description": "The ID of the step to change its status. This is the first parameter passed to `createStep` when creating the step.",
203 "optional": false,
204 "defaultValue": "",
205 "expandable": false,
206 "children": []
207 },
208 {
209 "name": "workflowId",
210 "type": "`string`",
211 "description": "The ID of the workflow. This is the first parameter passed to `createWorkflow` when creating the workflow.",
212 "optional": false,
213 "defaultValue": "",
214 "expandable": false,
215 "children": []
216 }
217 ]
218 },
219 {
220 "name": "stepResponse",
221 "type": "`StepResponse`",
222 "description": "Set the response of the step. This is similar to the response you return in a step's definition, but since the async step doesn't have a response, you set its response when changing its status.",
223 "optional": false,
224 "defaultValue": "",
225 "expandable": false,
226 "children": []
227 },
228 {
229 "name": "options",
230 "type": "`object`",
231 "description": "Options to pass to the step.",
232 "optional": true,
233 "defaultValue": "",
234 "expandable": false,
235 "children": [
236 {
237 "name": "container",
238 "type": "`Container`",
239 "description": "An instance of the Medusa container.",
240 "optional": false,
241 "defaultValue": "",
242 "expandable": false,
243 "children": []
244 }
245 ]
246 }
247 ]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="setStepSuccess"/>
248
249 ---
250
251 ## setStepFailure
252
253 This method sets an async step in a currently-executing [long-running workflow](!docs!/learn/fundamentals/workflows/long-running-workflow) as failed. The workflow will then stop executing and the compensation functions of the workflow's steps will be executed.
254
255 ### Example
256
257 ```ts
258 // other imports...
259 import {
260 TransactionHandlerType,
261 } from "@medusajs/framework/utils"
262
263 await workflowEngineModuleService.setStepFailure({
264 idempotencyKey: {
265 action: TransactionHandlerType.INVOKE,
266 transactionId,
267 stepId: "step-2",
268 workflowId: "hello-world",
269 },
270 stepResponse: new StepResponse("Failed!"),
271 })
272 ```
273
274 ### Parameters
275
276 <TypeList types={[
277 {
278 "name": "idempotencyKey",
279 "type": "`object`",
280 "description": "The details of the step to set as failed.",
281 "optional": false,
282 "defaultValue": "",
283 "expandable": false,
284 "children": [
285 {
286 "name": "action",
287 "type": "`invoke` | `compensate`",
288 "description": "If the step's compensation function is running, use `compensate`. Otherwise, use `invoke`.",
289 "optional": false,
290 "defaultValue": "",
291 "expandable": false,
292 "children": []
293 },
294 {
295 "name": "transactionId",
296 "type": "`string`",
297 "description": "The ID of the workflow execution's transaction.",
298 "optional": false,
299 "defaultValue": "",
300 "expandable": false,
301 "children": []
302 },
303 {
304 "name": "stepId",
305 "type": "`string`",
306 "description": "The ID of the step to change its status. This is the first parameter passed to `createStep` when creating the step.",
307 "optional": false,
308 "defaultValue": "",
309 "expandable": false,
310 "children": []
311 },
312 {
313 "name": "workflowId",
314 "type": "`string`",
315 "description": "The ID of the workflow. This is the first parameter passed to `createWorkflow` when creating the workflow.",
316 "optional": false,
317 "defaultValue": "",
318 "expandable": false,
319 "children": []
320 }
321 ]
322 },
323 {
324 "name": "stepResponse",
325 "type": "`StepResponse`",
326 "description": "Set the response of the step. This is similar to the response you return in a step's definition, but since the async step doesn't have a response, you set its response when changing its status.",
327 "optional": false,
328 "defaultValue": "",
329 "expandable": false,
330 "children": []
331 },
332 {
333 "name": "options",
334 "type": "`object`",
335 "description": "Options to pass to the step.",
336 "optional": true,
337 "defaultValue": "",
338 "expandable": false,
339 "children": [
340 {
341 "name": "container",
342 "type": "`Container`",
343 "description": "An instance of the Medusa container.",
344 "optional": false,
345 "defaultValue": "",
346 "expandable": false,
347 "children": []
348 }
349 ]
350 }
351 ]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="setStepFailure"/>
352
353 ---
354
355 ## subscribe
356
357 This method subscribes to a workflow's events. You can use this method to listen to a [long-running workflow](!docs!/learn/fundamentals/workflows/long-running-workflow)'s events and retrieve its result once it's done executing.
358
359 Refer to the [Long-Running Workflows](!docs!/learn/fundamentals/workflows/long-running-workflow#access-long-running-workflow-status-and-result) documentation to learn more.
360
361 ### Example
362
363 ```ts
364 const { transaction } = await helloWorldWorkflow(container).run()
365
366 const subscriptionOptions = {
367 workflowId: "hello-world",
368 transactionId: transaction.transactionId,
369 subscriberId: "hello-world-subscriber",
370 }
371
372 await workflowEngineModuleService.subscribe({
373 ...subscriptionOptions,
374 subscriber: async (data) => {
375 if (data.eventType === "onFinish") {
376 console.log("Finished execution", data.result)
377 // unsubscribe
378 await workflowEngineModuleService.unsubscribe({
379 ...subscriptionOptions,
380 subscriberOrId: subscriptionOptions.subscriberId,
381 })
382 } else if (data.eventType === "onStepFailure") {
383 console.log("Workflow failed", data.step)
384 }
385 },
386 })
387 ```
388
389 ### Parameters
390
391 <TypeList types={[
392 {
393 "name": "subscriptionOptions",
394 "type": "`object`",
395 "description": "The options for the subscription.",
396 "optional": false,
397 "defaultValue": "",
398 "expandable": false,
399 "children": [
400 {
401 "name": "workflowId",
402 "type": "`string`",
403 "description": "The ID of the workflow to subscribe to. This is the first parameter passed to `createWorkflow` when creating the workflow.",
404 "optional": false,
405 "defaultValue": "",
406 "expandable": false,
407 "children": []
408 },
409 {
410 "name": "transactionId",
411 "type": "`string`",
412 "description": "The ID of the workflow execution's transaction. This is returned when you execute a workflow.",
413 "optional": false,
414 "defaultValue": "",
415 "expandable": false,
416 "children": []
417 },
418 {
419 "name": "subscriberId",
420 "type": "`string`",
421 "description": "A unique ID for the subscriber. It's used to unsubscribe from the workflow's events.",
422 "optional": false,
423 "defaultValue": "",
424 "expandable": false,
425 "children": []
426 },
427 {
428 "name": "subscriber",
429 "type": "`(data: WorkflowEvent) => void`",
430 "description": "The subscriber function that will be called when the workflow emits an event.",
431 "optional": false,
432 "defaultValue": "",
433 "expandable": false,
434 "children": []
435 }
436 ]
437 }
438 ]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="subscribe"/>
439
440 ---
441
442 ## unsubscribe
443
444 This method unsubscribes from a workflow's events. You can use this method to stop listening to a [long-running workflow](!docs!/learn/fundamentals/workflows/long-running-workflow)'s events after you've received the result.
445
446 ### Example
447
448 ```ts
449 await workflowEngineModuleService.unsubscribe({
450 workflowId: "hello-world",
451 transactionId: "transaction-id",
452 subscriberOrId: "hello-world-subscriber",
453 })
454 ```
455
456 ### Parameters
457
458 <TypeList types={[
459 {
460 "name": "workflowId",
461 "type": "`string`",
462 "description": "The ID of the workflow to unsubscribe from. This is the first parameter passed to `createWorkflow` when creating the workflow.",
463 "optional": false,
464 "defaultValue": "",
465 "expandable": false,
466 "children": []
467 },
468 {
469 "name": "transactionId",
470 "type": "`string`",
471 "description": "The ID of the workflow execution's transaction. This is returned when you execute a workflow.",
472 "optional": false,
473 "defaultValue": "",
474 "expandable": false,
475 "children": []
476 },
477 {
478 "name": "subscriberOrId",
479 "type": "`string`",
480 "description": "The subscriber ID or the subscriber function to unsubscribe from the workflow's events.",
481 "optional": false,
482 "defaultValue": "",
483 "expandable": false,
484 "children": []
485 }
486 ]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="unsubscribe"/>