|
1
|
import { IWorkflowEngineService } from "@medusajs/framework/types" |
|
2
|
import { Modules, TransactionHandlerType } from "@medusajs/framework/utils" |
|
3
|
import { |
|
4
|
createStep, |
|
5
|
createWorkflow, |
|
6
|
parallelize, |
|
7
|
StepResponse, |
|
8
|
transform, |
|
9
|
WorkflowResponse, |
|
10
|
} from "@medusajs/framework/workflows-sdk" |
|
11
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
12
|
import { setTimeout } from "timers/promises" |
|
13
|
import { ulid } from "ulid" |
|
14
|
import "../__fixtures__" |
|
15
|
import { TestDatabase } from "../utils/database" |
|
16
|
|
|
17
|
jest.setTimeout(20000) |
|
18
|
|
|
19
|
const testRunId = ulid() |
|
20
|
|
|
21
|
moduleIntegrationTestRunner<IWorkflowEngineService>({ |
|
22
|
moduleName: Modules.WORKFLOW_ENGINE, |
|
23
|
resolve: __dirname + "/../..", |
|
24
|
moduleOptions: { |
|
25
|
redis: { |
|
26
|
redisUrl: "localhost:6379", |
|
27
|
queueName: `medusa-workflows-${ |
|
28
|
process.env.JEST_WORKER_ID ?? "1" |
|
29
|
}-${testRunId}`, |
|
30
|
jobQueueName: `medusa-workflows-jobs-${ |
|
31
|
process.env.JEST_WORKER_ID ?? "1" |
|
32
|
}-${testRunId}`, |
|
33
|
}, |
|
34
|
}, |
|
35
|
testSuite: ({ service: workflowOrcModule, medusaApp }) => { |
|
36
|
describe("Testing race condition of the workflow during retry", () => { |
|
37
|
beforeEach(async () => { |
|
38
|
await TestDatabase.clearTables() |
|
39
|
jest.clearAllMocks() |
|
40
|
}) |
|
41
|
|
|
42
|
afterEach(async () => { |
|
43
|
await TestDatabase.clearTables() |
|
44
|
}) |
|
45
|
|
|
46
|
it("should manage saving multiple async steps in concurrency", async () => { |
|
47
|
const step0 = createStep( |
|
48
|
{ name: "step0", async: true, backgroundExecution: true }, |
|
49
|
async () => { |
|
50
|
return new StepResponse("result from step 0") |
|
51
|
} |
|
52
|
) |
|
53
|
|
|
54
|
const step1 = createStep( |
|
55
|
{ name: "step1", async: true, backgroundExecution: true }, |
|
56
|
async () => { |
|
57
|
return new StepResponse("result from step 1") |
|
58
|
} |
|
59
|
) |
|
60
|
|
|
61
|
const step2 = createStep( |
|
62
|
{ name: "step2", async: true, backgroundExecution: true }, |
|
63
|
async () => { |
|
64
|
return new StepResponse("result from step 2") |
|
65
|
} |
|
66
|
) |
|
67
|
const step3 = createStep( |
|
68
|
{ name: "step3", async: true, backgroundExecution: true }, |
|
69
|
async () => { |
|
70
|
return new StepResponse("result from step 3") |
|
71
|
} |
|
72
|
) |
|
73
|
|
|
74
|
const step4 = createStep( |
|
75
|
{ name: "step4", async: true, backgroundExecution: true }, |
|
76
|
async () => { |
|
77
|
return new StepResponse("result from step 4") |
|
78
|
} |
|
79
|
) |
|
80
|
const step5 = createStep({ name: "step5" }, async (all: string[]) => { |
|
81
|
const ret = [...all, "result from step 5"] |
|
82
|
return new StepResponse(ret) |
|
83
|
}) |
|
84
|
|
|
85
|
const workflowId = "workflow-1" + ulid() |
|
86
|
createWorkflow( |
|
87
|
{ |
|
88
|
name: workflowId, |
|
89
|
idempotent: true, |
|
90
|
retentionTime: 5, |
|
91
|
}, |
|
92
|
function () { |
|
93
|
const all = parallelize(step0(), step1(), step2(), step3(), step4()) |
|
94
|
const res = step5(all) |
|
95
|
return new WorkflowResponse(res) |
|
96
|
} |
|
97
|
) |
|
98
|
|
|
99
|
const transactionId = ulid() |
|
100
|
const done = new Promise<void>((resolve, reject) => { |
|
101
|
void workflowOrcModule.subscribe({ |
|
102
|
workflowId: workflowId, |
|
103
|
transactionId, |
|
104
|
subscriber: async (event) => { |
|
105
|
if (event.eventType === "onFinish") { |
|
106
|
resolve(event.result) |
|
107
|
} |
|
108
|
}, |
|
109
|
}) |
|
110
|
}) |
|
111
|
|
|
112
|
await workflowOrcModule.run(workflowId, { |
|
113
|
throwOnError: false, |
|
114
|
logOnError: true, |
|
115
|
transactionId, |
|
116
|
}) |
|
117
|
|
|
118
|
const result = await done |
|
119
|
|
|
120
|
expect(result).toEqual([ |
|
121
|
"result from step 0", |
|
122
|
"result from step 1", |
|
123
|
"result from step 2", |
|
124
|
"result from step 3", |
|
125
|
"result from step 4", |
|
126
|
"result from step 5", |
|
127
|
]) |
|
128
|
}) |
|
129
|
|
|
130
|
it("should manage saving multiple sync steps in concurrency", async () => { |
|
131
|
const step0 = createStep({ name: "step0" }, async () => { |
|
132
|
return new StepResponse("result from step 0") |
|
133
|
}) |
|
134
|
|
|
135
|
const step1 = createStep({ name: "step1" }, async () => { |
|
136
|
return new StepResponse("result from step 1") |
|
137
|
}) |
|
138
|
|
|
139
|
const step2 = createStep({ name: "step2" }, async () => { |
|
140
|
return new StepResponse("result from step 2") |
|
141
|
}) |
|
142
|
const step3 = createStep({ name: "step3" }, async () => { |
|
143
|
return new StepResponse("result from step 3") |
|
144
|
}) |
|
145
|
|
|
146
|
const step4 = createStep({ name: "step4" }, async () => { |
|
147
|
return new StepResponse("result from step 4") |
|
148
|
}) |
|
149
|
const step5 = createStep({ name: "step5" }, async (all: string[]) => { |
|
150
|
const ret = [...all, "result from step 5"] |
|
151
|
return new StepResponse(ret) |
|
152
|
}) |
|
153
|
|
|
154
|
const workflowId = "workflow-1" + ulid() |
|
155
|
createWorkflow( |
|
156
|
{ |
|
157
|
name: workflowId, |
|
158
|
idempotent: true, |
|
159
|
retentionTime: 5, |
|
160
|
}, |
|
161
|
function () { |
|
162
|
const all = parallelize(step0(), step1(), step2(), step3(), step4()) |
|
163
|
const res = step5(all) |
|
164
|
return new WorkflowResponse(res) |
|
165
|
} |
|
166
|
) |
|
167
|
|
|
168
|
const transactionId = ulid() |
|
169
|
const done = new Promise<void>((resolve, reject) => { |
|
170
|
void workflowOrcModule.subscribe({ |
|
171
|
workflowId: workflowId, |
|
172
|
transactionId, |
|
173
|
subscriber: async (event) => { |
|
174
|
if (event.eventType === "onFinish") { |
|
175
|
resolve(event.result) |
|
176
|
} |
|
177
|
}, |
|
178
|
}) |
|
179
|
}) |
|
180
|
|
|
181
|
await workflowOrcModule.run(workflowId, { |
|
182
|
throwOnError: false, |
|
183
|
logOnError: true, |
|
184
|
transactionId, |
|
185
|
}) |
|
186
|
|
|
187
|
const result = await done |
|
188
|
|
|
189
|
expect(result).toEqual([ |
|
190
|
"result from step 0", |
|
191
|
"result from step 1", |
|
192
|
"result from step 2", |
|
193
|
"result from step 3", |
|
194
|
"result from step 4", |
|
195
|
"result from step 5", |
|
196
|
]) |
|
197
|
}) |
|
198
|
|
|
199
|
it("should manage saving multiple async steps in concurrency without background execution while setting steps as success manually concurrently", async () => { |
|
200
|
const step0 = createStep({ name: "step0", async: true }, async () => {}) |
|
201
|
|
|
202
|
const step1 = createStep({ name: "step1", async: true }, async () => {}) |
|
203
|
|
|
204
|
const step2 = createStep({ name: "step2", async: true }, async () => {}) |
|
205
|
const step3 = createStep({ name: "step3", async: true }, async () => {}) |
|
206
|
|
|
207
|
const step4 = createStep({ name: "step4", async: true }, async () => {}) |
|
208
|
const step5 = createStep({ name: "step5" }, async (all: any[]) => { |
|
209
|
const ret = [...all, "result from step 5"] |
|
210
|
return new StepResponse(ret) |
|
211
|
}) |
|
212
|
|
|
213
|
const workflowId = "workflow-1" + ulid() |
|
214
|
createWorkflow( |
|
215
|
{ |
|
216
|
name: workflowId, |
|
217
|
idempotent: true, |
|
218
|
retentionTime: 1, |
|
219
|
}, |
|
220
|
function () { |
|
221
|
const all = parallelize(step0(), step1(), step2(), step3(), step4()) |
|
222
|
const res = step5(all) |
|
223
|
return new WorkflowResponse(res) |
|
224
|
} |
|
225
|
) |
|
226
|
|
|
227
|
const transactionId = ulid() |
|
228
|
const done = new Promise<void>((resolve, reject) => { |
|
229
|
void workflowOrcModule.subscribe({ |
|
230
|
workflowId: workflowId, |
|
231
|
transactionId, |
|
232
|
subscriber: async (event) => { |
|
233
|
if (event.eventType === "onFinish") { |
|
234
|
resolve(event.result) |
|
235
|
} |
|
236
|
}, |
|
237
|
}) |
|
238
|
}) |
|
239
|
|
|
240
|
await workflowOrcModule.run(workflowId, { |
|
241
|
throwOnError: false, |
|
242
|
logOnError: true, |
|
243
|
transactionId, |
|
244
|
}) |
|
245
|
|
|
246
|
await setTimeout(100) // Just to wait a bit before firering everything |
|
247
|
|
|
248
|
for (let i = 0; i <= 4; i++) { |
|
249
|
void workflowOrcModule.setStepSuccess({ |
|
250
|
idempotencyKey: { |
|
251
|
workflowId: workflowId, |
|
252
|
transactionId: transactionId, |
|
253
|
stepId: `step${i}`, |
|
254
|
action: TransactionHandlerType.INVOKE, |
|
255
|
}, |
|
256
|
stepResponse: new StepResponse("result from step " + i), |
|
257
|
}) |
|
258
|
} |
|
259
|
|
|
260
|
const res = await done |
|
261
|
|
|
262
|
expect(res).toEqual([ |
|
263
|
"result from step 0", |
|
264
|
"result from step 1", |
|
265
|
"result from step 2", |
|
266
|
"result from step 3", |
|
267
|
"result from step 4", |
|
268
|
"result from step 5", |
|
269
|
]) |
|
270
|
}) |
|
271
|
|
|
272
|
it("should prevent race continuation of the workflow during retryIntervalAwaiting in background execution", async () => { |
|
273
|
const transactionId = "transaction_id" + ulid() |
|
274
|
const workflowId = "RACE_workflow-1" + ulid() |
|
275
|
|
|
276
|
const step0InvokeMock = jest.fn() |
|
277
|
const step1InvokeMock = jest.fn() |
|
278
|
const step2InvokeMock = jest.fn() |
|
279
|
const transformMock = jest.fn() |
|
280
|
|
|
281
|
const step0 = createStep("step0", async (_) => { |
|
282
|
step0InvokeMock() |
|
283
|
return new StepResponse("result from step 0") |
|
284
|
}) |
|
285
|
|
|
286
|
const step1 = createStep("step1", async (_) => { |
|
287
|
step1InvokeMock() |
|
288
|
await setTimeout(1000) |
|
289
|
return new StepResponse({ isSuccess: true }) |
|
290
|
}) |
|
291
|
|
|
292
|
const step2 = createStep("step2", async (input: any) => { |
|
293
|
step2InvokeMock() |
|
294
|
return new StepResponse({ result: input }) |
|
295
|
}) |
|
296
|
|
|
297
|
const subWorkflow = createWorkflow("sub-workflow-1", function () { |
|
298
|
const status = step1() |
|
299
|
return new WorkflowResponse(status) |
|
300
|
}) |
|
301
|
|
|
302
|
createWorkflow( |
|
303
|
{ |
|
304
|
name: workflowId, |
|
305
|
idempotent: true, |
|
306
|
retentionTime: 5, |
|
307
|
}, |
|
308
|
function () { |
|
309
|
const build = step0() |
|
310
|
|
|
311
|
const status = subWorkflow.runAsStep({} as any).config({ |
|
312
|
async: true, |
|
313
|
compensateAsync: true, |
|
314
|
backgroundExecution: true, |
|
315
|
retryIntervalAwaiting: 0.1, |
|
316
|
}) |
|
317
|
|
|
318
|
const transformedResult = transform({ status }, (data) => { |
|
319
|
transformMock() |
|
320
|
return { |
|
321
|
status: data.status, |
|
322
|
} |
|
323
|
}) |
|
324
|
|
|
325
|
step2(transformedResult) |
|
326
|
return new WorkflowResponse(build) |
|
327
|
} |
|
328
|
) |
|
329
|
|
|
330
|
const onFinish = new Promise<void>((resolve) => { |
|
331
|
void workflowOrcModule.subscribe({ |
|
332
|
workflowId, |
|
333
|
transactionId, |
|
334
|
subscriber: (event) => { |
|
335
|
if (event.eventType === "onFinish") { |
|
336
|
resolve() |
|
337
|
} |
|
338
|
}, |
|
339
|
}) |
|
340
|
}) |
|
341
|
|
|
342
|
workflowOrcModule |
|
343
|
.run(workflowId, { |
|
344
|
transactionId, |
|
345
|
throwOnError: false, |
|
346
|
logOnError: true, |
|
347
|
}) |
|
348
|
.then(({ result }) => { |
|
349
|
expect(result).toBe("result from step 0") |
|
350
|
}) |
|
351
|
|
|
352
|
await onFinish |
|
353
|
|
|
354
|
expect(step0InvokeMock).toHaveBeenCalledTimes(1) |
|
355
|
expect(step1InvokeMock.mock.calls.length).toBeGreaterThan(1) |
|
356
|
expect(step2InvokeMock).toHaveBeenCalledTimes(1) |
|
357
|
expect(transformMock).toHaveBeenCalledTimes(1) |
|
358
|
}) |
|
359
|
|
|
360
|
it("should prevent race continuation of the workflow compensation during retryIntervalAwaiting in background execution", async () => { |
|
361
|
const transactionId = "transaction_id" + ulid() |
|
362
|
const workflowId = "RACE_workflow-1" + ulid() |
|
363
|
|
|
364
|
const step0InvokeMock = jest.fn() |
|
365
|
const step0CompensateMock = jest.fn() |
|
366
|
const step1InvokeMock = jest.fn() |
|
367
|
const step1CompensateMock = jest.fn() |
|
368
|
const step2InvokeMock = jest.fn() |
|
369
|
const transformMock = jest.fn() |
|
370
|
|
|
371
|
const step0 = createStep( |
|
372
|
"RACE_step0", |
|
373
|
async (_) => { |
|
374
|
step0InvokeMock() |
|
375
|
return new StepResponse("result from step 0") |
|
376
|
}, |
|
377
|
() => { |
|
378
|
step0CompensateMock() |
|
379
|
} |
|
380
|
) |
|
381
|
|
|
382
|
const step1 = createStep( |
|
383
|
"RACE_step1", |
|
384
|
async (_) => { |
|
385
|
step1InvokeMock() |
|
386
|
await setTimeout(1000) |
|
387
|
throw new Error("error from step 1") |
|
388
|
}, |
|
389
|
() => { |
|
390
|
step1CompensateMock() |
|
391
|
} |
|
392
|
) |
|
393
|
|
|
394
|
const step2 = createStep("RACE_step2", async (input: any) => { |
|
395
|
step2InvokeMock() |
|
396
|
return new StepResponse({ result: input }) |
|
397
|
}) |
|
398
|
|
|
399
|
const subWorkflow = createWorkflow("RACE_sub-workflow-1", function () { |
|
400
|
const status = step1() |
|
401
|
return new WorkflowResponse(status) |
|
402
|
}) |
|
403
|
|
|
404
|
createWorkflow( |
|
405
|
{ |
|
406
|
name: workflowId, |
|
407
|
}, |
|
408
|
function () { |
|
409
|
const build = step0() |
|
410
|
|
|
411
|
const status = subWorkflow.runAsStep({} as any).config({ |
|
412
|
async: true, |
|
413
|
compensateAsync: true, |
|
414
|
backgroundExecution: true, |
|
415
|
retryIntervalAwaiting: 0.1, |
|
416
|
maxAwaitingRetries: 3, |
|
417
|
}) |
|
418
|
|
|
419
|
const transformedResult = transform({ status }, (data) => { |
|
420
|
transformMock() |
|
421
|
return { |
|
422
|
status: data.status, |
|
423
|
} |
|
424
|
}) |
|
425
|
|
|
426
|
step2(transformedResult) |
|
427
|
return new WorkflowResponse(build) |
|
428
|
} |
|
429
|
) |
|
430
|
|
|
431
|
const onFinish = new Promise<void>((resolve) => { |
|
432
|
void workflowOrcModule.subscribe({ |
|
433
|
workflowId, |
|
434
|
transactionId, |
|
435
|
subscriber: async (event) => { |
|
436
|
if (event.eventType === "onFinish") { |
|
437
|
resolve() |
|
438
|
} |
|
439
|
}, |
|
440
|
}) |
|
441
|
}) |
|
442
|
|
|
443
|
await workflowOrcModule |
|
444
|
.run(workflowId, { |
|
445
|
transactionId, |
|
446
|
throwOnError: false, |
|
447
|
logOnError: true, |
|
448
|
}) |
|
449
|
.then(({ result }) => { |
|
450
|
expect(result).toBe("result from step 0") |
|
451
|
}) |
|
452
|
|
|
453
|
await onFinish |
|
454
|
|
|
455
|
expect(step0InvokeMock).toHaveBeenCalledTimes(1) |
|
456
|
expect(step0CompensateMock).toHaveBeenCalledTimes(1) |
|
457
|
expect(step1InvokeMock).toHaveBeenCalledTimes(3) |
|
458
|
expect(step1CompensateMock.mock.calls.length).toBeGreaterThan(0) |
|
459
|
expect(step2InvokeMock).toHaveBeenCalledTimes(0) |
|
460
|
expect(transformMock).toHaveBeenCalledTimes(0) |
|
461
|
}) |
|
462
|
}) |
|
463
|
}, |
|
464
|
}) |