@tsed/bullmq 7.38.0

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.
Files changed (118) hide show
  1. package/.barrelsby.json +5 -0
  2. package/README.md +180 -0
  3. package/coverage/clover.xml +180 -0
  4. package/coverage/coverage-final.json +5 -0
  5. package/coverage/coverage-summary.json +6 -0
  6. package/coverage/lcov-report/Job.ts.html +136 -0
  7. package/coverage/lcov-report/base.css +224 -0
  8. package/coverage/lcov-report/block-navigation.js +87 -0
  9. package/coverage/lcov-report/decorators/Job.ts.html +136 -0
  10. package/coverage/lcov-report/decorators/index.html +116 -0
  11. package/coverage/lcov-report/dispatchers/JobDispatcher.ts.html +148 -0
  12. package/coverage/lcov-report/dispatchers/index.html +116 -0
  13. package/coverage/lcov-report/favicon.png +0 -0
  14. package/coverage/lcov-report/index.html +161 -0
  15. package/coverage/lcov-report/prettify.css +1 -0
  16. package/coverage/lcov-report/prettify.js +2 -0
  17. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  18. package/coverage/lcov-report/sorter.js +196 -0
  19. package/coverage/lcov-report/src/BullMQModule.ts.html +361 -0
  20. package/coverage/lcov-report/src/config/config.ts.html +142 -0
  21. package/coverage/lcov-report/src/config/index.html +116 -0
  22. package/coverage/lcov-report/src/decorators/Job.ts.html +136 -0
  23. package/coverage/lcov-report/src/decorators/index.html +116 -0
  24. package/coverage/lcov-report/src/dispatchers/JobDispatcher.ts.html +151 -0
  25. package/coverage/lcov-report/src/dispatchers/index.html +116 -0
  26. package/coverage/lcov-report/src/index.html +116 -0
  27. package/coverage/lcov-report/src/processors/ProcessorFactory.ts.html +166 -0
  28. package/coverage/lcov-report/src/processors/index.html +116 -0
  29. package/coverage/lcov.info +245 -0
  30. package/jest.config.js +11 -0
  31. package/lib/cjs/BullMQModule.js +98 -0
  32. package/lib/cjs/BullMQModule.js.map +1 -0
  33. package/lib/cjs/config/config.js +3 -0
  34. package/lib/cjs/config/config.js.map +1 -0
  35. package/lib/cjs/contracts/JobMethods.js +3 -0
  36. package/lib/cjs/contracts/JobMethods.js.map +1 -0
  37. package/lib/cjs/contracts/JobStore.js +3 -0
  38. package/lib/cjs/contracts/JobStore.js.map +1 -0
  39. package/lib/cjs/contracts/index.js +9 -0
  40. package/lib/cjs/contracts/index.js.map +1 -0
  41. package/lib/cjs/decorators/Job.js +17 -0
  42. package/lib/cjs/decorators/Job.js.map +1 -0
  43. package/lib/cjs/decorators/JobController.js +17 -0
  44. package/lib/cjs/decorators/JobController.js.map +1 -0
  45. package/lib/cjs/decorators/index.js +8 -0
  46. package/lib/cjs/decorators/index.js.map +1 -0
  47. package/lib/cjs/dispatchers/JobDispatcher.js +27 -0
  48. package/lib/cjs/dispatchers/JobDispatcher.js.map +1 -0
  49. package/lib/cjs/dispatchers/index.js +8 -0
  50. package/lib/cjs/dispatchers/index.js.map +1 -0
  51. package/lib/cjs/index.js +13 -0
  52. package/lib/cjs/index.js.map +1 -0
  53. package/lib/cjs/package.json +3 -0
  54. package/lib/cjs/processors/ProcessorFactory.js +20 -0
  55. package/lib/cjs/processors/ProcessorFactory.js.map +1 -0
  56. package/lib/esm/BullMQModule.js +95 -0
  57. package/lib/esm/BullMQModule.js.map +1 -0
  58. package/lib/esm/config/config.js +2 -0
  59. package/lib/esm/config/config.js.map +1 -0
  60. package/lib/esm/contracts/JobMethods.js +2 -0
  61. package/lib/esm/contracts/JobMethods.js.map +1 -0
  62. package/lib/esm/contracts/JobStore.js +2 -0
  63. package/lib/esm/contracts/JobStore.js.map +1 -0
  64. package/lib/esm/contracts/index.js +6 -0
  65. package/lib/esm/contracts/index.js.map +1 -0
  66. package/lib/esm/decorators/JobController.js +13 -0
  67. package/lib/esm/decorators/JobController.js.map +1 -0
  68. package/lib/esm/decorators/index.js +5 -0
  69. package/lib/esm/decorators/index.js.map +1 -0
  70. package/lib/esm/dispatchers/JobDispatcher.js +24 -0
  71. package/lib/esm/dispatchers/JobDispatcher.js.map +1 -0
  72. package/lib/esm/dispatchers/index.js +5 -0
  73. package/lib/esm/dispatchers/index.js.map +1 -0
  74. package/lib/esm/index.js +10 -0
  75. package/lib/esm/index.js.map +1 -0
  76. package/lib/esm/package.json +3 -0
  77. package/lib/tsconfig.esm.tsbuildinfo +1 -0
  78. package/lib/tsconfig.tsbuildinfo +1 -0
  79. package/lib/types/BullMQModule.d.ts +14 -0
  80. package/lib/types/config/config.d.ts +15 -0
  81. package/lib/types/contracts/JobMethods.d.ts +3 -0
  82. package/lib/types/contracts/JobStore.d.ts +6 -0
  83. package/lib/types/contracts/index.d.ts +5 -0
  84. package/lib/types/decorators/Job.d.ts +2 -0
  85. package/lib/types/decorators/JobController.d.ts +2 -0
  86. package/lib/types/decorators/index.d.ts +4 -0
  87. package/lib/types/dispatchers/JobDispatcher.d.ts +9 -0
  88. package/lib/types/dispatchers/index.d.ts +4 -0
  89. package/lib/types/index.d.ts +9 -0
  90. package/lib/types/processors/ProcessorFactory.d.ts +7 -0
  91. package/package.json +51 -0
  92. package/src/BullMQModule.js +149 -0
  93. package/src/BullMQModule.spec.js +236 -0
  94. package/src/BullMQModule.spec.ts +184 -0
  95. package/src/BullMQModule.ts +93 -0
  96. package/src/config/config.js +2 -0
  97. package/src/config/config.ts +19 -0
  98. package/src/contracts/JobMethods.js +2 -0
  99. package/src/contracts/JobMethods.ts +3 -0
  100. package/src/contracts/JobStore.js +2 -0
  101. package/src/contracts/JobStore.ts +7 -0
  102. package/src/contracts/index.js +21 -0
  103. package/src/contracts/index.ts +6 -0
  104. package/src/decorators/Job.js +18 -0
  105. package/src/decorators/JobController.js +18 -0
  106. package/src/decorators/JobController.spec.ts +50 -0
  107. package/src/decorators/JobController.ts +17 -0
  108. package/src/decorators/index.js +20 -0
  109. package/src/decorators/index.ts +5 -0
  110. package/src/dispatchers/JobDispatcher.js +72 -0
  111. package/src/dispatchers/JobDispatcher.spec.js +98 -0
  112. package/src/dispatchers/JobDispatcher.spec.ts +52 -0
  113. package/src/dispatchers/JobDispatcher.ts +22 -0
  114. package/src/dispatchers/index.js +20 -0
  115. package/src/dispatchers/index.ts +5 -0
  116. package/src/index.ts +10 -0
  117. package/tsconfig.esm.json +39 -0
  118. package/tsconfig.json +39 -0
