|
1
|
import { emitEventStep } from "@medusajs/core-flows" |
|
2
|
import { Modules, TransactionState } from "@medusajs/framework/utils" |
|
3
|
import { |
|
4
|
createStep, |
|
5
|
createWorkflow, |
|
6
|
StepResponse, |
|
7
|
WorkflowData, |
|
8
|
WorkflowResponse, |
|
9
|
} from "@medusajs/framework/workflows-sdk" |
|
10
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
11
|
import { IEventBusModuleService } from "@medusajs/types" |
|
12
|
import { |
|
13
|
adminHeaders, |
|
14
|
createAdminUser, |
|
15
|
} from "../../../helpers/create-admin-user" |
|
16
|
|
|
17
|
jest.setTimeout(300000) |
|
18
|
|
|
19
|
medusaIntegrationTestRunner({ |
|
20
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
21
|
describe("Workflow Engine API", () => { |
|
22
|
let medusaContainer |
|
23
|
|
|
24
|
beforeAll(() => { |
|
25
|
medusaContainer = getContainer() |
|
26
|
}) |
|
27
|
|
|
28
|
beforeAll(async () => { |
|
29
|
await createAdminUser(dbConnection, adminHeaders, medusaContainer) |
|
30
|
|
|
31
|
await dbUtils.snapshot() |
|
32
|
}) |
|
33
|
|
|
34
|
describe("Testing WorkflowEngine.run", () => { |
|
35
|
beforeAll(async () => { |
|
36
|
const step1 = createStep( |
|
37
|
{ |
|
38
|
name: "my-step", |
|
39
|
}, |
|
40
|
async (input: { initial: string }, { container }) => { |
|
41
|
const testMod = container.resolve("testingModule") as any |
|
42
|
|
|
43
|
return new StepResponse(testMod.methodName(input.initial)) |
|
44
|
} |
|
45
|
) |
|
46
|
|
|
47
|
createWorkflow( |
|
48
|
{ |
|
49
|
name: "my-workflow-name", |
|
50
|
}, |
|
51
|
function (input: WorkflowData<{ initial: string }>) { |
|
52
|
const stepRes = step1(input) |
|
53
|
|
|
54
|
return new WorkflowResponse(stepRes) |
|
55
|
} |
|
56
|
) |
|
57
|
}) |
|
58
|
|
|
59
|
it("Should invoke modules passing the current medusa context as argument", async () => { |
|
60
|
const testMod = medusaContainer.resolve("testingModule") as any |
|
61
|
|
|
62
|
const methodSpy = jest.spyOn(testMod, "methodName") |
|
63
|
|
|
64
|
const engine = medusaContainer.resolve(Modules.WORKFLOW_ENGINE) |
|
65
|
|
|
66
|
const res = await engine.run("my-workflow-name", { |
|
67
|
transactionId: "trx-id", |
|
68
|
input: { |
|
69
|
initial: "abc", |
|
70
|
}, |
|
71
|
context: { |
|
72
|
meta: { |
|
73
|
myStuff: "myStuff", |
|
74
|
}, |
|
75
|
}, |
|
76
|
}) |
|
77
|
|
|
78
|
expect(res.result).toEqual("abc called") |
|
79
|
|
|
80
|
expect(methodSpy).toHaveBeenCalledTimes(1) |
|
81
|
expect(methodSpy).toHaveBeenCalledWith( |
|
82
|
"abc", |
|
83
|
expect.objectContaining({ |
|
84
|
transactionId: "trx-id", |
|
85
|
__type: "MedusaContext", |
|
86
|
eventGroupId: expect.any(String), |
|
87
|
idempotencyKey: "my-workflow-name:trx-id:my-step:invoke", |
|
88
|
meta: { |
|
89
|
myStuff: "myStuff", |
|
90
|
}, |
|
91
|
}) |
|
92
|
) |
|
93
|
}) |
|
94
|
}) |
|
95
|
|
|
96
|
describe("Workflows event", () => { |
|
97
|
const failingEventName = "failing-event" |
|
98
|
|
|
99
|
it("should not compensate the workflow if the event subscriber fails", async () => { |
|
100
|
const step1 = createStep( |
|
101
|
{ |
|
102
|
name: "my-step", |
|
103
|
}, |
|
104
|
async (_) => { |
|
105
|
return new StepResponse({ result: "success" }) |
|
106
|
} |
|
107
|
) |
|
108
|
|
|
109
|
createWorkflow( |
|
110
|
{ |
|
111
|
name: "my-workflow-name-2", |
|
112
|
retentionTime: 50, |
|
113
|
}, |
|
114
|
function (input: WorkflowData<{ initial: string }>) { |
|
115
|
const stepRes = step1() |
|
116
|
|
|
117
|
emitEventStep({ |
|
118
|
eventName: failingEventName, |
|
119
|
data: { |
|
120
|
input: stepRes, |
|
121
|
}, |
|
122
|
}) |
|
123
|
|
|
124
|
return new WorkflowResponse(stepRes) |
|
125
|
} |
|
126
|
) |
|
127
|
|
|
128
|
const container = getContainer() |
|
129
|
const eventBus = container.resolve( |
|
130
|
Modules.EVENT_BUS |
|
131
|
) as IEventBusModuleService |
|
132
|
|
|
133
|
const eventSpy = jest.fn() |
|
134
|
eventBus.subscribe(failingEventName, async (event) => { |
|
135
|
eventSpy(event) |
|
136
|
throw new Error("Failed to emit event") |
|
137
|
}) |
|
138
|
|
|
139
|
const engine = container.resolve(Modules.WORKFLOW_ENGINE) |
|
140
|
|
|
141
|
const transactionId = "trx-id-failing-event" |
|
142
|
const res = await engine.run("my-workflow-name-2", { |
|
143
|
transactionId, |
|
144
|
input: { |
|
145
|
initial: "abc", |
|
146
|
}, |
|
147
|
}) |
|
148
|
|
|
149
|
expect(res.result).toEqual({ result: "success" }) |
|
150
|
|
|
151
|
const executions = await engine.listWorkflowExecutions({ |
|
152
|
transaction_id: transactionId, |
|
153
|
}) |
|
154
|
|
|
155
|
expect(executions.length).toBe(1) |
|
156
|
expect(executions[0].state).toBe(TransactionState.DONE) |
|
157
|
|
|
158
|
expect(eventSpy).toHaveBeenCalledTimes(1) |
|
159
|
expect(eventSpy).toHaveBeenCalledWith( |
|
160
|
expect.objectContaining({ |
|
161
|
data: { |
|
162
|
input: { |
|
163
|
result: "success", |
|
164
|
}, |
|
165
|
}, |
|
166
|
}) |
|
167
|
) |
|
168
|
}) |
|
169
|
}) |
|
170
|
|
|
171
|
describe("Step timeout with continueOnPermanentFailure", () => { |
|
172
|
it("should continue the workflow when an async step with retryIntervalAwaiting reaches timeout and continueOnPermanentFailure is set", async () => { |
|
173
|
const invokeSpy = jest.fn() |
|
174
|
const invokeSpyB = jest.fn() |
|
175
|
|
|
176
|
const step1 = createStep( |
|
177
|
{ |
|
178
|
name: "step-sync", |
|
179
|
}, |
|
180
|
async () => { |
|
181
|
return new StepResponse({ result: "from-step-1" }) |
|
182
|
} |
|
183
|
) |
|
184
|
|
|
185
|
const step2b = createStep( |
|
186
|
{ |
|
187
|
name: "step-async-timeout-b", |
|
188
|
async: true, |
|
189
|
timeout: 1, |
|
190
|
retryIntervalAwaiting: 0.3, |
|
191
|
maxAwaitingRetries: 2, |
|
192
|
continueOnPermanentFailure: true, |
|
193
|
}, |
|
194
|
async () => { |
|
195
|
invokeSpyB() |
|
196
|
} |
|
197
|
) |
|
198
|
|
|
199
|
const step2 = createStep( |
|
200
|
{ |
|
201
|
name: "step-async-timeout", |
|
202
|
async: true, |
|
203
|
timeout: 1, |
|
204
|
retryIntervalAwaiting: 0.3, |
|
205
|
maxAwaitingRetries: 2, |
|
206
|
continueOnPermanentFailure: true, |
|
207
|
noCompensation: true, |
|
208
|
}, |
|
209
|
async () => { |
|
210
|
invokeSpy() |
|
211
|
} |
|
212
|
) |
|
213
|
|
|
214
|
const step3 = createStep( |
|
215
|
{ |
|
216
|
name: "step-after-timeout", |
|
217
|
}, |
|
218
|
async () => { |
|
219
|
return new StepResponse({ result: "from-step-3" }) |
|
220
|
} |
|
221
|
) |
|
222
|
|
|
223
|
createWorkflow( |
|
224
|
{ |
|
225
|
name: "wf-timeout-continue", |
|
226
|
retentionTime: 50, |
|
227
|
}, |
|
228
|
function () { |
|
229
|
step1() |
|
230
|
step2() |
|
231
|
step2b() |
|
232
|
const res3 = step3() |
|
233
|
return new WorkflowResponse(res3) |
|
234
|
} |
|
235
|
) |
|
236
|
|
|
237
|
const container = getContainer() |
|
238
|
const engine = container.resolve(Modules.WORKFLOW_ENGINE) |
|
239
|
|
|
240
|
const transactionId = "trx-timeout-continue" |
|
241
|
const { transaction } = await engine.run("wf-timeout-continue", { |
|
242
|
transactionId, |
|
243
|
throwOnError: false, |
|
244
|
}) |
|
245
|
|
|
246
|
// The workflow should be waiting for the async step |
|
247
|
expect(transaction.getState()).toBe(TransactionState.INVOKING) |
|
248
|
|
|
249
|
// Wait for the step timeout to fire plus some buffer |
|
250
|
await new Promise((resolve) => global.setTimeout(resolve, 4000)) |
|
251
|
|
|
252
|
const executions = await engine.listWorkflowExecutions({ |
|
253
|
transaction_id: transactionId, |
|
254
|
}) |
|
255
|
|
|
256
|
expect(executions.length).toBe(1) |
|
257
|
expect(executions[0].state).toBe(TransactionState.DONE) |
|
258
|
|
|
259
|
expect(invokeSpy).toHaveBeenCalled() |
|
260
|
expect(invokeSpyB).toHaveBeenCalled() |
|
261
|
}) |
|
262
|
}) |
|
263
|
}) |
|
264
|
}, |
|
265
|
}) |