Raw
1 import { Table, Prerequisites, CodeTabs, CodeTab } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Debug Workflows`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn about the different ways you can debug workflows in Medusa.
10
11 Debugging workflows is essential to ensure your custom features work as expected. It helps you identify unexpected issues and bugs in your workflow logic.
12
13 ## Approaches to Debug Workflows
14
15 There are several ways to debug workflows in Medusa:
16
17 <Table>
18 <Table.Header>
19 <Table.Row>
20 <Table.HeaderCell>
21 Approach
22 </Table.HeaderCell>
23 <Table.HeaderCell>
24 When to Use
25 </Table.HeaderCell>
26 </Table.Row>
27 </Table.Header>
28 <Table.Body>
29 <Table.Row>
30 <Table.Cell>
31 [Write integration tests](#approach-1-write-integration-tests)
32 </Table.Cell>
33 <Table.Cell>
34 To ensure your workflow produces the expected results and handles edge cases.
35 </Table.Cell>
36 </Table.Row>
37 <Table.Row>
38 <Table.Cell>
39 [Add breakpoints](#approach-2-add-breakpoints)
40 </Table.Cell>
41 <Table.Cell>
42 To inspect specific steps in your workflow and understand the data flow.
43 </Table.Cell>
44 </Table.Row>
45 <Table.Row>
46 <Table.Cell>
47 [Log messages](#approach-3-log-messages)
48 </Table.Cell>
49 <Table.Cell>
50 To check values during execution with minimal overhead.
51 </Table.Cell>
52 </Table.Row>
53 <Table.Row>
54 <Table.Cell>
55 [View Workflow Executions in Medusa Admin](#approach-4-monitor-workflow-executions-in-medusa-admin)
56 </Table.Cell>
57 <Table.Cell>
58 To monitor stored workflow executions and long-running workflows, especially in production environments.
59 </Table.Cell>
60 </Table.Row>
61 </Table.Body>
62 </Table>
63
64 ---
65
66 ## Approach 1: Write Integration Tests
67
68 Integration tests run your workflow in a controlled environment to verify its behavior and outcome. By writing integration tests, you ensure your workflow produces the expected results and handles edge cases.
69
70 ### When to Use Integration Tests
71
72 It's recommended to always write integration tests for your workflows. This helps you catch issues early and ensures your custom logic works as intended.
73
74 ### How to Write Integration Tests for Workflows
75
76 Refer to the [Integration Tests](../testing-tools/integration-tests/page.mdx) chapter to learn how to write integration tests for your workflows and find examples of testing workflows.
77
78 ---
79
80 ## Approach 2: Add Breakpoints
81
82 Breakpoints allow you to pause workflow execution at specific steps and inspect the data. They're useful for understanding the data flow in your steps and identifying issues.
83
84 ### When to Use Breakpoints
85
86 Use breakpoints when you need to debug specific steps in your workflow, rather than the entire workflow. You can verify that the step is behaving as expected and is producing the correct output.
87
88 ### Where Can You Add Breakpoints
89
90 Since Medusa stores an internal representation of the workflow constructor on application startup, breakpoints within the workflow's constructor won't work during execution. Learn more in the [Data Manipulation](../../fundamentals/workflows/variable-manipulation/page.mdx) chapter.
91
92 Instead, you can add breakpoints in:
93
94 - A step function.
95 - A step's compensation function.
96 - The `transform` callback function of a step.
97
98 For example:
99
100 <CodeTabs group="debug-type">
101 <CodeTab label="Step Function" value="step-function">
102
103 ```ts highlights={[["11"], ["12"]]} collapsibleLines="1-7" expandButtonLabel="Show Imports"
104 import {
105 createStep,
106 createWorkflow,
107 StepResponse,
108 WorkflowResponse,
109 } from "@medusajs/framework/workflows-sdk"
110
111 const step1 = createStep(
112 "step-1",
113 async () => {
114 // Add a breakpoint here to inspect the message
115 const message = "Hello from step 1!"
116
117 return new StepResponse(
118 message
119 )
120 }
121 )
122
123 export const myWorkflow = createWorkflow(
124 "my-workflow",
125 () => {
126 const response = step1()
127
128 return new WorkflowResponse({
129 response,
130 })
131 }
132 )
133 ```
134 </CodeTab>
135 <CodeTab label="Compensation Function" value="compensation-function">
136
137 ```ts highlights={[["18"], ["19"]]} collapsibleLines="1-7" expandButtonLabel="Show Imports"
138 import {
139 createStep,
140 createWorkflow,
141 StepResponse,
142 WorkflowResponse,
143 } from "@medusajs/framework/workflows-sdk"
144
145 const step1 = createStep(
146 "step-1",
147 async () => {
148 const message = "Hello from step 1!"
149
150 return new StepResponse(
151 message
152 )
153 },
154 async () => {
155 // Add a breakpoint here to inspect the compensation logic
156 console.log("Compensating step 1")
157 }
158 )
159
160 const step2 = createStep(
161 "step-2",
162 async () => {
163 throw new Error("This is an error in step 2")
164 }
165 )
166
167 export const myWorkflow = createWorkflow(
168 "my-workflow",
169 () => {
170 const response = step1()
171 step2()
172
173 return new WorkflowResponse({
174 response,
175 })
176 }
177 )
178 ```
179 </CodeTab>
180 <CodeTab label="Transform Callback" value="transform-callback">
181
182 ```ts highlights={[["28"], ["29"]]} collapsibleLines="1-8" expandButtonLabel="Show Imports"
183 import {
184 createStep,
185 createWorkflow,
186 StepResponse,
187 WorkflowResponse,
188 transform,
189 } from "@medusajs/framework/workflows-sdk"
190
191 const step1 = createStep(
192 "step-1",
193 async () => {
194 const message = "Hello from step 1!"
195
196 return new StepResponse(
197 message
198 )
199 }
200 )
201
202 export const myWorkflow = createWorkflow(
203 "my-workflow",
204 () => {
205 const response = step1()
206
207 const transformedMessage = transform(
208 { response },
209 (data) => {
210 // Add a breakpoint here to inspect the transformed data
211 const upperCase = data.response.toUpperCase()
212 return upperCase
213 }
214 )
215
216 return new WorkflowResponse({
217 response: transformedMessage,
218 })
219 }
220 )
221 ```
222 </CodeTab>
223 </CodeTabs>
224
225 ### How to Add Breakpoints
226
227 If your code editor supports adding breakpoints, you can add them in your step and compensation functions, or the `transform` callback function. When the workflow execution reaches the breakpoint, your code editor will pause execution, allowing you to inspect the data and walk through the code.
228
229 If you're using VS Code or Cursor, learn how to add breakpoints in the [VS Code documentation](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_breakpoints). For other code editors, refer to their respective documentation.
230
231 ---
232
233 ## Approach 3: Log Messages
234
235 Logging messages is a simple yet effective way to debug code. By logging messages, you can check values during execution with minimal overhead.
236
237 ### When to Use Logging
238
239 Use logging when debugging workflows and you want to check values during execution without the overhead of setting up breakpoints.
240
241 Logging is also useful when you want to verify variable values between steps or in a `transform` callback function.
242
243 ### How to Log Messages
244
245 Since Medusa stores an internal representation of the workflow constructor on application startup, you can't directly log messages in the workflow's constructor.
246
247 Instead, you can log messages in:
248
249 - A step function.
250 - A step's compensation function.
251 - The `transform` callback function of a step.
252
253 You can log messages with `console.log`. In step and compensation functions, you can also use the [Logger](../logging/page.mdx) to log messages with different log levels (info, warn, error).
254
255 For example:
256
257 <CodeTabs group="debug-type">
258 <CodeTab label="Step Function" value="step-function">
259
260 ```ts highlights={[["19"]]} collapsibleLines="1-10" expandButtonLabel="Show Imports"
261 import {
262 createStep,
263 createWorkflow,
264 StepResponse,
265 WorkflowResponse,
266 } from "@medusajs/framework/workflows-sdk"
267 import {
268 ContainerRegistrationKeys,
269 } from "@medusajs/framework/utils"
270
271 const step1 = createStep(
272 "step-1",
273 async ({}, { container }) => {
274 const logger = container.resolve(
275 ContainerRegistrationKeys.LOGGER
276 )
277 const message = "Hello from step 1!"
278
279 logger.info(`Step 1 output: ${message}`)
280
281 return new StepResponse(
282 message
283 )
284 }
285 )
286
287 export const myWorkflow = createWorkflow(
288 "my-workflow",
289 () => {
290 const response = step1()
291
292 return new WorkflowResponse({
293 response,
294 })
295 }
296 )
297 ```
298
299 </CodeTab>
300 <CodeTab label="Compensation Function" value="compensation-function">
301
302 ```ts highlights={[["29"]]} collapsibleLines="1-10" expandButtonLabel="Show Imports"
303 import {
304 createStep,
305 createWorkflow,
306 StepResponse,
307 WorkflowResponse,
308 } from "@medusajs/framework/workflows-sdk"
309 import {
310 ContainerRegistrationKeys,
311 } from "@medusajs/framework/utils"
312
313 const step1 = createStep(
314 "step-1",
315 async ({}, { container }) => {
316 const logger = container.resolve(
317 ContainerRegistrationKeys.LOGGER
318 )
319 const message = "Hello from step 1!"
320
321 logger.info(`Step 1 output: ${message}`)
322
323 return new StepResponse(
324 message
325 )
326 },
327 async (_, { container }) => {
328 const logger = container.resolve(
329 ContainerRegistrationKeys.LOGGER
330 )
331 logger.warn("Compensating step 1")
332 }
333 )
334
335 export const myWorkflow = createWorkflow(
336 "my-workflow",
337 () => {
338 const response = step1()
339
340 return new WorkflowResponse({
341 response,
342 })
343 }
344 )
345 ```
346
347 </CodeTab>
348 <CodeTab label="Transform Callback" value="transform-callback">
349
350 ```ts highlights={[["29"]]} collapsibleLines="1-8" expandButtonLabel="Show Imports"
351 import {
352 createStep,
353 createWorkflow,
354 StepResponse,
355 WorkflowResponse,
356 transform,
357 } from "@medusajs/framework/workflows-sdk"
358
359 const step1 = createStep(
360 "step-1",
361 async () => {
362 const message = "Hello from step 1!"
363
364 return new StepResponse(
365 message
366 )
367 }
368 )
369
370 export const myWorkflow = createWorkflow(
371 "my-workflow",
372 () => {
373 const response = step1()
374
375 const transformedMessage = transform(
376 { response },
377 (data) => {
378 const upperCase = data.response.toUpperCase()
379 console.log("Transformed Data:", upperCase)
380 return upperCase
381 }
382 )
383
384 return new WorkflowResponse({
385 response: transformedMessage,
386 })
387 }
388 )
389 ```
390
391 </CodeTab>
392 </CodeTabs>
393
394 If you execute the workflow, you'll see the logged message in your console.
395
396 <Note>
397
398 Learn more about logging in the [Logger](../logging/page.mdx) chapter.
399
400 </Note>
401
402 ---
403
404 ## Approach 4: Monitor Workflow Executions in Medusa Admin
405
406 The Medusa Admin has a [Workflows](!user-guide!/settings/developer/workflows) settings page that provides a user-friendly interface to view stored workflow executions.
407
408 ### When to Use Admin Monitoring
409
410 Use the Medusa Admin to monitor [stored workflow executions](../../fundamentals/workflows/store-executions/page.mdx) when debugging unexpected issues and edge cases, especially in production environments and long-running workflows that run in the background.
411
412 By viewing the workflow executions through the Medusa Admin, you can:
413
414 - View the status of stored workflow executions.
415 - Inspect input and output data for each execution and its steps.
416 - Identify any issues or errors in the workflow execution.
417
418 ### How to Monitor Workflow Executions in the Admin
419
420 The Workflows settings page in the Medusa Admin shows you the history of stored workflow executions only. Workflow executions are stored if a workflow is [long-running](../../fundamentals/workflows/long-running-workflow/page.mdx), or if the `store` and `retentionTime` options are set on the workflow.
421
422 For example, to store workflow executions:
423
424 <Prerequisites
425 items={[
426 {
427 text: "Redis Workflow Engine must be installed and configured.",
428 link: "!resources!/infrastructure-modules/workflow-engine/redis"
429 }
430 ]}
431 />
432
433 ```ts highlights={[["22"], ["23"]]} collapsibleLines="1-7" expandButtonLabel="Show Imports"
434 import {
435 createStep,
436 createWorkflow,
437 StepResponse,
438 WorkflowResponse,
439 } from "@medusajs/framework/workflows-sdk"
440
441 const step1 = createStep(
442 "step-1",
443 async () => {
444 const message = "Hello from step 1!"
445
446 return new StepResponse(
447 message
448 )
449 }
450 )
451
452 export const myWorkflow = createWorkflow(
453 {
454 name: "my-workflow",
455 retentionTime: 99999,
456 store: true,
457 },
458 () => {
459 const response = step1()
460
461 return new WorkflowResponse({
462 response,
463 })
464 }
465 )
466 ```
467
468 <Note>
469
470 Refer to the [Store Workflow Executions](../../fundamentals/workflows/store-executions/page.mdx) chapter to learn more.
471
472 </Note>
473
474 You can view all executions of this workflow in the Medusa Admin under the [Workflows settings page](!user-guide!/settings/developer/workflows). Each execution will show you the status, input, and output data.