@@ -0,0 +1,2 @@
1
+ import { JobsOptions } from "bullmq";
2
+ export declare function Job(name: string, queue?: string, opts?: JobsOptions): any;
@@ -0,0 +1,2 @@
1
+ import { JobsOptions } from "bullmq";
2
+ export declare function JobController(name: string, queue?: string, opts?: JobsOptions): any;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./JobController";
@@ -0,0 +1,9 @@
1
+ import { InjectorService } from "@tsed/di";
2
+ import { JobMethods } from "../contracts";
3
+ import { Type } from "@tsed/core";
4
+ import { Job as BullMQJob } from "bullmq";
5
+ export declare class JobDispatcher {
6
+ private readonly injector;
7
+ constructor(injector: InjectorService);
8
+ dispatch<T extends JobMethods>(job: Type<JobMethods>, payload?: Parameters<T["handle"]>[0]): Promise<BullMQJob>;
9
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./JobDispatcher";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./BullMQModule";
5
+ export * from "./config/config";
6
+ export * from "./contracts/JobMethods";
7
+ export * from "./contracts/JobStore";
8
+ export * from "./decorators/JobController";
9
+ export * from "./dispatchers/JobDispatcher";
@@ -0,0 +1,7 @@
1
+ import { InjectorService } from "@tsed/di";
2
+ import { Processor } from "bullmq";
3
+ export default class ProcessorFactory {
4
+ private readonly injector;
5
+ constructor(injector: InjectorService);
6
+ build(): Processor;
7
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@tsed/bullmq",
3
+ "version": "7.38.0",
4
+ "description": "BullMQ integration for Ts.ED",
5
+ "keywords": [
6
+ "Ts.ED",
7
+ "BullMQ"
8
+ ],
9
+ "source": "./src/index.ts",
10
+ "main": "./lib/cjs/index.js",
11
+ "module": "./lib/esm/index.js",
12
+ "typings": "./lib/types/index.d.ts",
13
+ "exports": {
14
+ "types": "./lib/types/index.d.ts",
15
+ "import": "./lib/esm/index.js",
16
+ "require": "./lib/cjs/index.js",
17
+ "default": "./lib/esm/index.js"
18
+ },
19
+ "scripts": {
20
+ "build": "yarn barrels && yarn build:ts",
21
+ "barrels": "yarn barrelsby --config .barrelsby.json",
22
+ "test": "cross-env NODE_ENV=test yarn jest --runInBand && jest-coverage-thresholds-bumper",
23
+ "build:ts": "tsc --build tsconfig.json && tsc --build tsconfig.esm.json",
24
+ "lint": "eslint '**/*.{ts,js}'",
25
+ "lint:fix": "eslint '**/*.{ts,js}' --fix"
26
+ },
27
+ "dependencies": {
28
+ "tslib": "2.5.0"
29
+ },
30
+ "devDependencies": {
31
+ "@tsed/common": "7.38.0",
32
+ "@tsed/core": "7.38.0",
33
+ "@tsed/di": "7.38.0",
34
+ "@tsed/eslint": "7.38.0",
35
+ "@tsed/schema": "7.38.0",
36
+ "@tsed/typescript": "7.38.0",
37
+ "bullmq": "^4.12.3",
38
+ "eslint": "^8.12.0",
39
+ "ts-mockito": "^2.6.1"
40
+ },
41
+ "peerDependencies": {
42
+ "bullmq": "^4.12.3"
43
+ },
44
+ "repository": "https://github.com/tsedio/tsed",
45
+ "bugs": {
46
+ "url": "https://github.com/tsedio/tsed/issues"
47
+ },
48
+ "homepage": "https://github.com/tsedio/tsed/tree/production/packages/third-parties/bullmq",
49
+ "author": "Romain Lenzotti",
50
+ "license": "MIT"
51
+ }
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ var __generator = (this && this.__generator) || function (thisArg, body) {
18
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
19
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
20
+ function verb(n) { return function (v) { return step([n, v]); }; }
21
+ function step(op) {
22
+ if (f) throw new TypeError("Generator is already executing.");
23
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
24
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
25
+ if (y = 0, t) op = [op[0] & 2, t.value];
26
+ switch (op[0]) {
27
+ case 0: case 1: t = op; break;
28
+ case 4: _.label++; return { value: op[1], done: false };
29
+ case 5: _.label++; y = op[1]; op = [0]; continue;
30
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
31
+ default:
32
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
33
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
34
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
35
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
36
+ if (t[2]) _.ops.pop();
37
+ _.trys.pop(); continue;
38
+ }
39
+ op = body.call(thisArg, _);
40
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
41
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
42
+ }
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.BullMQModule = void 0;
46
+ var common_1 = require("@tsed/common");
47
+ var di_1 = require("@tsed/di");
48
+ var schema_1 = require("@tsed/schema");
49
+ var bullmq_1 = require("bullmq");
50
+ var uuid_1 = require("uuid");
51
+ var BullMQModule = /** @class */ (function () {
52
+ function BullMQModule(injector, dispatcher) {
53
+ this.injector = injector;
54
+ this.dispatcher = dispatcher;
55
+ }
56
+ BullMQModule.prototype.$beforeInit = function () {
57
+ var _this = this;
58
+ this.buildWorkers();
59
+ this.buildQueues();
60
+ this.injector.getMany("bullmq:cron").map(function (job) { return _this.dispatcher.dispatch((0, schema_1.getComputedType)(job)); });
61
+ };
62
+ BullMQModule.prototype.buildQueues = function () {
63
+ var _this = this;
64
+ this.bullmq.queues.forEach(function (queue) {
65
+ _this.injector
66
+ .add("bullmq.queue.".concat(queue), {
67
+ type: "bullmq:queue",
68
+ useValue: new bullmq_1.Queue(queue, {
69
+ connection: _this.bullmq.connection,
70
+ defaultJobOptions: _this.bullmq.defaultJobOptions
71
+ }),
72
+ hooks: {
73
+ $onDestroy: function (queue) { return queue.close(); }
74
+ }
75
+ })
76
+ .invoke("bullmq.queue.".concat(queue));
77
+ });
78
+ };
79
+ BullMQModule.prototype.buildWorkers = function () {
80
+ var _this = this;
81
+ var _a;
82
+ ((_a = this.bullmq.workerQueues) !== null && _a !== void 0 ? _a : this.bullmq.queues).forEach(function (queue) {
83
+ _this.injector
84
+ .add("bullmq.worker.".concat(queue), {
85
+ type: "bullmq:worker",
86
+ useValue: new bullmq_1.Worker(queue, _this.onProcess.bind(_this), {
87
+ connection: _this.bullmq.connection
88
+ }),
89
+ hooks: {
90
+ $onDestroy: function (worker) { return worker.close(); }
91
+ }
92
+ })
93
+ .invoke("bullmq.worker.".concat(queue));
94
+ });
95
+ };
96
+ BullMQModule.prototype.getJob = function (name, queueName) {
97
+ return this.injector.get("bullmq.job.".concat(queueName, ".").concat(name));
98
+ };
99
+ BullMQModule.prototype.onProcess = function (job) {
100
+ return __awaiter(this, void 0, void 0, function () {
101
+ var jobService, $ctx, er_1;
102
+ return __generator(this, function (_a) {
103
+ switch (_a.label) {
104
+ case 0:
105
+ jobService = this.getJob(job.name, job.queueName);
106
+ if (!jobService) {
107
+ this.injector.logger.warn({
108
+ event: "BULLMQ_JOB_NOT_FOUND",
109
+ message: "Job ".concat(job.name, " ").concat(job.queueName, " not found")
110
+ });
111
+ return [2 /*return*/];
112
+ }
113
+ $ctx = new common_1.DIContext({
114
+ injector: this.injector,
115
+ logger: this.injector.logger,
116
+ id: job.id || (0, uuid_1.v4)().split("-").join("")
117
+ });
118
+ $ctx.set("BULLMQ_JOB", job);
119
+ _a.label = 1;
120
+ case 1:
121
+ _a.trys.push([1, 3, 4, 6]);
122
+ return [4 /*yield*/, (0, common_1.runInContext)($ctx, function () { return jobService.handle(job.data, job); })];
123
+ case 2: return [2 /*return*/, _a.sent()];
124
+ case 3:
125
+ er_1 = _a.sent();
126
+ $ctx.logger.error({
127
+ event: "BULLMQ_JOB_ERROR",
128
+ message: er_1.message,
129
+ stack: er_1.stack
130
+ });
131
+ throw er_1;
132
+ case 4: return [4 /*yield*/, $ctx.destroy()];
133
+ case 5:
134
+ _a.sent();
135
+ return [7 /*endfinally*/];
136
+ case 6: return [2 /*return*/];
137
+ }
138
+ });
139
+ });
140
+ };
141
+ __decorate([
142
+ (0, di_1.Constant)("bullmq")
143
+ ], BullMQModule.prototype, "bullmq", void 0);
144
+ BullMQModule = __decorate([
145
+ (0, di_1.Module)()
146
+ ], BullMQModule);
147
+ return BullMQModule;
148
+ }());
149
+ exports.BullMQModule = BullMQModule;
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ var __generator = (this && this.__generator) || function (thisArg, body) {
18
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
19
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
20
+ function verb(n) { return function (v) { return step([n, v]); }; }
21
+ function step(op) {
22
+ if (f) throw new TypeError("Generator is already executing.");
23
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
24
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
25
+ if (y = 0, t) op = [op[0] & 2, t.value];
26
+ switch (op[0]) {
27
+ case 0: case 1: t = op; break;
28
+ case 4: _.label++; return { value: op[1], done: false };
29
+ case 5: _.label++; y = op[1]; op = [0]; continue;
30
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
31
+ default:
32
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
33
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
34
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
35
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
36
+ if (t[2]) _.ops.pop();
37
+ _.trys.pop(); continue;
38
+ }
39
+ op = body.call(thisArg, _);
40
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
41
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
42
+ }
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ var common_1 = require("@tsed/common");
46
+ var core_1 = require("@tsed/core");
47
+ var bullmq_1 = require("bullmq");
48
+ var ts_mockito_1 = require("ts-mockito");
49
+ require("./BullMQModule");
50
+ var BullMQModule_1 = require("./BullMQModule");
51
+ var decorators_1 = require("./decorators");
52
+ var dispatchers_1 = require("./dispatchers");
53
+ jest.mock("bullmq", function () {
54
+ return {
55
+ Queue: /** @class */ (function () {
56
+ function class_1(name) {
57
+ this.name = name;
58
+ }
59
+ class_1.prototype.close = function () { };
60
+ return class_1;
61
+ }()),
62
+ Worker: /** @class */ (function () {
63
+ function class_2(name) {
64
+ this.name = name;
65
+ }
66
+ class_2.prototype.close = function () { };
67
+ return class_2;
68
+ }())
69
+ };
70
+ });
71
+ var bullmq = {
72
+ queues: ["default", "foo", "bar"],
73
+ connection: {},
74
+ workerQueues: ["default", "foo"]
75
+ };
76
+ var CustomCronJob = /** @class */ (function () {
77
+ function CustomCronJob() {
78
+ }
79
+ CustomCronJob.prototype.handle = function () { };
80
+ CustomCronJob = __decorate([
81
+ (0, decorators_1.JobController)("cron", "default", {
82
+ repeat: {
83
+ pattern: "* * * * *"
84
+ }
85
+ })
86
+ ], CustomCronJob);
87
+ return CustomCronJob;
88
+ }());
89
+ var RegularJob = /** @class */ (function () {
90
+ function RegularJob() {
91
+ }
92
+ RegularJob.prototype.handle = function () { };
93
+ RegularJob = __decorate([
94
+ (0, decorators_1.JobController)("regular", "default")
95
+ ], RegularJob);
96
+ return RegularJob;
97
+ }());
98
+ describe("module", function () {
99
+ var dispatcher;
100
+ beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
101
+ return __generator(this, function (_a) {
102
+ switch (_a.label) {
103
+ case 0:
104
+ dispatcher = (0, ts_mockito_1.mock)(dispatchers_1.JobDispatcher);
105
+ (0, ts_mockito_1.when)(dispatcher.dispatch(CustomCronJob)).thenResolve();
106
+ return [4 /*yield*/, common_1.PlatformTest.create({
107
+ bullmq: bullmq,
108
+ imports: [
109
+ {
110
+ token: dispatchers_1.JobDispatcher,
111
+ use: (0, ts_mockito_1.instance)(dispatcher)
112
+ }
113
+ ]
114
+ })];
115
+ case 1:
116
+ _a.sent();
117
+ return [2 /*return*/];
118
+ }
119
+ });
120
+ }); });
121
+ beforeEach(function () { });
122
+ afterEach(common_1.PlatformTest.reset);
123
+ describe("cronjobs", function () {
124
+ it("should dispatch cron jobs", function () {
125
+ (0, ts_mockito_1.verify)(dispatcher.dispatch(CustomCronJob)).once();
126
+ });
127
+ });
128
+ describe("queues", function () {
129
+ it("should get default", function () {
130
+ var instance = common_1.PlatformTest.get("bullmq.queue.default");
131
+ expect(instance).toBeInstanceOf(bullmq_1.Queue);
132
+ expect(instance.name).toBe("default");
133
+ });
134
+ it.each(bullmq.queues)("should register queue(%s)", function (queue) {
135
+ var instance = common_1.PlatformTest.get("bullmq.queue.".concat(queue));
136
+ expect(instance).toBeInstanceOf(bullmq_1.Queue);
137
+ expect(instance.name).toBe(queue);
138
+ });
139
+ it("should not allow direct injection of the queue", function () {
140
+ expect(common_1.PlatformTest.get(bullmq_1.Queue)).not.toBeInstanceOf(bullmq_1.Queue);
141
+ });
142
+ });
143
+ describe("workers", function () {
144
+ it("should get default", function () {
145
+ var instance = common_1.PlatformTest.get("bullmq.worker.default");
146
+ expect(instance).toBeInstanceOf(bullmq_1.Worker);
147
+ expect(instance.name).toBe("default");
148
+ });
149
+ it.each(bullmq.workerQueues)("should register worker(%s)", function (queue) {
150
+ var instance = common_1.PlatformTest.get("bullmq.worker.".concat(queue));
151
+ expect(instance).toBeInstanceOf(bullmq_1.Worker);
152
+ expect(instance.name).toBe(queue);
153
+ });
154
+ it("should not register unspecified worker queue", function () {
155
+ expect(common_1.PlatformTest.get("bullmq.worker.bar")).toBeUndefined();
156
+ });
157
+ it("should not allow direct injection of the worker", function () {
158
+ expect(common_1.PlatformTest.get(bullmq_1.Worker)).not.toBeInstanceOf(bullmq_1.Worker);
159
+ });
160
+ it("should run worker and execute processor", function () { return __awaiter(void 0, void 0, void 0, function () {
161
+ var bullMQModule, worker, job;
162
+ return __generator(this, function (_a) {
163
+ switch (_a.label) {
164
+ case 0:
165
+ bullMQModule = common_1.PlatformTest.get(BullMQModule_1.BullMQModule);
166
+ worker = common_1.PlatformTest.get("bullmq.job.default.regular");
167
+ job = {
168
+ name: "regular",
169
+ queueName: "default",
170
+ data: { test: "test" }
171
+ };
172
+ jest.spyOn(worker, "handle").mockResolvedValueOnce(undefined);
173
+ return [4 /*yield*/, bullMQModule.onProcess(job)];
174
+ case 1:
175
+ _a.sent();
176
+ expect(worker.handle).toHaveBeenCalledWith({ test: "test" }, job);
177
+ return [2 /*return*/];
178
+ }
179
+ });
180
+ }); });
181
+ it("should log warning when the worker doesn't exists", function () { return __awaiter(void 0, void 0, void 0, function () {
182
+ var bullMQModule, job;
183
+ return __generator(this, function (_a) {
184
+ switch (_a.label) {
185
+ case 0:
186
+ bullMQModule = common_1.PlatformTest.get(BullMQModule_1.BullMQModule);
187
+ jest.spyOn(common_1.PlatformTest.injector.logger, "warn");
188
+ job = {
189
+ name: "regular",
190
+ queueName: "toto",
191
+ data: { test: "test" }
192
+ };
193
+ return [4 /*yield*/, bullMQModule.onProcess(job)];
194
+ case 1:
195
+ _a.sent();
196
+ expect(common_1.PlatformTest.injector.logger.warn).toHaveBeenCalledWith({
197
+ event: "BULLMQ_JOB_NOT_FOUND",
198
+ message: "Job regular toto not found"
199
+ });
200
+ return [2 /*return*/];
201
+ }
202
+ });
203
+ }); });
204
+ it("should run worker, execute processor and handle error", function () { return __awaiter(void 0, void 0, void 0, function () {
205
+ var bullMQModule, worker, job, error;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0:
209
+ bullMQModule = common_1.PlatformTest.get(BullMQModule_1.BullMQModule);
210
+ worker = common_1.PlatformTest.get("bullmq.job.default.regular");
211
+ job = {
212
+ name: "regular",
213
+ queueName: "default",
214
+ data: { test: "test" }
215
+ };
216
+ jest.spyOn(common_1.PlatformTest.injector.logger, "error");
217
+ jest.spyOn(worker, "handle").mockRejectedValue(new Error("error"));
218
+ return [4 /*yield*/, (0, core_1.catchAsyncError)(function () { return bullMQModule.onProcess(job); })];
219
+ case 1:
220
+ error = _a.sent();
221
+ expect(worker.handle).toHaveBeenCalledWith({ test: "test" }, job);
222
+ expect(common_1.PlatformTest.injector.logger.error).toHaveBeenCalledWith({
223
+ duration: expect.any(Number),
224
+ event: "BULLMQ_JOB_ERROR",
225
+ message: "error",
226
+ reqId: expect.any(String),
227
+ stack: expect.any(String),
228
+ time: expect.any(Object)
229
+ });
230
+ expect(error === null || error === void 0 ? void 0 : error.message).toEqual("error");
231
+ return [2 /*return*/];
232
+ }
233
+ });
234
+ }); });
235
+ });
236
+ });
@@ -0,0 +1,184 @@
1
+ import {PlatformTest} from "@tsed/common";
2
+ import {catchAsyncError} from "@tsed/core";
3
+ import {Queue, Worker} from "bullmq";
4
+ import {instance, mock, verify, when} from "ts-mockito";
5
+
6
+ import "./BullMQModule";
7
+ import {BullMQModule} from "./BullMQModule";
8
+ import {type BullMQConfig} from "./config/config";
9
+ import {JobMethods} from "./contracts";
10
+ import {JobController} from "./decorators";
11
+ import {JobDispatcher} from "./dispatchers";
12
+
13
+ jest.mock("bullmq", () => {
14
+ return {
15
+ Queue: class {
16
+ public name: string;
17
+
18
+ constructor(name: string) {
19
+ this.name = name;
20
+ }
21
+ close() {}
22
+ },
23
+ Worker: class {
24
+ public name: string;
25
+
26
+ constructor(name: string) {
27
+ this.name = name;
28
+ }
29
+ close() {}
30
+ }
31
+ };
32
+ });
33
+
34
+ const bullmq = {
35
+ queues: ["default", "foo", "bar"],
36
+ connection: {},
37
+ workerQueues: ["default", "foo"]
38
+ } as BullMQConfig;
39
+
40
+ @JobController("cron", "default", {
41
+ repeat: {
42
+ pattern: "* * * * *"
43
+ }
44
+ })
45
+ class CustomCronJob implements JobMethods {
46
+ handle() {}
47
+ }
48
+
49
+ @JobController("regular", "default")
50
+ class RegularJob {
51
+ handle() {}
52
+ }
53
+
54
+ describe("module", () => {
55
+ let dispatcher: JobDispatcher;
56
+ beforeEach(async () => {
57
+ dispatcher = mock(JobDispatcher);
58
+ when(dispatcher.dispatch(CustomCronJob)).thenResolve();
59
+
60
+ await PlatformTest.create({
61
+ bullmq,
62
+ imports: [
63
+ {
64
+ token: JobDispatcher,
65
+ use: instance(dispatcher)
66
+ }
67
+ ]
68
+ });
69
+ });
70
+ beforeEach(() => {});
71
+ afterEach(PlatformTest.reset);
72
+
73
+ describe("cronjobs", () => {
74
+ it("should dispatch cron jobs", () => {
75
+ verify(dispatcher.dispatch(CustomCronJob)).once();
76
+ });
77
+ });
78
+
79
+ describe("queues", () => {
80
+ it("should get default", () => {
81
+ const instance = PlatformTest.get<Queue>("bullmq.queue.default");
82
+
83
+ expect(instance).toBeInstanceOf(Queue);
84
+ expect(instance.name).toBe("default");
85
+ });
86
+
87
+ it.each(bullmq.queues)("should register queue(%s)", (queue) => {
88
+ const instance = PlatformTest.get<Queue>(`bullmq.queue.${queue}`);
89
+
90
+ expect(instance).toBeInstanceOf(Queue);
91
+ expect(instance.name).toBe(queue);
92
+ });
93
+
94
+ it("should not allow direct injection of the queue", () => {
95
+ expect(PlatformTest.get(Queue)).not.toBeInstanceOf(Queue);
96
+ });
97
+ });
98
+
99
+ describe("workers", () => {
100
+ it("should get default", () => {
101
+ const instance = PlatformTest.get<Worker>("bullmq.worker.default");
102
+
103
+ expect(instance).toBeInstanceOf(Worker);
104
+ expect(instance.name).toBe("default");
105
+ });
106
+
107
+ it.each(bullmq.workerQueues)("should register worker(%s)", (queue) => {
108
+ const instance = PlatformTest.get<Worker>(`bullmq.worker.${queue}`);
109
+
110
+ expect(instance).toBeInstanceOf(Worker);
111
+ expect(instance.name).toBe(queue);
112
+ });
113
+
114
+ it("should not register unspecified worker queue", () => {
115
+ expect(PlatformTest.get("bullmq.worker.bar")).toBeUndefined();
116
+ });
117
+
118
+ it("should not allow direct injection of the worker", () => {
119
+ expect(PlatformTest.get(Worker)).not.toBeInstanceOf(Worker);
120
+ });
121
+
122
+ it("should run worker and execute processor", async () => {
123
+ const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
124
+ const worker = PlatformTest.get<JobMethods>("bullmq.job.default.regular");
125
+ const job = {
126
+ name: "regular",
127
+ queueName: "default",
128
+ data: {test: "test"}
129
+ };
130
+
131
+ jest.spyOn(worker, "handle").mockResolvedValueOnce(undefined as never);
132
+
133
+ await (bullMQModule as any).onProcess(job);
134
+
135
+ expect(worker.handle).toHaveBeenCalledWith({test: "test"}, job);
136
+ });
137
+
138
+ it("should log warning when the worker doesn't exists", async () => {
139
+ const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
140
+
141
+ jest.spyOn(PlatformTest.injector.logger, "warn");
142
+
143
+ const job = {
144
+ name: "regular",
145
+ queueName: "toto",
146
+ data: {test: "test"}
147
+ };
148
+
149
+ await (bullMQModule as any).onProcess(job);
150
+
151
+ expect(PlatformTest.injector.logger.warn).toHaveBeenCalledWith({
152
+ event: "BULLMQ_JOB_NOT_FOUND",
153
+ message: "Job regular toto not found"
154
+ });
155
+ });
156
+
157
+ it("should run worker, execute processor and handle error", async () => {
158
+ const bullMQModule = PlatformTest.get<BullMQModule>(BullMQModule);
159
+ const worker = PlatformTest.get<JobMethods>("bullmq.job.default.regular");
160
+ const job = {
161
+ name: "regular",
162
+ queueName: "default",
163
+ data: {test: "test"}
164
+ };
165
+
166
+ jest.spyOn(PlatformTest.injector.logger, "error");
167
+
168
+ jest.spyOn(worker, "handle").mockRejectedValue(new Error("error") as never);
169
+
170
+ const error = await catchAsyncError(() => (bullMQModule as any).onProcess(job));
171
+
172
+ expect(worker.handle).toHaveBeenCalledWith({test: "test"}, job);
173
+ expect(PlatformTest.injector.logger.error).toHaveBeenCalledWith({
174
+ duration: expect.any(Number),
175
+ event: "BULLMQ_JOB_ERROR",
176
+ message: "error",
177
+ reqId: expect.any(String),
178
+ stack: expect.any(String),
179
+ time: expect.any(Object)
180
+ });
181
+ expect(error?.message).toEqual("error");
182
+ });
183
+ });
184
+ });