|
1
|
import { MedusaContainer } from "@medusajs/framework" |
|
2
|
import { asFunction } from "@medusajs/framework/awilix" |
|
3
|
import { |
|
4
|
DistributedTransactionType, |
|
5
|
TransactionState, |
|
6
|
WorkflowManager, |
|
7
|
} from "@medusajs/framework/orchestration" |
|
8
|
import { |
|
9
|
Context, |
|
10
|
IWorkflowEngineService, |
|
11
|
Logger, |
|
12
|
RemoteQueryFunction, |
|
13
|
} from "@medusajs/framework/types" |
|
14
|
import { |
|
15
|
ContainerRegistrationKeys, |
|
16
|
Module, |
|
17
|
Modules, |
|
18
|
promiseAll, |
|
19
|
TransactionHandlerType, |
|
20
|
} from "@medusajs/framework/utils" |
|
21
|
import { |
|
22
|
createStep, |
|
23
|
createWorkflow, |
|
24
|
StepResponse, |
|
25
|
WorkflowResponse, |
|
26
|
} from "@medusajs/framework/workflows-sdk" |
|
27
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
28
|
import { WorkflowsModuleService } from "@services" |
|
29
|
import { setTimeout as setTimeoutSync } from "timers" |
|
30
|
import { setTimeout as setTimeoutPromise } from "timers/promises" |
|
31
|
import { ulid } from "ulid" |
|
32
|
import "../__fixtures__" |
|
33
|
import { |
|
34
|
conditionalStep2Invoke, |
|
35
|
conditionalStep3Invoke, |
|
36
|
workflow2Step2Invoke, |
|
37
|
workflow2Step3Invoke, |
|
38
|
workflowNotIdempotentWithRetentionStep2Invoke, |
|
39
|
workflowNotIdempotentWithRetentionStep3Invoke, |
|
40
|
} from "../__fixtures__" |
|
41
|
import { |
|
42
|
step1CompensateMock as step1CompensateMockAutoRetries, |
|
43
|
step1InvokeMock as step1InvokeMockAutoRetries, |
|
44
|
step2CompensateMock as step2CompensateMockAutoRetries, |
|
45
|
step2InvokeMock as step2InvokeMockAutoRetries, |
|
46
|
} from "../__fixtures__/workflow_1_auto_retries" |
|
47
|
import { |
|
48
|
step1CompensateMock as step1CompensateMockAutoRetriesFalse, |
|
49
|
step1InvokeMock as step1InvokeMockAutoRetriesFalse, |
|
50
|
step2CompensateMock as step2CompensateMockAutoRetriesFalse, |
|
51
|
step2InvokeMock as step2InvokeMockAutoRetriesFalse, |
|
52
|
} from "../__fixtures__/workflow_1_auto_retries_false" |
|
53
|
import { |
|
54
|
step1InvokeMock as step1InvokeMockManualRetry, |
|
55
|
step2InvokeMock as step2InvokeMockManualRetry, |
|
56
|
} from "../__fixtures__/workflow_1_manual_retry_step" |
|
57
|
import { |
|
58
|
eventGroupWorkflowId, |
|
59
|
workflowEventGroupIdStep1Mock, |
|
60
|
workflowEventGroupIdStep2Mock, |
|
61
|
} from "../__fixtures__/workflow_event_group_id" |
|
62
|
import { createScheduled } from "../__fixtures__/workflow_scheduled" |
|
63
|
|
|
64
|
jest.setTimeout(60000) |
|
65
|
|
|
66
|
function times(num) { |
|
67
|
let resolver |
|
68
|
let counter = 0 |
|
69
|
const promise = new Promise((resolve) => { |
|
70
|
resolver = resolve |
|
71
|
}) |
|
72
|
|
|
73
|
return { |
|
74
|
next: () => { |
|
75
|
counter += 1 |
|
76
|
if (counter === num) { |
|
77
|
resolver() |
|
78
|
} |
|
79
|
}, |
|
80
|
// Force resolution after 10 seconds to prevent infinite awaiting |
|
81
|
promise: Promise.race([ |
|
82
|
promise, |
|
83
|
new Promise((_, reject) => { |
|
84
|
setTimeoutSync( |
|
85
|
() => reject("times has not been resolved after 10 seconds."), |
|
86
|
10000 |
|
87
|
) |
|
88
|
}), |
|
89
|
]), |
|
90
|
} |
|
91
|
} |
|
92
|
|
|
93
|
moduleIntegrationTestRunner<IWorkflowEngineService>({ |
|
94
|
moduleName: Modules.WORKFLOW_ENGINE, |
|
95
|
resolve: __dirname + "/../..", |
|
96
|
testSuite: ({ service: workflowOrcModule, medusaApp }) => { |
|
97
|
describe("Workflow Orchestrator module", function () { |
|
98
|
let query: RemoteQueryFunction |
|
99
|
let sharedContainer_: MedusaContainer |
|
100
|
|
|
101
|
beforeEach(() => { |
|
102
|
query = medusaApp.query |
|
103
|
sharedContainer_ = medusaApp.sharedContainer |
|
104
|
}) |
|
105
|
|
|
106
|
it(`should export the appropriate linkable configuration`, () => { |
|
107
|
const linkable = Module(Modules.WORKFLOW_ENGINE, { |
|
108
|
service: WorkflowsModuleService, |
|
109
|
}).linkable |
|
110
|
|
|
111
|
expect(Object.keys(linkable)).toEqual(["workflowExecution"]) |
|
112
|
|
|
113
|
Object.keys(linkable).forEach((key) => { |
|
114
|
delete linkable[key].toJSON |
|
115
|
}) |
|
116
|
|
|
117
|
expect(linkable).toEqual({ |
|
118
|
workflowExecution: { |
|
119
|
id: { |
|
120
|
linkable: "workflow_execution_id", |
|
121
|
entity: "WorkflowExecution", |
|
122
|
primaryKey: "id", |
|
123
|
serviceName: "workflows", |
|
124
|
field: "workflowExecution", |
|
125
|
}, |
|
126
|
transaction_id: { |
|
127
|
linkable: "workflow_execution_transaction_id", |
|
128
|
entity: "WorkflowExecution", |
|
129
|
primaryKey: "transaction_id", |
|
130
|
serviceName: "workflows", |
|
131
|
field: "workflowExecution", |
|
132
|
}, |
|
133
|
workflow_id: { |
|
134
|
linkable: "workflow_execution_workflow_id", |
|
135
|
entity: "WorkflowExecution", |
|
136
|
primaryKey: "workflow_id", |
|
137
|
serviceName: "workflows", |
|
138
|
field: "workflowExecution", |
|
139
|
}, |
|
140
|
run_id: { |
|
141
|
linkable: "workflow_execution_run_id", |
|
142
|
entity: "WorkflowExecution", |
|
143
|
primaryKey: "run_id", |
|
144
|
serviceName: "workflows", |
|
145
|
field: "workflowExecution", |
|
146
|
}, |
|
147
|
}, |
|
148
|
}) |
|
149
|
}) |
|
150
|
|
|
151
|
describe("Cancel transaction", function () { |
|
152
|
it("should cancel an ongoing execution with async unfinished yet step", async () => { |
|
153
|
const transactionId = "transaction-to-cancel-id" + ulid() |
|
154
|
const step1 = createStep("step1", async () => { |
|
155
|
return new StepResponse("step1") |
|
156
|
}) |
|
157
|
|
|
158
|
const step2 = createStep("step2", async () => { |
|
159
|
await setTimeoutPromise(200) |
|
160
|
return new StepResponse("step2") |
|
161
|
}) |
|
162
|
|
|
163
|
const step3 = createStep("step3", async () => { |
|
164
|
return new StepResponse("step3") |
|
165
|
}) |
|
166
|
|
|
167
|
const workflowId = "workflow-to-cancel-id" + ulid() |
|
168
|
|
|
169
|
createWorkflow({ name: workflowId, retentionTime: 60 }, function () { |
|
170
|
step1() |
|
171
|
step2().config({ async: true }) |
|
172
|
step3() |
|
173
|
|
|
174
|
return new WorkflowResponse("finished") |
|
175
|
}) |
|
176
|
|
|
177
|
const onFinish = new Promise<void>((resolve) => { |
|
178
|
workflowOrcModule.subscribe({ |
|
179
|
workflowId, |
|
180
|
transactionId, |
|
181
|
subscriber: async (event) => { |
|
182
|
if (event.eventType === "onFinish") { |
|
183
|
resolve() |
|
184
|
} |
|
185
|
}, |
|
186
|
}) |
|
187
|
|
|
188
|
workflowOrcModule |
|
189
|
.run(workflowId, { |
|
190
|
input: {}, |
|
191
|
transactionId, |
|
192
|
}) |
|
193
|
.then(async () => { |
|
194
|
await setTimeoutPromise(100) |
|
195
|
|
|
196
|
await workflowOrcModule.cancel(workflowId, { |
|
197
|
transactionId, |
|
198
|
}) |
|
199
|
}) |
|
200
|
}) |
|
201
|
|
|
202
|
await onFinish |
|
203
|
|
|
204
|
const execution = await workflowOrcModule.listWorkflowExecutions({ |
|
205
|
transaction_id: transactionId, |
|
206
|
}) |
|
207
|
|
|
208
|
expect(execution.length).toEqual(1) |
|
209
|
expect(execution[0].state).toEqual(TransactionState.REVERTED) |
|
210
|
}) |
|
211
|
|
|
212
|
it("should cancel a complete execution with a sync workflow running as async", async () => { |
|
213
|
const workflowId = "workflow-to-cancel-id" + ulid() |
|
214
|
const transactionId = "transaction-to-cancel-id" + ulid() |
|
215
|
const step1 = createStep("step1", async () => { |
|
216
|
return new StepResponse("step1") |
|
217
|
}) |
|
218
|
|
|
219
|
const step2 = createStep("step2", async () => { |
|
220
|
return new StepResponse("step2") |
|
221
|
}) |
|
222
|
|
|
223
|
const step3 = createStep("step3", async () => { |
|
224
|
return new StepResponse("step3") |
|
225
|
}) |
|
226
|
|
|
227
|
const subWorkflowId = "sub-workflow-id" + ulid() |
|
228
|
const subWorkflow = createWorkflow( |
|
229
|
{ name: subWorkflowId, retentionTime: 60 }, |
|
230
|
function () { |
|
231
|
return new WorkflowResponse(step2()) |
|
232
|
} |
|
233
|
) |
|
234
|
|
|
235
|
createWorkflow({ name: workflowId, retentionTime: 60 }, function () { |
|
236
|
step1() |
|
237
|
subWorkflow.runAsStep({ input: {} }).config({ async: true }) |
|
238
|
step3() |
|
239
|
|
|
240
|
return new WorkflowResponse("finished") |
|
241
|
}) |
|
242
|
|
|
243
|
await workflowOrcModule.run(workflowId, { |
|
244
|
input: {}, |
|
245
|
transactionId, |
|
246
|
}) |
|
247
|
|
|
248
|
await setTimeoutPromise(100) |
|
249
|
|
|
250
|
await workflowOrcModule.cancel(workflowId, { |
|
251
|
transactionId, |
|
252
|
}) |
|
253
|
|
|
254
|
await setTimeoutPromise(500) |
|
255
|
|
|
256
|
const execution = await workflowOrcModule.listWorkflowExecutions({ |
|
257
|
transaction_id: transactionId, |
|
258
|
}) |
|
259
|
|
|
260
|
expect(execution.length).toEqual(1) |
|
261
|
expect(execution[0].state).toEqual(TransactionState.REVERTED) |
|
262
|
}) |
|
263
|
|
|
264
|
it("should cancel an ongoing execution with a sync workflow running as async", async () => { |
|
265
|
const workflowId = "workflow-to-cancel-id" + ulid() |
|
266
|
const transactionId = "transaction-to-cancel-id" + ulid() |
|
267
|
const step1 = createStep("step1", async () => { |
|
268
|
return new StepResponse("step1") |
|
269
|
}) |
|
270
|
|
|
271
|
const step2 = createStep("step2", async () => { |
|
272
|
await setTimeoutPromise(500) |
|
273
|
return new StepResponse("step2") |
|
274
|
}) |
|
275
|
|
|
276
|
const step3 = createStep("step3", async () => { |
|
277
|
return new StepResponse("step3") |
|
278
|
}) |
|
279
|
|
|
280
|
const subWorkflowId = "sub-workflow-id" + ulid() |
|
281
|
const subWorkflow = createWorkflow( |
|
282
|
{ name: subWorkflowId, retentionTime: 60 }, |
|
283
|
function () { |
|
284
|
return new WorkflowResponse(step2()) |
|
285
|
} |
|
286
|
) |
|
287
|
|
|
288
|
createWorkflow({ name: workflowId, retentionTime: 60 }, function () { |
|
289
|
step1() |
|
290
|
subWorkflow.runAsStep({ input: {} }).config({ async: true }) |
|
291
|
step3() |
|
292
|
|
|
293
|
return new WorkflowResponse("finished") |
|
294
|
}) |
|
295
|
|
|
296
|
await workflowOrcModule.run(workflowId, { |
|
297
|
input: {}, |
|
298
|
transactionId, |
|
299
|
}) |
|
300
|
|
|
301
|
await setTimeoutPromise(100) |
|
302
|
|
|
303
|
await workflowOrcModule.cancel(workflowId, { |
|
304
|
transactionId, |
|
305
|
}) |
|
306
|
|
|
307
|
await setTimeoutPromise(1000) |
|
308
|
|
|
309
|
const execution = await workflowOrcModule.listWorkflowExecutions({ |
|
310
|
transaction_id: transactionId, |
|
311
|
}) |
|
312
|
|
|
313
|
expect(execution.length).toEqual(1) |
|
314
|
expect(execution[0].state).toEqual(TransactionState.REVERTED) |
|
315
|
}) |
|
316
|
|
|
317
|
it("should cancel an ongoing execution with sync steps only", async () => { |
|
318
|
const transactionId = "transaction-to-cancel-id" + ulid() |
|
319
|
const step1 = createStep("step1", async () => { |
|
320
|
return new StepResponse("step1") |
|
321
|
}) |
|
322
|
|
|
323
|
const step2 = createStep("step2", async () => { |
|
324
|
await setTimeoutPromise(500) |
|
325
|
return new StepResponse("step2") |
|
326
|
}) |
|
327
|
|
|
328
|
const step3 = createStep("step3", async () => { |
|
329
|
return new StepResponse("step3") |
|
330
|
}) |
|
331
|
|
|
332
|
const workflowId = "workflow-to-cancel-id" + ulid() |
|
333
|
|
|
334
|
createWorkflow({ name: workflowId, retentionTime: 60 }, function () { |
|
335
|
step1() |
|
336
|
step2() |
|
337
|
step3() |
|
338
|
|
|
339
|
return new WorkflowResponse("finished") |
|
340
|
}) |
|
341
|
|
|
342
|
await workflowOrcModule.run(workflowId, { |
|
343
|
input: {}, |
|
344
|
transactionId, |
|
345
|
}) |
|
346
|
|
|
347
|
await setTimeoutPromise(100) |
|
348
|
|
|
349
|
await workflowOrcModule.cancel(workflowId, { |
|
350
|
transactionId, |
|
351
|
}) |
|
352
|
|
|
353
|
await setTimeoutPromise(1000) |
|
354
|
|
|
355
|
const execution = await workflowOrcModule.listWorkflowExecutions({ |
|
356
|
transaction_id: transactionId, |
|
357
|
}) |
|
358
|
|
|
359
|
expect(execution.length).toEqual(1) |
|
360
|
expect(execution[0].state).toEqual(TransactionState.REVERTED) |
|
361
|
}) |
|
362
|
}) |
|
363
|
|
|
364
|
it("should manually retry a step that is taking too long to finish", async () => { |
|
365
|
const transactionId = "transaction-manual-retry" + ulid() |
|
366
|
const workflowId = "workflow_1_manual_retry_step" |
|
367
|
|
|
368
|
await workflowOrcModule |
|
369
|
.run(workflowId, { |
|
370
|
input: {}, |
|
371
|
transactionId, |
|
372
|
}) |
|
373
|
.then(() => { |
|
374
|
expect(step1InvokeMockManualRetry).toHaveBeenCalledTimes(1) |
|
375
|
expect(step2InvokeMockManualRetry).toHaveBeenCalledTimes(1) |
|
376
|
|
|
377
|
const onFinishPromise = new Promise<void>((resolve, reject) => { |
|
378
|
workflowOrcModule.subscribe({ |
|
379
|
workflowId, |
|
380
|
transactionId, |
|
381
|
subscriber: async (event) => { |
|
382
|
if (event.eventType === "onFinish") { |
|
383
|
resolve() |
|
384
|
} |
|
385
|
}, |
|
386
|
}) |
|
387
|
}) |
|
388
|
|
|
389
|
void workflowOrcModule.retryStep({ |
|
390
|
idempotencyKey: { |
|
391
|
workflowId, |
|
392
|
transactionId, |
|
393
|
stepId: "step_2", |
|
394
|
action: "invoke", |
|
395
|
}, |
|
396
|
}) |
|
397
|
|
|
398
|
return onFinishPromise |
|
399
|
}) |
|
400
|
|
|
401
|
expect(step1InvokeMockManualRetry).toHaveBeenCalledTimes(1) |
|
402
|
expect(step2InvokeMockManualRetry).toHaveBeenCalledTimes(2) |
|
403
|
}) |
|
404
|
|
|
405
|
it("should retry steps X times automatically when maxRetries is set", async () => { |
|
406
|
const transactionId = "transaction-auto-retries" + ulid() |
|
407
|
const workflowId = "workflow_1_auto_retries" |
|
408
|
|
|
409
|
const onFinishPromise = new Promise<void>((resolve, reject) => { |
|
410
|
workflowOrcModule.subscribe({ |
|
411
|
workflowId, |
|
412
|
transactionId, |
|
413
|
subscriber: async (event) => { |
|
414
|
if (event.eventType === "onFinish") { |
|
415
|
resolve() |
|
416
|
} |
|
417
|
}, |
|
418
|
}) |
|
419
|
}) |
|
420
|
|
|
421
|
void workflowOrcModule.run(workflowId, { |
|
422
|
input: {}, |
|
423
|
transactionId, |
|
424
|
}) |
|
425
|
|
|
426
|
await onFinishPromise |
|
427
|
|
|
428
|
expect(step1InvokeMockAutoRetries).toHaveBeenCalledTimes(1) |
|
429
|
expect(step2InvokeMockAutoRetries).toHaveBeenCalledTimes(3) |
|
430
|
expect(step1CompensateMockAutoRetries).toHaveBeenCalledTimes(1) |
|
431
|
expect(step2CompensateMockAutoRetries).toHaveBeenCalledTimes(1) |
|
432
|
}) |
|
433
|
|
|
434
|
it("should not retry steps X times automatically when maxRetries is set and autoRetry is false", async () => { |
|
435
|
const transactionId = "transaction-auto-retries" + ulid() |
|
436
|
const workflowId = "workflow_1_auto_retries_false" |
|
437
|
|
|
438
|
await workflowOrcModule.run(workflowId, { |
|
439
|
input: {}, |
|
440
|
transactionId, |
|
441
|
throwOnError: false, |
|
442
|
}) |
|
443
|
|
|
444
|
const onFinishPromise = new Promise<void>((resolve, reject) => { |
|
445
|
workflowOrcModule.subscribe({ |
|
446
|
workflowId, |
|
447
|
transactionId, |
|
448
|
subscriber: async (event) => { |
|
449
|
if (event.eventType === "onFinish") { |
|
450
|
expect(step1InvokeMockAutoRetriesFalse).toHaveBeenCalledTimes(1) |
|
451
|
expect(step2InvokeMockAutoRetriesFalse).toHaveBeenCalledTimes(3) |
|
452
|
expect( |
|
453
|
step1CompensateMockAutoRetriesFalse |
|
454
|
).toHaveBeenCalledTimes(1) |
|
455
|
expect( |
|
456
|
step2CompensateMockAutoRetriesFalse |
|
457
|
).toHaveBeenCalledTimes(1) |
|
458
|
resolve() |
|
459
|
} |
|
460
|
}, |
|
461
|
}) |
|
462
|
}) |
|
463
|
|
|
464
|
expect(step1InvokeMockAutoRetriesFalse).toHaveBeenCalledTimes(1) |
|
465
|
expect(step2InvokeMockAutoRetriesFalse).toHaveBeenCalledTimes(1) |
|
466
|
expect(step1CompensateMockAutoRetriesFalse).toHaveBeenCalledTimes(0) |
|
467
|
expect(step2CompensateMockAutoRetriesFalse).toHaveBeenCalledTimes(0) |
|
468
|
|
|
469
|
await setTimeoutPromise(2000) |
|
470
|
|
|
471
|
await workflowOrcModule.run(workflowId, { |
|
472
|
input: {}, |
|
473
|
transactionId, |
|
474
|
throwOnError: false, |
|
475
|
}) |
|
476
|
|
|
477
|
await setTimeoutPromise(2000) |
|
478
|
|
|
479
|
expect(step1InvokeMockAutoRetriesFalse).toHaveBeenCalledTimes(1) |
|
480
|
expect(step2InvokeMockAutoRetriesFalse).toHaveBeenCalledTimes(2) |
|
481
|
expect(step1CompensateMockAutoRetriesFalse).toHaveBeenCalledTimes(0) |
|
482
|
expect(step2CompensateMockAutoRetriesFalse).toHaveBeenCalledTimes(0) |
|
483
|
|
|
484
|
await workflowOrcModule.run(workflowId, { |
|
485
|
input: {}, |
|
486
|
transactionId, |
|
487
|
throwOnError: false, |
|
488
|
}) |
|
489
|
|
|
490
|
await onFinishPromise |
|
491
|
}) |
|
492
|
|
|
493
|
it("should prevent executing twice the same workflow in perfect concurrency with the same transactionId and non idempotent and not async but retention time is set", async () => { |
|
494
|
const transactionId = "transaction_id" + ulid() |
|
495
|
const workflowId = "workflow_id" + ulid() |
|
496
|
|
|
497
|
const step1 = createStep("step1", async () => { |
|
498
|
await setTimeoutPromise(100) |
|
499
|
return new StepResponse("step1") |
|
500
|
}) |
|
501
|
|
|
502
|
createWorkflow( |
|
503
|
{ |
|
504
|
name: workflowId, |
|
505
|
retentionTime: 1000, |
|
506
|
}, |
|
507
|
function () { |
|
508
|
return new WorkflowResponse(step1()) |
|
509
|
} |
|
510
|
) |
|
511
|
|
|
512
|
const [result1, result2] = await promiseAll([ |
|
513
|
workflowOrcModule |
|
514
|
.run(workflowId, { |
|
515
|
input: {}, |
|
516
|
transactionId, |
|
517
|
}) |
|
518
|
.catch((e) => e), |
|
519
|
workflowOrcModule |
|
520
|
.run(workflowId, { |
|
521
|
input: {}, |
|
522
|
transactionId, |
|
523
|
}) |
|
524
|
.catch((e) => e), |
|
525
|
]) |
|
526
|
|
|
527
|
expect(result1.result || result2.result).toEqual("step1") |
|
528
|
expect(result2.message || result1.message).toEqual( |
|
529
|
"Transaction already started for transactionId: " + transactionId |
|
530
|
) |
|
531
|
}) |
|
532
|
|
|
533
|
it("should execute an async workflow keeping track of the event group id provided in the context", async () => { |
|
534
|
const eventGroupId = "event-group-id" |
|
535
|
const transactionId = "transaction_id" + ulid() |
|
536
|
|
|
537
|
await workflowOrcModule.run(eventGroupWorkflowId, { |
|
538
|
input: {}, |
|
539
|
transactionId, |
|
540
|
context: { |
|
541
|
eventGroupId, |
|
542
|
}, |
|
543
|
throwOnError: true, |
|
544
|
}) |
|
545
|
|
|
546
|
await workflowOrcModule.setStepSuccess({ |
|
547
|
idempotencyKey: { |
|
548
|
action: TransactionHandlerType.INVOKE, |
|
549
|
stepId: "step_1_event_group_id_background", |
|
550
|
workflowId: eventGroupWorkflowId, |
|
551
|
transactionId, |
|
552
|
}, |
|
553
|
stepResponse: { hey: "oh" }, |
|
554
|
}) |
|
555
|
|
|
556
|
// Validate context event group id |
|
557
|
expect( |
|
558
|
(workflowEventGroupIdStep1Mock.mock.calls[0] as any[])[1] |
|
559
|
).toEqual(expect.objectContaining({ eventGroupId })) |
|
560
|
expect( |
|
561
|
(workflowEventGroupIdStep2Mock.mock.calls[0] as any[])[1] |
|
562
|
).toEqual(expect.objectContaining({ eventGroupId })) |
|
563
|
}) |
|
564
|
|
|
565
|
it("should execute an async workflow keeping track of the event group id that has been auto generated", async () => { |
|
566
|
const transactionId = "transaction_id_2" + ulid() |
|
567
|
await workflowOrcModule.run(eventGroupWorkflowId, { |
|
568
|
input: {}, |
|
569
|
transactionId, |
|
570
|
throwOnError: true, |
|
571
|
}) |
|
572
|
|
|
573
|
await workflowOrcModule.setStepSuccess({ |
|
574
|
idempotencyKey: { |
|
575
|
action: TransactionHandlerType.INVOKE, |
|
576
|
stepId: "step_1_event_group_id_background", |
|
577
|
workflowId: eventGroupWorkflowId, |
|
578
|
transactionId, |
|
579
|
}, |
|
580
|
stepResponse: { hey: "oh" }, |
|
581
|
}) |
|
582
|
|
|
583
|
const generatedEventGroupId = (( |
|
584
|
workflowEventGroupIdStep1Mock.mock.calls[0] as any[] |
|
585
|
)[1] as unknown as Context)!.eventGroupId |
|
586
|
|
|
587
|
// Validate context event group id |
|
588
|
expect( |
|
589
|
(workflowEventGroupIdStep1Mock.mock.calls[0] as any[])[1] |
|
590
|
).toEqual( |
|
591
|
expect.objectContaining({ eventGroupId: generatedEventGroupId }) |
|
592
|
) |
|
593
|
expect( |
|
594
|
(workflowEventGroupIdStep2Mock.mock.calls[0] as any[])[1] |
|
595
|
).toEqual( |
|
596
|
expect.objectContaining({ eventGroupId: generatedEventGroupId }) |
|
597
|
) |
|
598
|
}) |
|
599
|
|
|
600
|
it("should compose nested workflows w/ async steps", async () => { |
|
601
|
const asyncResults: any[] = [] |
|
602
|
const mockStep1Fn = jest.fn().mockImplementation(() => { |
|
603
|
const res = { obj: "return from 1" } |
|
604
|
asyncResults.push(res) |
|
605
|
return new StepResponse(res) |
|
606
|
}) |
|
607
|
const mockStep2Fn = jest.fn().mockImplementation(async () => { |
|
608
|
await setTimeoutPromise(100) |
|
609
|
const res = { obj: "return from 2" } |
|
610
|
asyncResults.push(res) |
|
611
|
return new StepResponse(res) |
|
612
|
}) |
|
613
|
|
|
614
|
const mockStep3Fn = jest.fn().mockImplementation(() => { |
|
615
|
const res = { obj: "return from 3" } |
|
616
|
asyncResults.push(res) |
|
617
|
return new StepResponse(res) |
|
618
|
}) |
|
619
|
|
|
620
|
const step1 = createStep("step1", mockStep1Fn) |
|
621
|
const step2 = createStep( |
|
622
|
{ |
|
623
|
name: "step2", |
|
624
|
async: true, |
|
625
|
backgroundExecution: true, |
|
626
|
}, |
|
627
|
mockStep2Fn |
|
628
|
) |
|
629
|
const step3 = createStep("step3", mockStep3Fn) |
|
630
|
|
|
631
|
const wf3 = createWorkflow("workflow3", function (input) { |
|
632
|
return new WorkflowResponse(step2(input)) |
|
633
|
}) |
|
634
|
|
|
635
|
const wf2 = createWorkflow("workflow2", function (input) { |
|
636
|
const ret3 = wf3.runAsStep({ |
|
637
|
input: {}, |
|
638
|
}) |
|
639
|
return new WorkflowResponse(ret3) |
|
640
|
}) |
|
641
|
|
|
642
|
const workflowId = "workflow1" |
|
643
|
createWorkflow(workflowId, function (input) { |
|
644
|
step1(input) |
|
645
|
wf2.runAsStep({ input }) |
|
646
|
const fourth = step3({}) |
|
647
|
return new WorkflowResponse(fourth) |
|
648
|
}) |
|
649
|
|
|
650
|
asyncResults.push("begin workflow") |
|
651
|
await workflowOrcModule.run(workflowId, { |
|
652
|
input: {}, |
|
653
|
}) |
|
654
|
|
|
655
|
const onFinishPromise = new Promise<void>((resolve) => { |
|
656
|
void workflowOrcModule.subscribe({ |
|
657
|
workflowId, |
|
658
|
subscriber: (event) => { |
|
659
|
if (event.eventType === "onFinish") { |
|
660
|
expect(asyncResults).toEqual([ |
|
661
|
"begin workflow", |
|
662
|
{ obj: "return from 1" }, |
|
663
|
"returned workflow", |
|
664
|
{ obj: "return from 2" }, |
|
665
|
{ obj: "return from 3" }, |
|
666
|
]) |
|
667
|
resolve() |
|
668
|
} |
|
669
|
}, |
|
670
|
}) |
|
671
|
}) |
|
672
|
|
|
673
|
asyncResults.push("returned workflow") |
|
674
|
|
|
675
|
await onFinishPromise |
|
676
|
}) |
|
677
|
|
|
678
|
describe("Testing basic workflow", function () { |
|
679
|
beforeEach(() => { |
|
680
|
jest.clearAllMocks() |
|
681
|
}) |
|
682
|
|
|
683
|
it("should return a list of workflow executions and remove after completed when there is no retentionTime set", async () => { |
|
684
|
await workflowOrcModule.run("workflow_1", { |
|
685
|
input: { |
|
686
|
value: "123", |
|
687
|
}, |
|
688
|
throwOnError: true, |
|
689
|
}) |
|
690
|
|
|
691
|
let { data: executionsList } = await query.graph({ |
|
692
|
entity: "workflow_executions", |
|
693
|
fields: ["workflow_id", "transaction_id", "state"], |
|
694
|
}) |
|
695
|
|
|
696
|
expect(executionsList).toHaveLength(1) |
|
697
|
|
|
698
|
const { result } = await workflowOrcModule.setStepSuccess({ |
|
699
|
idempotencyKey: { |
|
700
|
action: TransactionHandlerType.INVOKE, |
|
701
|
stepId: "new_step_name", |
|
702
|
workflowId: "workflow_1", |
|
703
|
transactionId: executionsList[0].transaction_id, |
|
704
|
}, |
|
705
|
stepResponse: { uhuuuu: "yeaah!" }, |
|
706
|
}) |
|
707
|
|
|
708
|
;({ data: executionsList } = await query.graph({ |
|
709
|
entity: "workflow_executions", |
|
710
|
fields: ["id"], |
|
711
|
})) |
|
712
|
|
|
713
|
expect(executionsList).toHaveLength(0) |
|
714
|
expect(result).toEqual({ |
|
715
|
done: { |
|
716
|
inputFromSyncStep: "oh", |
|
717
|
}, |
|
718
|
}) |
|
719
|
}) |
|
720
|
|
|
721
|
it("should return a list of workflow executions and keep it saved when there is a retentionTime set", async () => { |
|
722
|
const transactionId = "transaction_1" + ulid() |
|
723
|
await workflowOrcModule.run("workflow_2", { |
|
724
|
input: { |
|
725
|
value: "123", |
|
726
|
}, |
|
727
|
throwOnError: true, |
|
728
|
transactionId, |
|
729
|
}) |
|
730
|
|
|
731
|
let { data: executionsList } = await query.graph({ |
|
732
|
entity: "workflow_executions", |
|
733
|
fields: ["id"], |
|
734
|
}) |
|
735
|
|
|
736
|
expect(executionsList).toHaveLength(1) |
|
737
|
|
|
738
|
await workflowOrcModule.setStepSuccess({ |
|
739
|
idempotencyKey: { |
|
740
|
action: TransactionHandlerType.INVOKE, |
|
741
|
stepId: "new_step_name", |
|
742
|
workflowId: "workflow_2", |
|
743
|
transactionId, |
|
744
|
}, |
|
745
|
stepResponse: { uhuuuu: "yeaah!" }, |
|
746
|
}) |
|
747
|
|
|
748
|
expect(workflow2Step2Invoke).toHaveBeenCalledTimes(2) |
|
749
|
expect(workflow2Step2Invoke.mock.calls[0][0]).toEqual({ hey: "oh" }) |
|
750
|
expect(workflow2Step2Invoke.mock.calls[1][0]).toEqual({}) |
|
751
|
|
|
752
|
expect(workflow2Step3Invoke).toHaveBeenCalledTimes(1) |
|
753
|
expect(workflow2Step3Invoke.mock.calls[0][0]).toEqual({ |
|
754
|
uhuuuu: "yeaah!", |
|
755
|
}) |
|
756
|
;({ data: executionsList } = await query.graph({ |
|
757
|
entity: "workflow_executions", |
|
758
|
fields: ["id"], |
|
759
|
})) |
|
760
|
|
|
761
|
expect(executionsList).toHaveLength(1) |
|
762
|
}) |
|
763
|
|
|
764
|
it("should return a list of workflow executions and keep it saved when there is a retentionTime set but allow for executing the same workflow multiple times with different run_id if the workflow is considered done", async () => { |
|
765
|
const transactionId = "transaction_1" + ulid() |
|
766
|
await workflowOrcModule.run( |
|
767
|
"workflow_not_idempotent_with_retention", |
|
768
|
{ |
|
769
|
input: { |
|
770
|
value: "123", |
|
771
|
}, |
|
772
|
transactionId, |
|
773
|
} |
|
774
|
) |
|
775
|
|
|
776
|
let { data: executionsList } = await query.graph({ |
|
777
|
entity: "workflow_executions", |
|
778
|
fields: ["id", "run_id", "transaction_id"], |
|
779
|
}) |
|
780
|
|
|
781
|
expect(executionsList).toHaveLength(1) |
|
782
|
|
|
783
|
expect( |
|
784
|
workflowNotIdempotentWithRetentionStep2Invoke |
|
785
|
).toHaveBeenCalledTimes(2) |
|
786
|
expect( |
|
787
|
workflowNotIdempotentWithRetentionStep2Invoke.mock.calls[0][0] |
|
788
|
).toEqual({ hey: "oh" }) |
|
789
|
expect( |
|
790
|
workflowNotIdempotentWithRetentionStep2Invoke.mock.calls[1][0] |
|
791
|
).toEqual({ |
|
792
|
hey: "hello", |
|
793
|
}) |
|
794
|
expect( |
|
795
|
workflowNotIdempotentWithRetentionStep3Invoke |
|
796
|
).toHaveBeenCalledTimes(1) |
|
797
|
expect( |
|
798
|
workflowNotIdempotentWithRetentionStep3Invoke.mock.calls[0][0] |
|
799
|
).toEqual({ |
|
800
|
notAsyncResponse: "hello", |
|
801
|
}) |
|
802
|
|
|
803
|
await workflowOrcModule.run( |
|
804
|
"workflow_not_idempotent_with_retention", |
|
805
|
{ |
|
806
|
input: { |
|
807
|
value: "123", |
|
808
|
}, |
|
809
|
transactionId, |
|
810
|
} |
|
811
|
) |
|
812
|
|
|
813
|
const { data: executionsList2 } = await query.graph({ |
|
814
|
entity: "workflow_executions", |
|
815
|
filters: { |
|
816
|
id: { $nin: executionsList.map((e) => e.id) }, |
|
817
|
}, |
|
818
|
fields: ["id", "run_id", "transaction_id"], |
|
819
|
}) |
|
820
|
|
|
821
|
expect(executionsList2).toHaveLength(1) |
|
822
|
expect(executionsList2[0].run_id).not.toEqual( |
|
823
|
executionsList[0].run_id |
|
824
|
) |
|
825
|
expect(executionsList2[0].transaction_id).toEqual( |
|
826
|
executionsList[0].transaction_id |
|
827
|
) |
|
828
|
}) |
|
829
|
|
|
830
|
it("should revert the entire transaction when a step timeout expires", async () => { |
|
831
|
const { transaction } = (await workflowOrcModule.run( |
|
832
|
"workflow_step_timeout", |
|
833
|
{ |
|
834
|
input: {}, |
|
835
|
throwOnError: false, |
|
836
|
} |
|
837
|
)) as Awaited<{ transaction: DistributedTransactionType }> |
|
838
|
|
|
839
|
expect(transaction.getFlow().state).toEqual("reverted") |
|
840
|
}) |
|
841
|
|
|
842
|
it("should revert the entire transaction when the transaction timeout expires", async () => { |
|
843
|
const { transaction } = (await workflowOrcModule.run( |
|
844
|
"workflow_transaction_timeout", |
|
845
|
{ |
|
846
|
input: {}, |
|
847
|
throwOnError: false, |
|
848
|
} |
|
849
|
)) as Awaited<{ transaction: DistributedTransactionType }> |
|
850
|
|
|
851
|
await setTimeoutPromise(200) |
|
852
|
|
|
853
|
expect(transaction.getFlow().state).toEqual("reverted") |
|
854
|
}) |
|
855
|
|
|
856
|
it("should subscribe to a async workflow and receive the response when it finishes", async () => { |
|
857
|
const transactionId = "trx_123" + ulid() |
|
858
|
|
|
859
|
const onFinishPromise = new Promise<void>((resolve) => { |
|
860
|
void workflowOrcModule.subscribe({ |
|
861
|
workflowId: "workflow_async_background", |
|
862
|
transactionId, |
|
863
|
subscriber: (event) => { |
|
864
|
if (event.eventType === "onFinish") { |
|
865
|
resolve() |
|
866
|
} |
|
867
|
}, |
|
868
|
}) |
|
869
|
}) |
|
870
|
|
|
871
|
void workflowOrcModule.run("workflow_async_background", { |
|
872
|
input: { |
|
873
|
myInput: "123", |
|
874
|
}, |
|
875
|
transactionId, |
|
876
|
throwOnError: false, |
|
877
|
}) |
|
878
|
|
|
879
|
await onFinishPromise |
|
880
|
}) |
|
881
|
|
|
882
|
it("should cancel and revert a completed workflow", async () => { |
|
883
|
const workflowId = "workflow_sync" |
|
884
|
|
|
885
|
const { acknowledgement, transaction: trx } = |
|
886
|
await workflowOrcModule.run(workflowId, { |
|
887
|
input: { |
|
888
|
value: "123", |
|
889
|
}, |
|
890
|
}) |
|
891
|
|
|
892
|
expect(trx.getFlow().state).toEqual("done") |
|
893
|
expect(acknowledgement.hasFinished).toBe(true) |
|
894
|
|
|
895
|
const { transaction } = await workflowOrcModule.cancel(workflowId, { |
|
896
|
transactionId: acknowledgement.transactionId, |
|
897
|
}) |
|
898
|
|
|
899
|
expect(transaction.getFlow().state).toEqual("reverted") |
|
900
|
}) |
|
901
|
|
|
902
|
it("should cancel and revert a non idempotent completed workflow with rentention time given a specific transaction id", async () => { |
|
903
|
const workflowId = "workflow_not_idempotent_with_retention" |
|
904
|
const transactionId = "trx_123" + ulid() |
|
905
|
|
|
906
|
await workflowOrcModule.run(workflowId, { |
|
907
|
input: { |
|
908
|
value: "123", |
|
909
|
}, |
|
910
|
transactionId, |
|
911
|
}) |
|
912
|
|
|
913
|
let executions = await workflowOrcModule.listWorkflowExecutions({ |
|
914
|
transaction_id: transactionId, |
|
915
|
}) |
|
916
|
|
|
917
|
expect(executions.length).toEqual(1) |
|
918
|
expect(executions[0].state).toEqual(TransactionState.DONE) |
|
919
|
expect(executions[0].transaction_id).toEqual(transactionId) |
|
920
|
|
|
921
|
await workflowOrcModule.cancel(workflowId, { |
|
922
|
transactionId, |
|
923
|
}) |
|
924
|
|
|
925
|
executions = await workflowOrcModule.listWorkflowExecutions({ |
|
926
|
transaction_id: transactionId, |
|
927
|
}) |
|
928
|
|
|
929
|
expect(executions.length).toEqual(1) |
|
930
|
expect(executions[0].state).toEqual(TransactionState.REVERTED) |
|
931
|
}) |
|
932
|
|
|
933
|
it("should run conditional steps if condition is true", async () => { |
|
934
|
let timeout: NodeJS.Timeout |
|
935
|
|
|
936
|
const onFinishPromise = new Promise<void>((resolve, reject) => { |
|
937
|
void workflowOrcModule.subscribe({ |
|
938
|
workflowId: "workflow_conditional_step", |
|
939
|
subscriber: (event) => { |
|
940
|
if (event.eventType === "onFinish") { |
|
941
|
resolve() |
|
942
|
} |
|
943
|
}, |
|
944
|
}) |
|
945
|
}) |
|
946
|
|
|
947
|
void workflowOrcModule.run("workflow_conditional_step", { |
|
948
|
input: { |
|
949
|
runNewStepName: true, |
|
950
|
}, |
|
951
|
throwOnError: true, |
|
952
|
}) |
|
953
|
|
|
954
|
await onFinishPromise |
|
955
|
|
|
956
|
expect(conditionalStep2Invoke).toHaveBeenCalledTimes(2) |
|
957
|
expect(conditionalStep3Invoke).toHaveBeenCalledTimes(1) |
|
958
|
}) |
|
959
|
|
|
960
|
it("should not run conditional steps if condition is false", async () => { |
|
961
|
const onFinishPromise = new Promise<void>((resolve, reject) => { |
|
962
|
void workflowOrcModule.subscribe({ |
|
963
|
workflowId: "workflow_conditional_step", |
|
964
|
subscriber: (event) => { |
|
965
|
if (event.eventType === "onFinish") { |
|
966
|
resolve() |
|
967
|
} |
|
968
|
}, |
|
969
|
}) |
|
970
|
}) |
|
971
|
|
|
972
|
workflowOrcModule.run("workflow_conditional_step", { |
|
973
|
input: { |
|
974
|
runNewStepName: false, |
|
975
|
}, |
|
976
|
throwOnError: true, |
|
977
|
}) |
|
978
|
|
|
979
|
await onFinishPromise |
|
980
|
|
|
981
|
expect(conditionalStep2Invoke).toHaveBeenCalledTimes(1) |
|
982
|
expect(conditionalStep3Invoke).toHaveBeenCalledTimes(0) |
|
983
|
}) |
|
984
|
}) |
|
985
|
|
|
986
|
describe("Scheduled workflows", () => { |
|
987
|
beforeEach(() => { |
|
988
|
jest.clearAllMocks() |
|
989
|
|
|
990
|
// Register test-value in the container for all tests |
|
991
|
const sharedContainer = |
|
992
|
workflowOrcModule["workflowOrchestratorService_"]["container_"] |
|
993
|
|
|
994
|
sharedContainer.register( |
|
995
|
"test-value", |
|
996
|
asFunction(() => "test") |
|
997
|
) |
|
998
|
}) |
|
999
|
|
|
1000
|
it("should execute a scheduled workflow", async () => { |
|
1001
|
const wait = times(2) |
|
1002
|
const spy = createScheduled("standard", wait.next) |
|
1003
|
|
|
1004
|
await wait.promise |
|
1005
|
expect(spy).toHaveBeenCalledTimes(2) |
|
1006
|
WorkflowManager.unregister("standard") |
|
1007
|
}) |
|
1008
|
|
|
1009
|
it("should stop executions after the set number of executions", async () => { |
|
1010
|
const wait = times(2) |
|
1011
|
const spy = createScheduled("num-executions", wait.next, { |
|
1012
|
interval: 1000, |
|
1013
|
numberOfExecutions: 2, |
|
1014
|
}) |
|
1015
|
|
|
1016
|
await wait.promise |
|
1017
|
expect(spy).toHaveBeenCalledTimes(2) |
|
1018
|
|
|
1019
|
// Make sure that on the next tick it doesn't execute again |
|
1020
|
await setTimeoutPromise(1100) |
|
1021
|
expect(spy).toHaveBeenCalledTimes(2) |
|
1022
|
|
|
1023
|
WorkflowManager.unregister("num-execution") |
|
1024
|
}) |
|
1025
|
|
|
1026
|
it("should remove scheduled workflow if workflow no longer exists", async () => { |
|
1027
|
const wait = times(1) |
|
1028
|
const logger = sharedContainer_.resolve<Logger>( |
|
1029
|
ContainerRegistrationKeys.LOGGER |
|
1030
|
) |
|
1031
|
|
|
1032
|
const spy = createScheduled("remove-scheduled", wait.next, { |
|
1033
|
interval: 1000, |
|
1034
|
}) |
|
1035
|
const logSpy = jest.spyOn(logger, "warn") |
|
1036
|
|
|
1037
|
await wait.promise |
|
1038
|
expect(spy).toHaveBeenCalledTimes(1) |
|
1039
|
WorkflowManager["workflows"].delete("remove-scheduled") |
|
1040
|
|
|
1041
|
await setTimeoutPromise(1100) |
|
1042
|
expect(spy).toHaveBeenCalledTimes(1) |
|
1043
|
expect(logSpy).toHaveBeenCalledWith( |
|
1044
|
"Tried to execute a scheduled workflow with ID remove-scheduled that does not exist, removing it from the scheduler." |
|
1045
|
) |
|
1046
|
}) |
|
1047
|
|
|
1048
|
it("the scheduled workflow should have access to the shared container", async () => { |
|
1049
|
const wait = times(1) |
|
1050
|
|
|
1051
|
const spy = await createScheduled("shared-container-job", wait.next, { |
|
1052
|
interval: 1000, |
|
1053
|
}) |
|
1054
|
await wait.promise |
|
1055
|
|
|
1056
|
expect(spy).toHaveBeenCalledTimes(1) |
|
1057
|
|
|
1058
|
expect(spy).toHaveReturnedWith( |
|
1059
|
expect.objectContaining({ output: { testValue: "test" } }) |
|
1060
|
) |
|
1061
|
WorkflowManager.unregister("shared-container-job") |
|
1062
|
}) |
|
1063
|
|
|
1064
|
it("should fetch an idempotent workflow after its completion", async () => { |
|
1065
|
const transactionId = "transaction_1" + ulid() |
|
1066
|
const { transaction: firstRun } = (await workflowOrcModule.run( |
|
1067
|
"workflow_idempotent", |
|
1068
|
{ |
|
1069
|
input: { |
|
1070
|
value: "123", |
|
1071
|
}, |
|
1072
|
throwOnError: true, |
|
1073
|
transactionId, |
|
1074
|
} |
|
1075
|
)) as Awaited<{ transaction: DistributedTransactionType }> |
|
1076
|
|
|
1077
|
let { data: executionsList } = await query.graph({ |
|
1078
|
entity: "workflow_executions", |
|
1079
|
fields: ["id"], |
|
1080
|
}) |
|
1081
|
|
|
1082
|
const { transaction: secondRun } = (await workflowOrcModule.run( |
|
1083
|
"workflow_idempotent", |
|
1084
|
{ |
|
1085
|
input: { |
|
1086
|
value: "123", |
|
1087
|
}, |
|
1088
|
throwOnError: true, |
|
1089
|
transactionId, |
|
1090
|
} |
|
1091
|
)) as Awaited<{ transaction: DistributedTransactionType }> |
|
1092
|
|
|
1093
|
const { data: executionsListAfter } = await query.graph({ |
|
1094
|
entity: "workflow_executions", |
|
1095
|
fields: ["id"], |
|
1096
|
}) |
|
1097
|
|
|
1098
|
expect(secondRun.getFlow().startedAt).toEqual( |
|
1099
|
firstRun.getFlow().startedAt |
|
1100
|
) |
|
1101
|
expect(executionsList).toHaveLength(1) |
|
1102
|
expect(executionsListAfter).toHaveLength(1) |
|
1103
|
}) |
|
1104
|
|
|
1105
|
it("should display error when multiple async steps are running in parallel", async () => { |
|
1106
|
let errors: Error[] = [] |
|
1107
|
const onFinishPromise = new Promise<void>((resolve) => { |
|
1108
|
void workflowOrcModule.subscribe({ |
|
1109
|
workflowId: "workflow_parallel_async", |
|
1110
|
subscriber: (event) => { |
|
1111
|
if (event.eventType === "onFinish") { |
|
1112
|
errors = event.errors |
|
1113
|
resolve() |
|
1114
|
} |
|
1115
|
}, |
|
1116
|
}) |
|
1117
|
}) |
|
1118
|
|
|
1119
|
void workflowOrcModule.run("workflow_parallel_async", { |
|
1120
|
input: {}, |
|
1121
|
throwOnError: false, |
|
1122
|
}) |
|
1123
|
|
|
1124
|
await onFinishPromise |
|
1125
|
|
|
1126
|
const errMessage = errors[0]?.error.message |
|
1127
|
expect(errMessage).toContain("Error in parallel step") |
|
1128
|
expect(errors).toEqual( |
|
1129
|
expect.arrayContaining([ |
|
1130
|
expect.objectContaining({ |
|
1131
|
action: "step_2", |
|
1132
|
handlerType: "invoke", |
|
1133
|
}), |
|
1134
|
]) |
|
1135
|
) |
|
1136
|
}) |
|
1137
|
}) |
|
1138
|
|
|
1139
|
describe("Cleaner job", function () { |
|
1140
|
it("should remove expired executions of finished workflows and keep the others", async () => { |
|
1141
|
const doneWorkflowId = "done-workflow-" + ulid() |
|
1142
|
createWorkflow({ name: doneWorkflowId, retentionTime: 1 }, () => { |
|
1143
|
return new WorkflowResponse("done") |
|
1144
|
}) |
|
1145
|
|
|
1146
|
const failingWorkflowId = "failing-workflow-" + ulid() |
|
1147
|
const failingStep = createStep("failing-step", () => { |
|
1148
|
throw new Error("I am failing") |
|
1149
|
}) |
|
1150
|
createWorkflow({ name: failingWorkflowId, retentionTime: 1 }, () => { |
|
1151
|
failingStep() |
|
1152
|
}) |
|
1153
|
|
|
1154
|
const revertingStep = createStep( |
|
1155
|
"reverting-step", |
|
1156
|
() => { |
|
1157
|
throw new Error("I am reverting") |
|
1158
|
}, |
|
1159
|
() => { |
|
1160
|
return new StepResponse("reverted") |
|
1161
|
} |
|
1162
|
) |
|
1163
|
|
|
1164
|
const revertingWorkflowId = "reverting-workflow-" + ulid() |
|
1165
|
createWorkflow( |
|
1166
|
{ name: revertingWorkflowId, retentionTime: 1 }, |
|
1167
|
() => { |
|
1168
|
revertingStep() |
|
1169
|
return new WorkflowResponse("reverted") |
|
1170
|
} |
|
1171
|
) |
|
1172
|
|
|
1173
|
const runningWorkflowId = "running-workflow-" + ulid() |
|
1174
|
const longRunningStep = createStep("long-running-step", async () => { |
|
1175
|
await setTimeoutPromise(10000) |
|
1176
|
return new StepResponse("long running finished") |
|
1177
|
}) |
|
1178
|
createWorkflow({ name: runningWorkflowId, retentionTime: 1 }, () => { |
|
1179
|
longRunningStep().config({ async: true, backgroundExecution: true }) |
|
1180
|
return new WorkflowResponse("running workflow started") |
|
1181
|
}) |
|
1182
|
|
|
1183
|
const notExpiredWorkflowId = "not-expired-workflow-" + ulid() |
|
1184
|
createWorkflow( |
|
1185
|
{ name: notExpiredWorkflowId, retentionTime: 1000 }, |
|
1186
|
() => { |
|
1187
|
return new WorkflowResponse("not expired") |
|
1188
|
} |
|
1189
|
) |
|
1190
|
|
|
1191
|
const trx_done = "trx-done-" + ulid() |
|
1192
|
const trx_failed = "trx-failed-" + ulid() |
|
1193
|
const trx_reverting = "trx-reverting-" + ulid() |
|
1194
|
const trx_running = "trx-running-" + ulid() |
|
1195
|
const trx_not_expired = "trx-not-expired-" + ulid() |
|
1196
|
|
|
1197
|
// run workflows |
|
1198
|
await workflowOrcModule.run(doneWorkflowId, { |
|
1199
|
transactionId: trx_done, |
|
1200
|
}) |
|
1201
|
|
|
1202
|
await workflowOrcModule.run(failingWorkflowId, { |
|
1203
|
transactionId: trx_failed, |
|
1204
|
throwOnError: false, |
|
1205
|
}) |
|
1206
|
|
|
1207
|
await workflowOrcModule.run(revertingWorkflowId, { |
|
1208
|
transactionId: trx_reverting, |
|
1209
|
throwOnError: false, |
|
1210
|
}) |
|
1211
|
|
|
1212
|
await workflowOrcModule.run(runningWorkflowId, { |
|
1213
|
transactionId: trx_running, |
|
1214
|
}) |
|
1215
|
|
|
1216
|
await workflowOrcModule.run(notExpiredWorkflowId, { |
|
1217
|
transactionId: trx_not_expired, |
|
1218
|
}) |
|
1219
|
|
|
1220
|
const executions = await workflowOrcModule.listWorkflowExecutions() |
|
1221
|
expect(executions).toHaveLength(5) |
|
1222
|
|
|
1223
|
await setTimeoutPromise(2000) |
|
1224
|
|
|
1225
|
// Manually trigger cleaner |
|
1226
|
await (workflowOrcModule as any).workflowOrchestratorService_[ |
|
1227
|
"inMemoryDistributedTransactionStorage_" |
|
1228
|
]["clearExpiredExecutions"]() |
|
1229
|
|
|
1230
|
let remainingExecutions = |
|
1231
|
await workflowOrcModule.listWorkflowExecutions() |
|
1232
|
|
|
1233
|
expect(remainingExecutions).toHaveLength(2) |
|
1234
|
|
|
1235
|
const remainingTrxIds = remainingExecutions |
|
1236
|
.map((e) => e.transaction_id) |
|
1237
|
.sort() |
|
1238
|
|
|
1239
|
expect(remainingTrxIds).toEqual([trx_not_expired, trx_running].sort()) |
|
1240
|
|
|
1241
|
const notExpiredExec = remainingExecutions.find( |
|
1242
|
(e) => e.transaction_id === trx_not_expired |
|
1243
|
) |
|
1244
|
expect(notExpiredExec?.state).toBe(TransactionState.DONE) |
|
1245
|
|
|
1246
|
const runningExec = remainingExecutions.find( |
|
1247
|
(e) => e.transaction_id === trx_running |
|
1248
|
) |
|
1249
|
expect(runningExec?.state).toBe(TransactionState.INVOKING) |
|
1250
|
}) |
|
1251
|
}) |
|
1252
|
}) |
|
1253
|
}, |
|
1254
|
}) |