@tsed/bullmq 8.11.1 → 8.11.2-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/BullMQModule.js +0 -1
- package/lib/esm/config/config.js +0 -1
- package/lib/esm/constants/BullMQTypes.js +0 -1
- package/lib/esm/constants/constants.js +0 -1
- package/lib/esm/contracts/JobMethods.js +0 -1
- package/lib/esm/contracts/JobStore.js +0 -1
- package/lib/esm/contracts/index.js +0 -1
- package/lib/esm/decorators/InjectQueue.js +0 -1
- package/lib/esm/decorators/InjectWorker.js +0 -1
- package/lib/esm/decorators/JobController.js +0 -1
- package/lib/esm/decorators/index.js +0 -1
- package/lib/esm/dispatchers/JobDispatcher.js +0 -1
- package/lib/esm/dispatchers/JobDispatcherOptions.js +0 -1
- package/lib/esm/dispatchers/index.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/utils/createQueueProvider.js +0 -1
- package/lib/esm/utils/createWorkerProvider.js +0 -1
- package/lib/esm/utils/getJobToken.js +0 -1
- package/lib/esm/utils/getQueueToken.js +0 -1
- package/lib/esm/utils/getWorkerToken.js +0 -1
- package/lib/esm/utils/mapQueueOptions.js +0 -1
- package/lib/esm/utils/mapWorkerOptions.js +0 -1
- package/package.json +11 -8
- package/.barrelsby.json +0 -5
- package/lib/esm/BullMQModule.js.map +0 -1
- package/lib/esm/config/config.js.map +0 -1
- package/lib/esm/constants/BullMQTypes.js.map +0 -1
- package/lib/esm/constants/constants.js.map +0 -1
- package/lib/esm/contracts/JobMethods.js.map +0 -1
- package/lib/esm/contracts/JobStore.js.map +0 -1
- package/lib/esm/contracts/index.js.map +0 -1
- package/lib/esm/decorators/InjectQueue.js.map +0 -1
- package/lib/esm/decorators/InjectWorker.js.map +0 -1
- package/lib/esm/decorators/JobController.js.map +0 -1
- package/lib/esm/decorators/index.js.map +0 -1
- package/lib/esm/dispatchers/JobDispatcher.js.map +0 -1
- package/lib/esm/dispatchers/JobDispatcherOptions.js.map +0 -1
- package/lib/esm/dispatchers/index.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/utils/createQueueProvider.js.map +0 -1
- package/lib/esm/utils/createWorkerProvider.js.map +0 -1
- package/lib/esm/utils/getJobToken.js.map +0 -1
- package/lib/esm/utils/getQueueToken.js.map +0 -1
- package/lib/esm/utils/getWorkerToken.js.map +0 -1
- package/lib/esm/utils/mapQueueOptions.js.map +0 -1
- package/lib/esm/utils/mapWorkerOptions.js.map +0 -1
- package/src/BullMQModule.spec.ts +0 -482
- package/src/BullMQModule.ts +0 -151
- package/src/config/config.ts +0 -59
- package/src/constants/BullMQTypes.ts +0 -7
- package/src/constants/constants.ts +0 -1
- package/src/contracts/JobMethods.ts +0 -7
- package/src/contracts/JobStore.ts +0 -7
- package/src/contracts/index.ts +0 -5
- package/src/decorators/InjectQueue.ts +0 -7
- package/src/decorators/InjectWorker.ts +0 -7
- package/src/decorators/JobController.spec.ts +0 -94
- package/src/decorators/JobController.ts +0 -33
- package/src/decorators/index.ts +0 -6
- package/src/dispatchers/JobDispatcher.spec.ts +0 -169
- package/src/dispatchers/JobDispatcher.ts +0 -78
- package/src/dispatchers/JobDispatcherOptions.ts +0 -4
- package/src/dispatchers/index.ts +0 -5
- package/src/index.ts +0 -24
- package/src/utils/createQueueProvider.ts +0 -16
- package/src/utils/createWorkerProvider.ts +0 -18
- package/src/utils/getJobToken.ts +0 -7
- package/src/utils/getQueueToken.ts +0 -3
- package/src/utils/getWorkerToken.ts +0 -3
- package/src/utils/mapQueueOptions.ts +0 -14
- package/src/utils/mapWorkerOptions.ts +0 -14
- package/vitest.config.mts +0 -26
package/src/BullMQModule.spec.ts
DELETED
|
@@ -1,482 +0,0 @@
|
|
|
1
|
-
import "./BullMQModule.js";
|
|
2
|
-
|
|
3
|
-
import {catchAsyncError} from "@tsed/core";
|
|
4
|
-
import {logger} from "@tsed/di";
|
|
5
|
-
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
6
|
-
import {Queue, Worker} from "bullmq";
|
|
7
|
-
import {anything, instance, mock, verify, when} from "ts-mockito";
|
|
8
|
-
import {beforeEach} from "vitest";
|
|
9
|
-
|
|
10
|
-
import {BullMQModule} from "./BullMQModule.js";
|
|
11
|
-
import {type BullMQConfig} from "./config/config.js";
|
|
12
|
-
import {JobMethods} from "./contracts/index.js";
|
|
13
|
-
import {FallbackJobController, JobController} from "./decorators/index.js";
|
|
14
|
-
import {InjectQueue} from "./decorators/InjectQueue.js";
|
|
15
|
-
import {InjectWorker} from "./decorators/InjectWorker.js";
|
|
16
|
-
import {JobDispatcher} from "./dispatchers/index.js";
|
|
17
|
-
|
|
18
|
-
const queueConstructorSpy = vi.fn();
|
|
19
|
-
const workerConstructorSpy = vi.fn();
|
|
20
|
-
|
|
21
|
-
vi.spyOn(logger(), "error");
|
|
22
|
-
|
|
23
|
-
vi.mock("bullmq", () => {
|
|
24
|
-
return {
|
|
25
|
-
Queue: class {
|
|
26
|
-
constructor(...args: any[]) {
|
|
27
|
-
queueConstructorSpy(...args);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
close() {}
|
|
31
|
-
},
|
|
32
|
-
Worker: class {
|
|
33
|
-
constructor(...args: any[]) {
|
|
34
|
-
workerConstructorSpy(...args);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
close() {}
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
@JobController("cron", "default", {
|
|
43
|
-
repeat: {
|
|
44
|
-
pattern: "* * * * *"
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
class CustomCronJob implements JobMethods {
|
|
48
|
-
@InjectQueue("default")
|
|
49
|
-
queue: Queue;
|
|
50
|
-
|
|
51
|
-
@InjectWorker("default")
|
|
52
|
-
worker: Worker;
|
|
53
|
-
|
|
54
|
-
handle() {}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
@JobController("regular", "default")
|
|
58
|
-
class RegularJob {
|
|
59
|
-
handle() {}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
describe("BullMQModule", () => {
|
|
63
|
-
let dispatcher: JobDispatcher;
|
|
64
|
-
|
|
65
|
-
beforeEach(() => {
|
|
66
|
-
dispatcher = mock(JobDispatcher);
|
|
67
|
-
when(dispatcher.dispatch(CustomCronJob)).thenResolve();
|
|
68
|
-
});
|
|
69
|
-
beforeEach(() => {
|
|
70
|
-
queueConstructorSpy.mockClear();
|
|
71
|
-
workerConstructorSpy.mockClear();
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
afterEach(PlatformTest.reset);
|
|
75
|
-
|
|
76
|
-
describe("configuration", () => {
|
|
77
|
-
describe("merges config correctly", () => {
|
|
78
|
-
beforeEach(() =>
|
|
79
|
-
PlatformTest.create({
|
|
80
|
-
bullmq: {
|
|
81
|
-
queues: ["default", "special"],
|
|
82
|
-
connection: {
|
|
83
|
-
connectionName: "defaultConnectionName"
|
|
84
|
-
},
|
|
85
|
-
defaultQueueOptions: {
|
|
86
|
-
defaultJobOptions: {
|
|
87
|
-
delay: 100
|
|
88
|
-
},
|
|
89
|
-
blockingConnection: true
|
|
90
|
-
},
|
|
91
|
-
queueOptions: {
|
|
92
|
-
special: {
|
|
93
|
-
connection: {
|
|
94
|
-
connectionName: "specialConnectionName"
|
|
95
|
-
},
|
|
96
|
-
defaultJobOptions: {
|
|
97
|
-
attempts: 9
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
defaultWorkerOptions: {
|
|
102
|
-
connection: {
|
|
103
|
-
connectTimeout: 123
|
|
104
|
-
},
|
|
105
|
-
concurrency: 50
|
|
106
|
-
},
|
|
107
|
-
workerOptions: {
|
|
108
|
-
special: {
|
|
109
|
-
concurrency: 1,
|
|
110
|
-
lockDuration: 2
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
imports: [
|
|
115
|
-
{
|
|
116
|
-
token: JobDispatcher,
|
|
117
|
-
use: instance(dispatcher)
|
|
118
|
-
}
|
|
119
|
-
]
|
|
120
|
-
})
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
it("queue", () => {
|
|
124
|
-
expect(queueConstructorSpy).toHaveBeenCalledTimes(2);
|
|
125
|
-
|
|
126
|
-
expect(queueConstructorSpy).toHaveBeenNthCalledWith(1, "default", {
|
|
127
|
-
connection: {
|
|
128
|
-
connectionName: "defaultConnectionName"
|
|
129
|
-
},
|
|
130
|
-
defaultJobOptions: {
|
|
131
|
-
delay: 100
|
|
132
|
-
},
|
|
133
|
-
blockingConnection: true
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
expect(queueConstructorSpy).toHaveBeenNthCalledWith(2, "special", {
|
|
137
|
-
connection: {
|
|
138
|
-
connectionName: "specialConnectionName"
|
|
139
|
-
},
|
|
140
|
-
defaultJobOptions: {
|
|
141
|
-
attempts: 9,
|
|
142
|
-
delay: 100
|
|
143
|
-
},
|
|
144
|
-
blockingConnection: true
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it("worker", () => {
|
|
149
|
-
expect(workerConstructorSpy).toHaveBeenCalledTimes(2);
|
|
150
|
-
|
|
151
|
-
expect(workerConstructorSpy).toHaveBeenNthCalledWith(1, "default", expect.any(Function), {
|
|
152
|
-
connection: {
|
|
153
|
-
connectTimeout: 123
|
|
154
|
-
},
|
|
155
|
-
concurrency: 50
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
expect(workerConstructorSpy).toHaveBeenNthCalledWith(2, "special", expect.any(Function), {
|
|
159
|
-
connection: {
|
|
160
|
-
connectTimeout: 123
|
|
161
|
-
},
|
|
162
|
-
concurrency: 1,
|
|
163
|
-
lockDuration: 2
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
describe("discover queues from decorators", () => {
|
|
168
|
-
beforeEach(() =>
|
|
169
|
-
PlatformTest.create({
|
|
170
|
-
bullmq: {
|
|
171
|
-
queues: ["special"],
|
|
172
|
-
connection: {
|
|
173
|
-
connectionName: "defaultConnectionName"
|
|
174
|
-
},
|
|
175
|
-
defaultQueueOptions: {
|
|
176
|
-
defaultJobOptions: {
|
|
177
|
-
delay: 100
|
|
178
|
-
},
|
|
179
|
-
blockingConnection: true
|
|
180
|
-
},
|
|
181
|
-
queueOptions: {
|
|
182
|
-
special: {
|
|
183
|
-
connection: {
|
|
184
|
-
connectionName: "specialConnectionName"
|
|
185
|
-
},
|
|
186
|
-
defaultJobOptions: {
|
|
187
|
-
attempts: 9
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
defaultWorkerOptions: {
|
|
192
|
-
connection: {
|
|
193
|
-
connectTimeout: 123
|
|
194
|
-
},
|
|
195
|
-
concurrency: 50
|
|
196
|
-
},
|
|
197
|
-
workerOptions: {
|
|
198
|
-
special: {
|
|
199
|
-
concurrency: 1,
|
|
200
|
-
lockDuration: 2
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
imports: [
|
|
205
|
-
{
|
|
206
|
-
token: JobDispatcher,
|
|
207
|
-
use: instance(dispatcher)
|
|
208
|
-
}
|
|
209
|
-
]
|
|
210
|
-
})
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
it("queue", () => {
|
|
214
|
-
expect(queueConstructorSpy).toHaveBeenCalledTimes(2);
|
|
215
|
-
|
|
216
|
-
expect(queueConstructorSpy).toHaveBeenNthCalledWith(1, "default", {
|
|
217
|
-
connection: {
|
|
218
|
-
connectionName: "defaultConnectionName"
|
|
219
|
-
},
|
|
220
|
-
defaultJobOptions: {
|
|
221
|
-
delay: 100
|
|
222
|
-
},
|
|
223
|
-
blockingConnection: true
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
expect(queueConstructorSpy).toHaveBeenNthCalledWith(2, "special", {
|
|
227
|
-
connection: {
|
|
228
|
-
connectionName: "specialConnectionName"
|
|
229
|
-
},
|
|
230
|
-
defaultJobOptions: {
|
|
231
|
-
attempts: 9,
|
|
232
|
-
delay: 100
|
|
233
|
-
},
|
|
234
|
-
blockingConnection: true
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
it("worker", () => {
|
|
239
|
-
expect(workerConstructorSpy).toHaveBeenCalledTimes(2);
|
|
240
|
-
|
|
241
|
-
expect(workerConstructorSpy).toHaveBeenNthCalledWith(1, "default", expect.any(Function), {
|
|
242
|
-
connection: {
|
|
243
|
-
connectTimeout: 123
|
|
244
|
-
},
|
|
245
|
-
concurrency: 50
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
expect(workerConstructorSpy).toHaveBeenNthCalledWith(2, "special", expect.any(Function), {
|
|
249
|
-
connection: {
|
|
250
|
-
connectTimeout: 123
|
|
251
|
-
},
|
|
252
|
-
concurrency: 1,
|
|
253
|
-
lockDuration: 2
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
describe("disableWorker", () => {
|
|
258
|
-
const config = {
|
|
259
|
-
queues: ["default", "foo", "bar"],
|
|
260
|
-
connection: {},
|
|
261
|
-
disableWorker: true
|
|
262
|
-
} as BullMQConfig;
|
|
263
|
-
|
|
264
|
-
beforeEach(() =>
|
|
265
|
-
PlatformTest.create({
|
|
266
|
-
bullmq: config,
|
|
267
|
-
imports: [
|
|
268
|
-
{
|
|
269
|
-
token: JobDispatcher,
|
|
270
|
-
use: instance(dispatcher)
|
|
271
|
-
}
|
|
272
|
-
]
|
|
273
|
-
})
|
|
274
|
-
);
|
|
275
|
-
|
|
276
|
-
it("should not create any workers", () => {
|
|
277
|
-
expect(workerConstructorSpy).toHaveBeenCalledTimes(0);
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
describe("without", () => {
|
|
281
|
-
beforeEach(() =>
|
|
282
|
-
PlatformTest.create({
|
|
283
|
-
imports: [
|
|
284
|
-
{
|
|
285
|
-
token: JobDispatcher,
|
|
286
|
-
use: instance(dispatcher)
|
|
287
|
-
}
|
|
288
|
-
]
|
|
289
|
-
})
|
|
290
|
-
);
|
|
291
|
-
it("skips initialization", async () => {
|
|
292
|
-
expect(queueConstructorSpy).not.toHaveBeenCalled();
|
|
293
|
-
verify(dispatcher.dispatch(anything())).never();
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
describe("functionality", () => {
|
|
299
|
-
const config = {
|
|
300
|
-
queues: ["default", "foo", "bar"],
|
|
301
|
-
connection: {},
|
|
302
|
-
workerQueues: ["default", "foo"]
|
|
303
|
-
} as BullMQConfig;
|
|
304
|
-
|
|
305
|
-
beforeEach(() =>
|
|
306
|
-
PlatformTest.create({
|
|
307
|
-
bullmq: config,
|
|
308
|
-
imports: [
|
|
309
|
-
{
|
|
310
|
-
token: JobDispatcher,
|
|
311
|
-
use: instance(dispatcher)
|
|
312
|
-
}
|
|
313
|
-
]
|
|
314
|
-
})
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
describe("cronjobs", () => {
|
|
318
|
-
it("should dispatch cron jobs automatically", () => {
|
|
319
|
-
verify(dispatcher.dispatch(CustomCronJob)).once();
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
describe("queues", () => {
|
|
324
|
-
it("should get default", () => {
|
|
325
|
-
const instance = PlatformTest.get<Queue>("bullmq.queue.default");
|
|
326
|
-
|
|
327
|
-
expect(instance).toBeInstanceOf(Queue);
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
it.each(config.queues!)("should register queue(%s)", (queue) => {
|
|
331
|
-
const instance = PlatformTest.get<Queue>(`bullmq.queue.${queue}`);
|
|
332
|
-
|
|
333
|
-
expect(instance).toBeInstanceOf(Queue);
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
describe("workers", () => {
|
|
338
|
-
it("should get default", () => {
|
|
339
|
-
const instance = PlatformTest.get<Worker>("bullmq.worker.default");
|
|
340
|
-
|
|
341
|
-
expect(instance).toBeInstanceOf(Worker);
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
it.each(config.workerQueues!)("should register worker(%s)", (queue) => {
|
|
345
|
-
const instance = PlatformTest.get<Worker>(`bullmq.worker.${queue}`);
|
|
346
|
-
|
|
347
|
-
expect(instance).toBeInstanceOf(Worker);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it("should not register unspecified worker queue", () => {
|
|
351
|
-
expect(PlatformTest.get("bullmq.worker.bar")).toBeUndefined();
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
it("should run worker and execute processor", async () => {
|
|
355
|
-
const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
|
|
356
|
-
const worker = PlatformTest.get<JobMethods>("bullmq.job.default.regular");
|
|
357
|
-
const job = {
|
|
358
|
-
name: "regular",
|
|
359
|
-
queueName: "default",
|
|
360
|
-
data: {test: "test"}
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
vi.spyOn(worker, "handle").mockResolvedValueOnce(undefined as never);
|
|
364
|
-
|
|
365
|
-
await (bullMQModule as any).onProcess(job);
|
|
366
|
-
|
|
367
|
-
expect(worker.handle).toHaveBeenCalledWith({test: "test"}, job);
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
it("should log warning when the worker doesn't exists", async () => {
|
|
371
|
-
const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
|
|
372
|
-
|
|
373
|
-
vi.spyOn(PlatformTest.injector.logger, "warn");
|
|
374
|
-
|
|
375
|
-
const job = {
|
|
376
|
-
name: "regular",
|
|
377
|
-
queueName: "toto",
|
|
378
|
-
data: {test: "test"}
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
await (bullMQModule as any).onProcess(job);
|
|
382
|
-
|
|
383
|
-
expect(PlatformTest.injector.logger.warn).toHaveBeenCalledWith({
|
|
384
|
-
event: "BULLMQ_JOB_NOT_FOUND",
|
|
385
|
-
message: "Job regular toto not found"
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
it("should run worker, execute processor and handle error", async () => {
|
|
390
|
-
vi.spyOn(logger(), "error").mockReturnThis();
|
|
391
|
-
|
|
392
|
-
logger().level = "error";
|
|
393
|
-
|
|
394
|
-
const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
|
|
395
|
-
const worker = PlatformTest.get<JobMethods>("bullmq.job.default.regular");
|
|
396
|
-
const job = {
|
|
397
|
-
name: "regular",
|
|
398
|
-
queueName: "default",
|
|
399
|
-
data: {test: "test"},
|
|
400
|
-
attemptsMade: 1
|
|
401
|
-
};
|
|
402
|
-
|
|
403
|
-
vi.spyOn(worker, "handle").mockRejectedValue(new Error("error") as never);
|
|
404
|
-
|
|
405
|
-
const error = await catchAsyncError(() => (bullMQModule as any).onProcess(job));
|
|
406
|
-
|
|
407
|
-
expect(worker.handle).toHaveBeenCalledWith({test: "test"}, job);
|
|
408
|
-
expect(logger().error).toHaveBeenCalledWith({
|
|
409
|
-
attempt: 1,
|
|
410
|
-
name: "regular",
|
|
411
|
-
queue: "default",
|
|
412
|
-
logType: "bullmq",
|
|
413
|
-
duration: expect.any(Number),
|
|
414
|
-
event: "BULLMQ_JOB_ERROR",
|
|
415
|
-
message: "error",
|
|
416
|
-
reqId: expect.any(String),
|
|
417
|
-
stack: expect.any(String),
|
|
418
|
-
time: expect.any(Object)
|
|
419
|
-
});
|
|
420
|
-
expect(error?.message).toEqual("error");
|
|
421
|
-
});
|
|
422
|
-
});
|
|
423
|
-
});
|
|
424
|
-
describe("with fallback controller", () => {
|
|
425
|
-
beforeEach(async () => {
|
|
426
|
-
@FallbackJobController("foo")
|
|
427
|
-
class FooFallbackController {
|
|
428
|
-
handle() {}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
@FallbackJobController()
|
|
432
|
-
class FallbackController {
|
|
433
|
-
handle() {}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
await PlatformTest.create({
|
|
437
|
-
bullmq: {
|
|
438
|
-
queues: ["default", "foo"],
|
|
439
|
-
connection: {}
|
|
440
|
-
},
|
|
441
|
-
imports: [
|
|
442
|
-
{
|
|
443
|
-
token: JobDispatcher,
|
|
444
|
-
use: instance(dispatcher)
|
|
445
|
-
}
|
|
446
|
-
]
|
|
447
|
-
});
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
it("should run queue specific fallback job controller", async () => {
|
|
451
|
-
const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
|
|
452
|
-
const worker = PlatformTest.get<JobMethods>("bullmq.fallback-job.foo");
|
|
453
|
-
const job = {
|
|
454
|
-
name: "unknown-name",
|
|
455
|
-
queueName: "foo",
|
|
456
|
-
data: {test: "test"}
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
vi.spyOn(worker, "handle").mockResolvedValueOnce(undefined as never);
|
|
460
|
-
|
|
461
|
-
await (bullMQModule as any).onProcess(job);
|
|
462
|
-
|
|
463
|
-
expect(worker.handle).toHaveBeenCalledWith({test: "test"}, job);
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
it("should run overall fallback job controller", async () => {
|
|
467
|
-
const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
|
|
468
|
-
const worker = PlatformTest.get<JobMethods>("bullmq.fallback-job");
|
|
469
|
-
const job = {
|
|
470
|
-
name: "unknown-name",
|
|
471
|
-
queueName: "default",
|
|
472
|
-
data: {test: "123"}
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
vi.spyOn(worker, "handle").mockResolvedValueOnce(undefined as never);
|
|
476
|
-
|
|
477
|
-
await (bullMQModule as any).onProcess(job);
|
|
478
|
-
|
|
479
|
-
expect(worker.handle).toHaveBeenCalledWith({test: "123"}, job);
|
|
480
|
-
});
|
|
481
|
-
});
|
|
482
|
-
});
|
package/src/BullMQModule.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
constant,
|
|
3
|
-
DIContext,
|
|
4
|
-
inject,
|
|
5
|
-
injectable,
|
|
6
|
-
injectMany,
|
|
7
|
-
injector,
|
|
8
|
-
logger,
|
|
9
|
-
OnDestroy,
|
|
10
|
-
type OnInit,
|
|
11
|
-
ProviderType,
|
|
12
|
-
runInContext
|
|
13
|
-
} from "@tsed/di";
|
|
14
|
-
import {getComputedType} from "@tsed/schema";
|
|
15
|
-
import {Job, Queue, Worker} from "bullmq";
|
|
16
|
-
import {v4} from "uuid";
|
|
17
|
-
|
|
18
|
-
import {BullMQConfig} from "./config/config.js";
|
|
19
|
-
import {BullMQTypes} from "./constants/BullMQTypes.js";
|
|
20
|
-
import {BULLMQ} from "./constants/constants.js";
|
|
21
|
-
import {JobMethods, JobStore} from "./contracts/index.js";
|
|
22
|
-
import {JobDispatcher} from "./dispatchers/index.js";
|
|
23
|
-
import {createQueueProvider} from "./utils/createQueueProvider.js";
|
|
24
|
-
import {createWorkerProvider} from "./utils/createWorkerProvider.js";
|
|
25
|
-
import {getFallbackJobToken, getJobToken} from "./utils/getJobToken.js";
|
|
26
|
-
import {mapQueueOptions} from "./utils/mapQueueOptions.js";
|
|
27
|
-
import {mapWorkerOptions} from "./utils/mapWorkerOptions.js";
|
|
28
|
-
|
|
29
|
-
export class BullMQModule implements OnInit, OnDestroy {
|
|
30
|
-
private readonly dispatcher = inject(JobDispatcher);
|
|
31
|
-
|
|
32
|
-
constructor() {
|
|
33
|
-
// build providers allow @Inject(queue) usage in JobController instance
|
|
34
|
-
if (this.isEnabled()) {
|
|
35
|
-
const queues = [...this.getUniqQueueNames()];
|
|
36
|
-
|
|
37
|
-
this.buildQueues(queues);
|
|
38
|
-
|
|
39
|
-
if (!this.isWorkerEnabled()) {
|
|
40
|
-
const workers = this.config.workerQueues?.length ? this.config.workerQueues : queues;
|
|
41
|
-
|
|
42
|
-
this.buildWorkers(workers);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get config() {
|
|
48
|
-
return constant<BullMQConfig>("bullmq")!;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
$onInit() {
|
|
52
|
-
if (this.isEnabled()) {
|
|
53
|
-
injectMany<JobMethods>(BullMQTypes.CRON).map((job) => this.dispatcher.dispatch(getComputedType(job)));
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async $onDestroy() {
|
|
58
|
-
if (!this.isEnabled()) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
await Promise.all(injectMany<Queue>(BullMQTypes.QUEUE).map((queue) => queue.close()));
|
|
63
|
-
await Promise.all(injectMany<Worker>(BullMQTypes.WORKER).map((worker) => worker.close()));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
isEnabled() {
|
|
67
|
-
return !!this.config;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
isWorkerEnabled() {
|
|
71
|
-
return this.config.disableWorker;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private buildQueues(queues: string[]) {
|
|
75
|
-
queues.forEach((queue) => {
|
|
76
|
-
const opts = mapQueueOptions(queue, this.config);
|
|
77
|
-
createQueueProvider(queue, opts);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private buildWorkers(workers: string[]) {
|
|
82
|
-
workers.forEach((worker) => {
|
|
83
|
-
const opts = mapWorkerOptions(worker, this.config);
|
|
84
|
-
createWorkerProvider(worker, this.onProcess, opts);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Auto discover queue names from provider and merge it with queue names from global configuration.
|
|
90
|
-
* @private
|
|
91
|
-
*/
|
|
92
|
-
private getUniqQueueNames() {
|
|
93
|
-
return new Set(
|
|
94
|
-
injector()
|
|
95
|
-
.getProviders([BullMQTypes.JOB, BullMQTypes.CRON, BullMQTypes.FALLBACK_JOB])
|
|
96
|
-
.map((provider) => provider.store.get<JobStore>(BULLMQ)?.queue)
|
|
97
|
-
.concat(this.config.queues!)
|
|
98
|
-
.filter(Boolean)
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private getJob(name: string, queueName: string) {
|
|
103
|
-
return inject<JobMethods>(getJobToken(queueName, name)) || inject(getFallbackJobToken(queueName)) || inject(getFallbackJobToken());
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private onProcess = async (job: Job) => {
|
|
107
|
-
const jobService = this.getJob(job.name, job.queueName);
|
|
108
|
-
|
|
109
|
-
if (!jobService) {
|
|
110
|
-
logger().warn({
|
|
111
|
-
event: "BULLMQ_JOB_NOT_FOUND",
|
|
112
|
-
message: `Job ${job.name} ${job.queueName} not found`
|
|
113
|
-
});
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const $ctx = new DIContext({
|
|
118
|
-
id: job.id || v4().split("-").join(""),
|
|
119
|
-
additionalProps: {
|
|
120
|
-
logType: "bullmq",
|
|
121
|
-
name: job.name,
|
|
122
|
-
queue: job.queueName,
|
|
123
|
-
attempt: job.attemptsMade
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
$ctx.set("BULLMQ_JOB", job);
|
|
128
|
-
|
|
129
|
-
try {
|
|
130
|
-
return await runInContext($ctx, () => {
|
|
131
|
-
$ctx.logger.info("Processing job");
|
|
132
|
-
try {
|
|
133
|
-
return jobService.handle(job.data, job);
|
|
134
|
-
} finally {
|
|
135
|
-
$ctx.logger.info("Finished processing job");
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
} catch (er) {
|
|
139
|
-
$ctx.logger.error({
|
|
140
|
-
event: "BULLMQ_JOB_ERROR",
|
|
141
|
-
message: er.message,
|
|
142
|
-
stack: er.stack
|
|
143
|
-
});
|
|
144
|
-
throw er;
|
|
145
|
-
} finally {
|
|
146
|
-
await $ctx.destroy();
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
injectable(BullMQModule).type(ProviderType.MODULE);
|
package/src/config/config.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type {ConnectionOptions, DefaultJobOptions, QueueOptions, WorkerOptions} from "bullmq";
|
|
2
|
-
|
|
3
|
-
export type BullMQConfig = {
|
|
4
|
-
/**
|
|
5
|
-
* Specify queue name's to create
|
|
6
|
-
*/
|
|
7
|
-
queues?: string[];
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Default connection to use for queue's and worker's
|
|
11
|
-
*/
|
|
12
|
-
connection: ConnectionOptions;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Default queue options which are applied to every queue
|
|
16
|
-
*
|
|
17
|
-
* Can be extended/overridden by `queueOptions`
|
|
18
|
-
*/
|
|
19
|
-
defaultQueueOptions?: Partial<QueueOptions>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Specify additional queue options by queue name
|
|
23
|
-
*/
|
|
24
|
-
queueOptions?: Record<string, Partial<QueueOptions>>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Disable the creation of any worker.
|
|
28
|
-
*
|
|
29
|
-
* All other worker configuration will be ignored
|
|
30
|
-
*/
|
|
31
|
-
disableWorker?: boolean;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Specify for which queues to start a worker for.
|
|
35
|
-
*
|
|
36
|
-
* Defaultly for every queue added in the `queues` parameter
|
|
37
|
-
*/
|
|
38
|
-
workerQueues?: string[];
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Default worker options which are applied to every worker
|
|
42
|
-
*
|
|
43
|
-
* Can be extended/overridden by `workerOptions`
|
|
44
|
-
*/
|
|
45
|
-
defaultWorkerOptions?: Partial<WorkerOptions>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Specify additional worker options by queue name
|
|
49
|
-
*/
|
|
50
|
-
workerOptions?: Record<string, Partial<WorkerOptions>>;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
declare global {
|
|
54
|
-
namespace TsED {
|
|
55
|
-
interface Configuration {
|
|
56
|
-
bullmq?: BullMQConfig;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const BULLMQ = "bullmq";
|