@midwayjs/bullmq 0.0.1 → 0.0.2
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/dist/config/config.default.d.ts +2 -15
- package/dist/config/config.default.js +1 -1
- package/dist/configuration.js +6 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +3 -1
- package/dist/decorator.d.ts +4 -3
- package/dist/decorator.js +14 -2
- package/dist/framework.d.ts +49 -14
- package/dist/framework.js +116 -37
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/interface.d.ts +11 -16
- package/index.d.ts +2 -14
- package/package.json +2 -2
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
defaultQueueOptions: {
|
|
4
|
-
defaultJobOptions: {
|
|
5
|
-
removeOnComplete: number;
|
|
6
|
-
removeOnFail: number;
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
-
defaultWorkerOptions: {
|
|
10
|
-
concurrency: number;
|
|
11
|
-
};
|
|
12
|
-
clearRepeatJobWhenStart: boolean;
|
|
13
|
-
contextLoggerApplyLogger: string;
|
|
14
|
-
contextLoggerFormat: (info: any) => string;
|
|
15
|
-
};
|
|
1
|
+
import { BullMQConfig } from '../interface';
|
|
2
|
+
export declare const bullmq: BullMQConfig;
|
|
16
3
|
export declare const midwayLogger: {
|
|
17
4
|
clients: {
|
|
18
5
|
bullMQLogger: {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.midwayLogger = exports.bullmq = void 0;
|
|
4
4
|
exports.bullmq = {
|
|
5
|
-
|
|
5
|
+
defaultPrefix: '{midway-bullmq}',
|
|
6
6
|
defaultQueueOptions: {
|
|
7
7
|
defaultJobOptions: {
|
|
8
8
|
removeOnComplete: 3,
|
package/dist/configuration.js
CHANGED
|
@@ -19,6 +19,12 @@ let BullConfiguration = class BullConfiguration {
|
|
|
19
19
|
this.decoratorService.registerPropertyHandler(constants_1.BULLMQ_QUEUE_KEY, (propertyName, meta) => {
|
|
20
20
|
return this.framework.getQueue(meta.queueName);
|
|
21
21
|
});
|
|
22
|
+
this.decoratorService.registerPropertyHandler(constants_1.BULLMQ_WORKER_KEY, (propertyName, meta) => {
|
|
23
|
+
return this.framework.getWorker(meta.queueName);
|
|
24
|
+
});
|
|
25
|
+
this.decoratorService.registerPropertyHandler(constants_1.BULLMQ_FLOW_PRODUCER_KEY, (propertyName, meta) => {
|
|
26
|
+
return this.framework.getFlowProducer(meta.producerName);
|
|
27
|
+
});
|
|
22
28
|
}
|
|
23
29
|
async onReady() {
|
|
24
30
|
this.framework.loadConfig();
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare const BULLMQ_QUEUE_KEY = "bullmq:queue";
|
|
2
2
|
export declare const BULLMQ_PROCESSOR_KEY = "bullmq:processor";
|
|
3
|
+
export declare const BULLMQ_WORKER_KEY = "bullmq:worker";
|
|
4
|
+
export declare const BULLMQ_FLOW_PRODUCER_KEY = "bullmq:flow-producer";
|
|
3
5
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/constants.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BULLMQ_PROCESSOR_KEY = exports.BULLMQ_QUEUE_KEY = void 0;
|
|
3
|
+
exports.BULLMQ_FLOW_PRODUCER_KEY = exports.BULLMQ_WORKER_KEY = exports.BULLMQ_PROCESSOR_KEY = exports.BULLMQ_QUEUE_KEY = void 0;
|
|
4
4
|
// task
|
|
5
5
|
exports.BULLMQ_QUEUE_KEY = 'bullmq:queue';
|
|
6
6
|
exports.BULLMQ_PROCESSOR_KEY = 'bullmq:processor';
|
|
7
|
+
exports.BULLMQ_WORKER_KEY = 'bullmq:worker';
|
|
8
|
+
exports.BULLMQ_FLOW_PRODUCER_KEY = 'bullmq:flow-producer';
|
|
7
9
|
//# sourceMappingURL=constants.js.map
|
package/dist/decorator.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare function Processor(queueName: string, jobOptions?: JobsOptions, workerOptions?: IWorkerOptions, queueOptions?: IQueueOptions): ClassDecorator;
|
|
1
|
+
import { QueueOptions, WorkerOptions, JobsOptions } from 'bullmq';
|
|
2
|
+
export declare function Processor(queueName: string, jobOptions?: JobsOptions, workerOptions?: Partial<WorkerOptions>, queueOptions?: Partial<QueueOptions>): ClassDecorator;
|
|
4
3
|
export declare function InjectQueue(queueName: string): PropertyDecorator;
|
|
4
|
+
export declare function InjectWorker(queueName: string): PropertyDecorator;
|
|
5
|
+
export declare function InjectFlowProducer(producerName: string): PropertyDecorator;
|
|
5
6
|
//# sourceMappingURL=decorator.d.ts.map
|
package/dist/decorator.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InjectQueue = exports.Processor = void 0;
|
|
3
|
+
exports.InjectFlowProducer = exports.InjectWorker = exports.InjectQueue = exports.Processor = void 0;
|
|
4
4
|
const core_1 = require("@midwayjs/core");
|
|
5
5
|
const constants_1 = require("./constants");
|
|
6
6
|
function Processor(queueName, jobOptions, workerOptions, queueOptions) {
|
|
@@ -9,8 +9,8 @@ function Processor(queueName, jobOptions, workerOptions, queueOptions) {
|
|
|
9
9
|
(0, core_1.saveClassMetadata)(constants_1.BULLMQ_PROCESSOR_KEY, {
|
|
10
10
|
queueName,
|
|
11
11
|
jobOptions,
|
|
12
|
-
queueOptions,
|
|
13
12
|
workerOptions,
|
|
13
|
+
queueOptions,
|
|
14
14
|
}, target);
|
|
15
15
|
(0, core_1.Provide)()(target);
|
|
16
16
|
(0, core_1.Scope)(core_1.ScopeEnum.Request)(target);
|
|
@@ -23,4 +23,16 @@ function InjectQueue(queueName) {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
exports.InjectQueue = InjectQueue;
|
|
26
|
+
function InjectWorker(queueName) {
|
|
27
|
+
return (0, core_1.createCustomPropertyDecorator)(constants_1.BULLMQ_WORKER_KEY, {
|
|
28
|
+
queueName,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
exports.InjectWorker = InjectWorker;
|
|
32
|
+
function InjectFlowProducer(producerName) {
|
|
33
|
+
return (0, core_1.createCustomPropertyDecorator)(constants_1.BULLMQ_FLOW_PRODUCER_KEY, {
|
|
34
|
+
producerName,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
exports.InjectFlowProducer = InjectFlowProducer;
|
|
26
38
|
//# sourceMappingURL=decorator.js.map
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import { BaseFramework, IMidwayBootstrapOptions, ILogger } from '@midwayjs/core';
|
|
2
|
-
import { Application, Context, IProcessor
|
|
3
|
-
import { Job, JobsOptions, Queue, Worker, QueueOptions } from 'bullmq';
|
|
4
|
-
export declare class BullMQQueue extends Queue
|
|
2
|
+
import { Application, Context, IProcessor } from './interface';
|
|
3
|
+
import { Job, JobsOptions, Queue, Worker, QueueOptions, WorkerOptions, QueueEvents, QueueEventsProducer, QueueBaseOptions, QueueEventsOptions, FlowProducer } from 'bullmq';
|
|
4
|
+
export declare class BullMQQueue extends Queue {
|
|
5
|
+
protected queueName: string;
|
|
6
|
+
protected queueOptions: QueueOptions;
|
|
7
|
+
private queueEventsList;
|
|
8
|
+
private queueEventsProducerList;
|
|
5
9
|
constructor(queueName: string, queueOptions: QueueOptions);
|
|
6
|
-
getJob(name: string): Promise<Job>;
|
|
7
10
|
runJob(data: any, options?: JobsOptions): Promise<Job>;
|
|
8
11
|
getQueueName(): string;
|
|
12
|
+
createQueueEvents(options?: QueueEventsOptions): QueueEvents;
|
|
13
|
+
getQueueEventsList(): QueueEvents[];
|
|
14
|
+
createQueueEventsProducer(options?: QueueBaseOptions): QueueEventsProducer;
|
|
15
|
+
getQueueEventsProducerList(): QueueEventsProducer[];
|
|
9
16
|
}
|
|
10
|
-
export declare class BullMQFramework extends BaseFramework<Application, Context, any>
|
|
11
|
-
private
|
|
12
|
-
private prefix?;
|
|
17
|
+
export declare class BullMQFramework extends BaseFramework<Application, Context, any> {
|
|
18
|
+
private defaultConnection;
|
|
13
19
|
private defaultQueueConfig;
|
|
14
20
|
private defaultWorkerConfig;
|
|
15
21
|
private clearRepeatJobWhenStart;
|
|
16
22
|
private queueMap;
|
|
17
|
-
/** keep a map of workers for gracefully shutdown */
|
|
18
23
|
private workerMap;
|
|
24
|
+
private flowProducerMap;
|
|
19
25
|
protected bullMQLogger: ILogger;
|
|
20
26
|
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
21
27
|
loadConfig(): void;
|
|
@@ -23,13 +29,42 @@ export declare class BullMQFramework extends BaseFramework<Application, Context,
|
|
|
23
29
|
getFrameworkName(): string;
|
|
24
30
|
run(): Promise<void>;
|
|
25
31
|
protected beforeStop(): Promise<void>;
|
|
26
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Create a queue with name and queueOptions
|
|
34
|
+
*/
|
|
35
|
+
createQueue(name: string, queueOptions?: Partial<QueueOptions>): BullMQQueue;
|
|
36
|
+
/**
|
|
37
|
+
* Get a queue by name
|
|
38
|
+
*/
|
|
27
39
|
getQueue(name: string): BullMQQueue;
|
|
28
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Ensure a queue by name and queueOptions
|
|
42
|
+
*/
|
|
43
|
+
protected ensureQueue(name: string, queueOptions?: Partial<QueueOptions>): BullMQQueue;
|
|
29
44
|
getQueueList(): BullMQQueue[];
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Get the first worker by queueName
|
|
47
|
+
*/
|
|
48
|
+
getWorker(queueName: string): Worker;
|
|
49
|
+
/**
|
|
50
|
+
* Get all workers by queueName
|
|
51
|
+
*/
|
|
52
|
+
getWorkers(queueName: string): Worker[];
|
|
53
|
+
/**
|
|
54
|
+
* Create a worker
|
|
55
|
+
*/
|
|
56
|
+
createWorker(queueName: string, processor: (job: Job, token?: string) => Promise<any>, workerOptions?: Partial<WorkerOptions>): Worker;
|
|
57
|
+
/**
|
|
58
|
+
* Add a processor class and init a worker
|
|
59
|
+
*/
|
|
60
|
+
addProcessor(processor: new (...args: any[]) => IProcessor, queueName: string, workerOptions?: WorkerOptions): Promise<Worker<any, any, string>>;
|
|
61
|
+
/**
|
|
62
|
+
* Create a flow producer, if producerName is provided, it will be store.
|
|
63
|
+
*/
|
|
64
|
+
createFlowProducer(options?: Partial<QueueBaseOptions>, producerName?: string): FlowProducer;
|
|
65
|
+
/**
|
|
66
|
+
* Get a flow producer by name
|
|
67
|
+
*/
|
|
68
|
+
getFlowProducer(producerName: string): FlowProducer | undefined;
|
|
34
69
|
}
|
|
35
70
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -16,11 +16,11 @@ const constants_1 = require("./constants");
|
|
|
16
16
|
class BullMQQueue extends bullmq_1.Queue {
|
|
17
17
|
constructor(queueName, queueOptions) {
|
|
18
18
|
super(queueName, queueOptions);
|
|
19
|
+
this.queueName = queueName;
|
|
20
|
+
this.queueOptions = queueOptions;
|
|
21
|
+
this.queueEventsList = [];
|
|
22
|
+
this.queueEventsProducerList = [];
|
|
19
23
|
}
|
|
20
|
-
getJob(name) {
|
|
21
|
-
throw new Error('Method not implemented.');
|
|
22
|
-
}
|
|
23
|
-
// bullmq 在 queue.add 新增第一个参数 jobName
|
|
24
24
|
// runJob 与 @midwayjs/bull 保持一致,如果想要使用 jobName 则可以直接调用 queue.add
|
|
25
25
|
async runJob(data, options) {
|
|
26
26
|
const { repeat, ...OtherOptions } = options !== null && options !== void 0 ? options : {};
|
|
@@ -34,7 +34,31 @@ class BullMQQueue extends bullmq_1.Queue {
|
|
|
34
34
|
return this.add('jobName', data || {}, options);
|
|
35
35
|
}
|
|
36
36
|
getQueueName() {
|
|
37
|
-
return this.
|
|
37
|
+
return this.queueName;
|
|
38
|
+
}
|
|
39
|
+
createQueueEvents(options) {
|
|
40
|
+
const evt = new bullmq_1.QueueEvents(this.name, {
|
|
41
|
+
connection: this.queueOptions.connection,
|
|
42
|
+
prefix: this.queueOptions.prefix,
|
|
43
|
+
...options,
|
|
44
|
+
});
|
|
45
|
+
this.queueEventsList.push(evt);
|
|
46
|
+
return evt;
|
|
47
|
+
}
|
|
48
|
+
getQueueEventsList() {
|
|
49
|
+
return this.queueEventsList;
|
|
50
|
+
}
|
|
51
|
+
createQueueEventsProducer(options) {
|
|
52
|
+
const producer = new bullmq_1.QueueEventsProducer(this.name, {
|
|
53
|
+
connection: this.queueOptions.connection,
|
|
54
|
+
prefix: this.queueOptions.prefix,
|
|
55
|
+
...options,
|
|
56
|
+
});
|
|
57
|
+
this.queueEventsProducerList.push(producer);
|
|
58
|
+
return producer;
|
|
59
|
+
}
|
|
60
|
+
getQueueEventsProducerList() {
|
|
61
|
+
return this.queueEventsProducerList;
|
|
38
62
|
}
|
|
39
63
|
}
|
|
40
64
|
exports.BullMQQueue = BullMQQueue;
|
|
@@ -42,15 +66,19 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
42
66
|
constructor() {
|
|
43
67
|
super(...arguments);
|
|
44
68
|
this.queueMap = new Map();
|
|
45
|
-
/** keep a map of workers for gracefully shutdown */
|
|
46
69
|
this.workerMap = new Map();
|
|
70
|
+
this.flowProducerMap = new Map();
|
|
47
71
|
}
|
|
48
72
|
async applicationInitialize(options) {
|
|
49
73
|
this.app = {};
|
|
50
74
|
}
|
|
51
75
|
loadConfig() {
|
|
52
|
-
|
|
53
|
-
|
|
76
|
+
const defaultConnection = this.configService.getConfiguration('bullmq.defaultConnection');
|
|
77
|
+
const defaultPrefix = this.configService.getConfiguration('bullmq.defaultPrefix');
|
|
78
|
+
this.defaultConnection = {
|
|
79
|
+
connection: defaultConnection,
|
|
80
|
+
prefix: defaultPrefix,
|
|
81
|
+
};
|
|
54
82
|
this.defaultQueueConfig = this.configService.getConfiguration('bullmq.defaultQueueOptions');
|
|
55
83
|
this.defaultWorkerConfig = this.configService.getConfiguration('bullmq.defaultWorkerOptions');
|
|
56
84
|
this.clearRepeatJobWhenStart = this.configService.getConfiguration('bullmq.clearRepeatJobWhenStart');
|
|
@@ -62,7 +90,7 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
62
90
|
return 'bullmq';
|
|
63
91
|
}
|
|
64
92
|
async run() {
|
|
65
|
-
var _a, _b;
|
|
93
|
+
var _a, _b, _c;
|
|
66
94
|
const processorModules = (0, core_1.listModule)(constants_1.BULLMQ_PROCESSOR_KEY);
|
|
67
95
|
for (const mod of processorModules) {
|
|
68
96
|
const options = (0, core_1.getClassMetadata)(constants_1.BULLMQ_PROCESSOR_KEY, mod);
|
|
@@ -72,8 +100,9 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
72
100
|
...queueOptions,
|
|
73
101
|
defaultJobOptions: otherOptions,
|
|
74
102
|
});
|
|
75
|
-
if (!currentQueue)
|
|
76
|
-
throw
|
|
103
|
+
if (!currentQueue) {
|
|
104
|
+
throw new core_1.MidwayCommonError(`[midway:bullmq] Queue ${options.queueName} not found`);
|
|
105
|
+
}
|
|
77
106
|
// clear old repeat job when start
|
|
78
107
|
if (this.clearRepeatJobWhenStart) {
|
|
79
108
|
const jobs = await currentQueue.getJobSchedulers();
|
|
@@ -88,7 +117,8 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
88
117
|
}
|
|
89
118
|
await this.addProcessor(mod, options.queueName, options.workerOptions);
|
|
90
119
|
if (repeat) {
|
|
91
|
-
|
|
120
|
+
// add repeatable job
|
|
121
|
+
await ((_c = this.getQueue(options.queueName)) === null || _c === void 0 ? void 0 : _c.runJob({}, options.jobOptions));
|
|
92
122
|
}
|
|
93
123
|
}
|
|
94
124
|
}
|
|
@@ -98,24 +128,40 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
98
128
|
await queue.close();
|
|
99
129
|
}
|
|
100
130
|
for (const worker of this.workerMap.values()) {
|
|
101
|
-
|
|
131
|
+
for (const w of worker) {
|
|
132
|
+
await w.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const producer of this.flowProducerMap.values()) {
|
|
136
|
+
await producer.close();
|
|
102
137
|
}
|
|
103
138
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Create a queue with name and queueOptions
|
|
141
|
+
*/
|
|
142
|
+
createQueue(name, queueOptions = {}) {
|
|
143
|
+
const mergedOptions = {
|
|
144
|
+
...this.defaultQueueConfig,
|
|
145
|
+
...this.defaultConnection,
|
|
146
|
+
...queueOptions,
|
|
147
|
+
};
|
|
148
|
+
const queue = new BullMQQueue(name, mergedOptions);
|
|
109
149
|
this.queueMap.set(name, queue);
|
|
110
150
|
queue.on('error', err => {
|
|
111
151
|
this.bullMQLogger.error(err);
|
|
112
152
|
});
|
|
113
153
|
return queue;
|
|
114
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Get a queue by name
|
|
157
|
+
*/
|
|
115
158
|
getQueue(name) {
|
|
116
159
|
return this.queueMap.get(name);
|
|
117
160
|
}
|
|
118
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Ensure a queue by name and queueOptions
|
|
163
|
+
*/
|
|
164
|
+
ensureQueue(name, queueOptions = {}) {
|
|
119
165
|
if (!this.queueMap.has(name)) {
|
|
120
166
|
this.createQueue(name, queueOptions);
|
|
121
167
|
}
|
|
@@ -124,17 +170,47 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
124
170
|
getQueueList() {
|
|
125
171
|
return Array.from(this.queueMap.values());
|
|
126
172
|
}
|
|
127
|
-
|
|
128
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Get the first worker by queueName
|
|
175
|
+
*/
|
|
176
|
+
getWorker(queueName) {
|
|
177
|
+
var _a;
|
|
178
|
+
return (_a = this.workerMap.get(queueName)) === null || _a === void 0 ? void 0 : _a[0];
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get all workers by queueName
|
|
182
|
+
*/
|
|
183
|
+
getWorkers(queueName) {
|
|
184
|
+
return this.workerMap.get(queueName);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Create a worker
|
|
188
|
+
*/
|
|
189
|
+
createWorker(queueName, processor, workerOptions = {}) {
|
|
190
|
+
const merged = {
|
|
191
|
+
...this.defaultConnection,
|
|
192
|
+
...this.defaultWorkerConfig,
|
|
193
|
+
...workerOptions,
|
|
194
|
+
};
|
|
195
|
+
const worker = new bullmq_1.Worker(queueName, processor, merged);
|
|
196
|
+
if (!this.workerMap.has(queueName)) {
|
|
197
|
+
this.workerMap.set(queueName, []);
|
|
198
|
+
}
|
|
199
|
+
this.workerMap.get(queueName).push(worker);
|
|
200
|
+
return worker;
|
|
129
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Add a processor class and init a worker
|
|
204
|
+
*/
|
|
130
205
|
async addProcessor(processor, queueName, workerOptions) {
|
|
131
206
|
const queue = this.queueMap.get(queueName);
|
|
132
207
|
if (!queue)
|
|
133
208
|
throw Error(`queue not found ${queueName}`);
|
|
134
|
-
|
|
209
|
+
return this.createWorker(queueName, async (job, token) => {
|
|
135
210
|
const ctx = this.app.createAnonymousContext({
|
|
136
211
|
jobId: job.id,
|
|
137
212
|
job,
|
|
213
|
+
token,
|
|
138
214
|
from: processor,
|
|
139
215
|
});
|
|
140
216
|
try {
|
|
@@ -147,7 +223,7 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
147
223
|
}
|
|
148
224
|
const service = await ctx.requestContext.getAsync(processor);
|
|
149
225
|
const fn = await this.applyMiddleware(async (ctx) => {
|
|
150
|
-
return await core_1.Utils.toAsyncFunction(service.execute.bind(service))(job.data, job);
|
|
226
|
+
return await core_1.Utils.toAsyncFunction(service.execute.bind(service))(job.data, job, token);
|
|
151
227
|
});
|
|
152
228
|
const result = await Promise.resolve(await fn(ctx));
|
|
153
229
|
ctx.logger.info(`complete process job ${job.id} from ${processor.name}`);
|
|
@@ -157,23 +233,26 @@ let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
|
157
233
|
ctx.logger.error(err);
|
|
158
234
|
return Promise.reject(err);
|
|
159
235
|
}
|
|
160
|
-
},
|
|
161
|
-
connection: this.connection,
|
|
162
|
-
prefix: this.prefix,
|
|
163
|
-
}));
|
|
164
|
-
this.workerMap.set(queueName, worker);
|
|
236
|
+
}, workerOptions);
|
|
165
237
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
238
|
+
/**
|
|
239
|
+
* Create a flow producer, if producerName is provided, it will be store.
|
|
240
|
+
*/
|
|
241
|
+
createFlowProducer(options, producerName) {
|
|
242
|
+
const producer = new bullmq_1.FlowProducer({
|
|
243
|
+
...this.defaultConnection,
|
|
244
|
+
...options,
|
|
245
|
+
});
|
|
246
|
+
if (producerName) {
|
|
247
|
+
this.flowProducerMap.set(producerName, producer);
|
|
170
248
|
}
|
|
249
|
+
return producer;
|
|
171
250
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Get a flow producer by name
|
|
253
|
+
*/
|
|
254
|
+
getFlowProducer(producerName) {
|
|
255
|
+
return this.flowProducerMap.get(producerName);
|
|
177
256
|
}
|
|
178
257
|
};
|
|
179
258
|
__decorate([
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { BullConfiguration as Configuration } from './configuration';
|
|
|
2
2
|
export { BullMQFramework as Framework, BullMQQueue } from './framework';
|
|
3
3
|
export * from './decorator';
|
|
4
4
|
export * from './interface';
|
|
5
|
+
export * as BullMQ from 'bullmq';
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.BullMQQueue = exports.Framework = exports.Configuration = void 0;
|
|
17
|
+
exports.BullMQ = exports.BullMQQueue = exports.Framework = exports.Configuration = void 0;
|
|
18
18
|
var configuration_1 = require("./configuration");
|
|
19
19
|
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.BullConfiguration; } });
|
|
20
20
|
var framework_1 = require("./framework");
|
|
@@ -22,4 +22,5 @@ Object.defineProperty(exports, "Framework", { enumerable: true, get: function ()
|
|
|
22
22
|
Object.defineProperty(exports, "BullMQQueue", { enumerable: true, get: function () { return framework_1.BullMQQueue; } });
|
|
23
23
|
__exportStar(require("./decorator"), exports);
|
|
24
24
|
__exportStar(require("./interface"), exports);
|
|
25
|
+
exports.BullMQ = require("bullmq");
|
|
25
26
|
//# sourceMappingURL=index.js.map
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
import { IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
2
|
-
import { WorkerOptions, QueueOptions, Job,
|
|
2
|
+
import { WorkerOptions, QueueOptions, Job, ConnectionOptions } from 'bullmq';
|
|
3
3
|
export interface IProcessor {
|
|
4
|
-
execute(data: any):
|
|
5
|
-
}
|
|
6
|
-
export interface IQueue<Job> {
|
|
7
|
-
runJob(data: Record<string, any>, options?: unknown): Promise<Job>;
|
|
8
|
-
getJob(name: string): Promise<Job>;
|
|
9
|
-
getQueueName(): string;
|
|
10
|
-
}
|
|
11
|
-
export interface IQueueManager<Queue extends IQueue<Job>, Job> {
|
|
12
|
-
runJob(queueName: string, jobData: any, options?: unknown): Promise<Job | undefined>;
|
|
13
|
-
getJob(queueName: string, jobName: string): Promise<Job | undefined>;
|
|
14
|
-
createQueue(queueName: string, queueOptions?: unknown): Queue;
|
|
15
|
-
getQueue(queueName: string): Queue | undefined;
|
|
16
|
-
getWorker(queueName: string): Worker | undefined;
|
|
4
|
+
execute(data: any, job: Job, token?: string): Promise<void>;
|
|
17
5
|
}
|
|
18
6
|
export interface Application extends IMidwayApplication<Context> {
|
|
19
7
|
}
|
|
@@ -23,6 +11,13 @@ export interface Context extends IMidwayContext {
|
|
|
23
11
|
job: Job;
|
|
24
12
|
from: new (...args: any[]) => IProcessor;
|
|
25
13
|
}
|
|
26
|
-
export
|
|
27
|
-
|
|
14
|
+
export interface BullMQConfig {
|
|
15
|
+
defaultConnection?: ConnectionOptions;
|
|
16
|
+
defaultPrefix?: string;
|
|
17
|
+
defaultQueueOptions?: Partial<QueueOptions>;
|
|
18
|
+
defaultWorkerOptions?: Partial<WorkerOptions>;
|
|
19
|
+
clearRepeatJobWhenStart?: boolean;
|
|
20
|
+
contextLoggerApplyLogger?: string;
|
|
21
|
+
contextLoggerFormat?: (info: any) => string;
|
|
22
|
+
}
|
|
28
23
|
//# sourceMappingURL=interface.d.ts.map
|
package/index.d.ts
CHANGED
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IQueueOptions, IWorkerOptions } from './dist/index';
|
|
1
|
+
import { BullMQConfig } from './dist/index';
|
|
3
2
|
export * from './dist/index';
|
|
4
|
-
export { Job } from 'bullmq';
|
|
5
3
|
|
|
6
4
|
declare module '@midwayjs/core/dist/interface' {
|
|
7
|
-
// bullmq 新引入了 worker 作为执行任务的实例,一个队列 queue 和 worker 中 connection, prefix 必须一致才能正常执行
|
|
8
|
-
// 所以 config 中 connection, prefix 单独配置
|
|
9
|
-
// eslint-disable-next-line
|
|
10
5
|
interface MidwayConfig {
|
|
11
|
-
bullmq?:
|
|
12
|
-
connection: ConnectionOptions;
|
|
13
|
-
prefix?: string;
|
|
14
|
-
defaultQueueOptions?: IQueueOptions;
|
|
15
|
-
defaultWorkerOptions?: IWorkerOptions;
|
|
16
|
-
clearRepeatJobWhenStart?: boolean;
|
|
17
|
-
contextLoggerFormat?: (info: any) => string;
|
|
18
|
-
};
|
|
6
|
+
bullmq?: BullMQConfig;
|
|
19
7
|
}
|
|
20
8
|
}
|
package/package.json
CHANGED