@midwayjs/bullmq 0.0.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/README.md +9 -0
- package/dist/config/config.default.d.ts +23 -0
- package/dist/config/config.default.js +29 -0
- package/dist/configuration.d.ts +9 -0
- package/dist/configuration.js +52 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +7 -0
- package/dist/decorator.d.ts +5 -0
- package/dist/decorator.js +26 -0
- package/dist/framework.d.ts +35 -0
- package/dist/framework.js +187 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +25 -0
- package/dist/interface.d.ts +28 -0
- package/dist/interface.js +3 -0
- package/index.d.ts +20 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const bullmq: {
|
|
2
|
+
prefix: string;
|
|
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
|
+
};
|
|
16
|
+
export declare const midwayLogger: {
|
|
17
|
+
clients: {
|
|
18
|
+
bullMQLogger: {
|
|
19
|
+
fileLogName: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=config.default.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midwayLogger = exports.bullmq = void 0;
|
|
4
|
+
exports.bullmq = {
|
|
5
|
+
prefix: '{midway-bullmq}',
|
|
6
|
+
defaultQueueOptions: {
|
|
7
|
+
defaultJobOptions: {
|
|
8
|
+
removeOnComplete: 3,
|
|
9
|
+
removeOnFail: 10,
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
defaultWorkerOptions: {
|
|
13
|
+
concurrency: 1,
|
|
14
|
+
},
|
|
15
|
+
clearRepeatJobWhenStart: true,
|
|
16
|
+
contextLoggerApplyLogger: 'bullMQLogger',
|
|
17
|
+
contextLoggerFormat: info => {
|
|
18
|
+
const { jobId, from } = info.ctx;
|
|
19
|
+
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}] ${info.message}`;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
exports.midwayLogger = {
|
|
23
|
+
clients: {
|
|
24
|
+
bullMQLogger: {
|
|
25
|
+
fileLogName: 'midway-bullmq.log',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=config.default.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
2
|
+
import { BullMQFramework } from './framework';
|
|
3
|
+
export declare class BullConfiguration {
|
|
4
|
+
framework: BullMQFramework;
|
|
5
|
+
decoratorService: MidwayDecoratorService;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
onReady(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,52 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BullConfiguration = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const DefaultConfig = require("./config/config.default");
|
|
15
|
+
const framework_1 = require("./framework");
|
|
16
|
+
const constants_1 = require("./constants");
|
|
17
|
+
let BullConfiguration = class BullConfiguration {
|
|
18
|
+
async init() {
|
|
19
|
+
this.decoratorService.registerPropertyHandler(constants_1.BULLMQ_QUEUE_KEY, (propertyName, meta) => {
|
|
20
|
+
return this.framework.getQueue(meta.queueName);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
async onReady() {
|
|
24
|
+
this.framework.loadConfig();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
__decorate([
|
|
28
|
+
(0, core_1.Inject)(),
|
|
29
|
+
__metadata("design:type", framework_1.BullMQFramework)
|
|
30
|
+
], BullConfiguration.prototype, "framework", void 0);
|
|
31
|
+
__decorate([
|
|
32
|
+
(0, core_1.Inject)(),
|
|
33
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
34
|
+
], BullConfiguration.prototype, "decoratorService", void 0);
|
|
35
|
+
__decorate([
|
|
36
|
+
(0, core_1.Init)(),
|
|
37
|
+
__metadata("design:type", Function),
|
|
38
|
+
__metadata("design:paramtypes", []),
|
|
39
|
+
__metadata("design:returntype", Promise)
|
|
40
|
+
], BullConfiguration.prototype, "init", null);
|
|
41
|
+
BullConfiguration = __decorate([
|
|
42
|
+
(0, core_1.Configuration)({
|
|
43
|
+
namespace: 'bullmq',
|
|
44
|
+
importConfigs: [
|
|
45
|
+
{
|
|
46
|
+
default: DefaultConfig,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
})
|
|
50
|
+
], BullConfiguration);
|
|
51
|
+
exports.BullConfiguration = BullConfiguration;
|
|
52
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BULLMQ_PROCESSOR_KEY = exports.BULLMQ_QUEUE_KEY = void 0;
|
|
4
|
+
// task
|
|
5
|
+
exports.BULLMQ_QUEUE_KEY = 'bullmq:queue';
|
|
6
|
+
exports.BULLMQ_PROCESSOR_KEY = 'bullmq:processor';
|
|
7
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IQueueOptions, IWorkerOptions } from './interface';
|
|
2
|
+
import { JobsOptions } from 'bullmq';
|
|
3
|
+
export declare function Processor(queueName: string, jobOptions?: JobsOptions, workerOptions?: IWorkerOptions, queueOptions?: IQueueOptions): ClassDecorator;
|
|
4
|
+
export declare function InjectQueue(queueName: string): PropertyDecorator;
|
|
5
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectQueue = exports.Processor = void 0;
|
|
4
|
+
const core_1 = require("@midwayjs/core");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
function Processor(queueName, jobOptions, workerOptions, queueOptions) {
|
|
7
|
+
return function (target) {
|
|
8
|
+
(0, core_1.saveModule)(constants_1.BULLMQ_PROCESSOR_KEY, target);
|
|
9
|
+
(0, core_1.saveClassMetadata)(constants_1.BULLMQ_PROCESSOR_KEY, {
|
|
10
|
+
queueName,
|
|
11
|
+
jobOptions,
|
|
12
|
+
queueOptions,
|
|
13
|
+
workerOptions,
|
|
14
|
+
}, target);
|
|
15
|
+
(0, core_1.Provide)()(target);
|
|
16
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Request)(target);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.Processor = Processor;
|
|
20
|
+
function InjectQueue(queueName) {
|
|
21
|
+
return (0, core_1.createCustomPropertyDecorator)(constants_1.BULLMQ_QUEUE_KEY, {
|
|
22
|
+
queueName,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
exports.InjectQueue = InjectQueue;
|
|
26
|
+
//# sourceMappingURL=decorator.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { BaseFramework, IMidwayBootstrapOptions, ILogger } from '@midwayjs/core';
|
|
2
|
+
import { Application, Context, IProcessor, IQueue, IQueueManager, IQueueOptions, IWorkerOptions } from './interface';
|
|
3
|
+
import { Job, JobsOptions, Queue, Worker, QueueOptions } from 'bullmq';
|
|
4
|
+
export declare class BullMQQueue extends Queue implements IQueue<Job> {
|
|
5
|
+
constructor(queueName: string, queueOptions: QueueOptions);
|
|
6
|
+
getJob(name: string): Promise<Job>;
|
|
7
|
+
runJob(data: any, options?: JobsOptions): Promise<Job>;
|
|
8
|
+
getQueueName(): string;
|
|
9
|
+
}
|
|
10
|
+
export declare class BullMQFramework extends BaseFramework<Application, Context, any> implements IQueueManager<BullMQQueue, Job> {
|
|
11
|
+
private connection;
|
|
12
|
+
private prefix?;
|
|
13
|
+
private defaultQueueConfig;
|
|
14
|
+
private defaultWorkerConfig;
|
|
15
|
+
private clearRepeatJobWhenStart;
|
|
16
|
+
private queueMap;
|
|
17
|
+
/** keep a map of workers for gracefully shutdown */
|
|
18
|
+
private workerMap;
|
|
19
|
+
protected bullMQLogger: ILogger;
|
|
20
|
+
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
21
|
+
loadConfig(): void;
|
|
22
|
+
configure(): any;
|
|
23
|
+
getFrameworkName(): string;
|
|
24
|
+
run(): Promise<void>;
|
|
25
|
+
protected beforeStop(): Promise<void>;
|
|
26
|
+
createQueue(name: string, queueOptions: IQueueOptions): BullMQQueue;
|
|
27
|
+
getQueue(name: string): BullMQQueue;
|
|
28
|
+
ensureQueue(name: string, queueOptions: IQueueOptions): BullMQQueue;
|
|
29
|
+
getQueueList(): BullMQQueue[];
|
|
30
|
+
getWorker(name: string): Worker<any, any, string>;
|
|
31
|
+
addProcessor(processor: new (...args: any[]) => IProcessor, queueName: string, workerOptions?: IWorkerOptions): Promise<void>;
|
|
32
|
+
runJob(queueName: string, jobData: any, options?: JobsOptions): Promise<Job | undefined>;
|
|
33
|
+
getJob(queueName: string, jobName: string): Promise<Job | undefined>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=framework.d.ts.map
|
|
@@ -0,0 +1,187 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BullMQFramework = exports.BullMQQueue = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const bullmq_1 = require("bullmq");
|
|
15
|
+
const constants_1 = require("./constants");
|
|
16
|
+
class BullMQQueue extends bullmq_1.Queue {
|
|
17
|
+
constructor(queueName, queueOptions) {
|
|
18
|
+
super(queueName, queueOptions);
|
|
19
|
+
}
|
|
20
|
+
getJob(name) {
|
|
21
|
+
throw new Error('Method not implemented.');
|
|
22
|
+
}
|
|
23
|
+
// bullmq 在 queue.add 新增第一个参数 jobName
|
|
24
|
+
// runJob 与 @midwayjs/bull 保持一致,如果想要使用 jobName 则可以直接调用 queue.add
|
|
25
|
+
async runJob(data, options) {
|
|
26
|
+
const { repeat, ...OtherOptions } = options !== null && options !== void 0 ? options : {};
|
|
27
|
+
if (repeat) {
|
|
28
|
+
return this.upsertJobScheduler(this.name, repeat, {
|
|
29
|
+
name: 'jobName',
|
|
30
|
+
data,
|
|
31
|
+
opts: OtherOptions, // additional job options
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return this.add('jobName', data || {}, options);
|
|
35
|
+
}
|
|
36
|
+
getQueueName() {
|
|
37
|
+
return this.name;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.BullMQQueue = BullMQQueue;
|
|
41
|
+
let BullMQFramework = class BullMQFramework extends core_1.BaseFramework {
|
|
42
|
+
constructor() {
|
|
43
|
+
super(...arguments);
|
|
44
|
+
this.queueMap = new Map();
|
|
45
|
+
/** keep a map of workers for gracefully shutdown */
|
|
46
|
+
this.workerMap = new Map();
|
|
47
|
+
}
|
|
48
|
+
async applicationInitialize(options) {
|
|
49
|
+
this.app = {};
|
|
50
|
+
}
|
|
51
|
+
loadConfig() {
|
|
52
|
+
this.connection = this.configService.getConfiguration('bullmq.connection');
|
|
53
|
+
this.prefix = this.configService.getConfiguration('bullmq.prefix');
|
|
54
|
+
this.defaultQueueConfig = this.configService.getConfiguration('bullmq.defaultQueueOptions');
|
|
55
|
+
this.defaultWorkerConfig = this.configService.getConfiguration('bullmq.defaultWorkerOptions');
|
|
56
|
+
this.clearRepeatJobWhenStart = this.configService.getConfiguration('bullmq.clearRepeatJobWhenStart');
|
|
57
|
+
}
|
|
58
|
+
configure() {
|
|
59
|
+
return this.configService.getConfiguration('bullmq');
|
|
60
|
+
}
|
|
61
|
+
getFrameworkName() {
|
|
62
|
+
return 'bullmq';
|
|
63
|
+
}
|
|
64
|
+
async run() {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
const processorModules = (0, core_1.listModule)(constants_1.BULLMQ_PROCESSOR_KEY);
|
|
67
|
+
for (const mod of processorModules) {
|
|
68
|
+
const options = (0, core_1.getClassMetadata)(constants_1.BULLMQ_PROCESSOR_KEY, mod);
|
|
69
|
+
const { repeat, ...otherOptions } = (_a = options.jobOptions) !== null && _a !== void 0 ? _a : {};
|
|
70
|
+
const queueOptions = (_b = options.queueOptions) !== null && _b !== void 0 ? _b : {};
|
|
71
|
+
const currentQueue = this.ensureQueue(options.queueName, {
|
|
72
|
+
...queueOptions,
|
|
73
|
+
defaultJobOptions: otherOptions,
|
|
74
|
+
});
|
|
75
|
+
if (!currentQueue)
|
|
76
|
+
throw Error('ensureQueue failed');
|
|
77
|
+
// clear old repeat job when start
|
|
78
|
+
if (this.clearRepeatJobWhenStart) {
|
|
79
|
+
const jobs = await currentQueue.getJobSchedulers();
|
|
80
|
+
for (const job of jobs) {
|
|
81
|
+
await currentQueue.removeJobScheduler(job.key);
|
|
82
|
+
}
|
|
83
|
+
// Repeatable in jobOptions is depecrate
|
|
84
|
+
const repeatableJobs = await currentQueue.getRepeatableJobs();
|
|
85
|
+
for (const job of repeatableJobs) {
|
|
86
|
+
await currentQueue.removeRepeatableByKey(job.key);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
await this.addProcessor(mod, options.queueName, options.workerOptions);
|
|
90
|
+
if (repeat) {
|
|
91
|
+
await this.runJob(options.queueName, {}, options.jobOptions);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async beforeStop() {
|
|
96
|
+
// loop queueMap and stop all queue
|
|
97
|
+
for (const queue of this.queueMap.values()) {
|
|
98
|
+
await queue.close();
|
|
99
|
+
}
|
|
100
|
+
for (const worker of this.workerMap.values()) {
|
|
101
|
+
await worker.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
createQueue(name, queueOptions) {
|
|
105
|
+
const queue = new BullMQQueue(name, (0, core_1.extend)(true, {}, this.defaultQueueConfig, queueOptions, {
|
|
106
|
+
connection: this.connection,
|
|
107
|
+
prefix: this.prefix,
|
|
108
|
+
}));
|
|
109
|
+
this.queueMap.set(name, queue);
|
|
110
|
+
queue.on('error', err => {
|
|
111
|
+
this.bullMQLogger.error(err);
|
|
112
|
+
});
|
|
113
|
+
return queue;
|
|
114
|
+
}
|
|
115
|
+
getQueue(name) {
|
|
116
|
+
return this.queueMap.get(name);
|
|
117
|
+
}
|
|
118
|
+
ensureQueue(name, queueOptions) {
|
|
119
|
+
if (!this.queueMap.has(name)) {
|
|
120
|
+
this.createQueue(name, queueOptions);
|
|
121
|
+
}
|
|
122
|
+
return this.queueMap.get(name);
|
|
123
|
+
}
|
|
124
|
+
getQueueList() {
|
|
125
|
+
return Array.from(this.queueMap.values());
|
|
126
|
+
}
|
|
127
|
+
getWorker(name) {
|
|
128
|
+
return this.workerMap.get(name);
|
|
129
|
+
}
|
|
130
|
+
async addProcessor(processor, queueName, workerOptions) {
|
|
131
|
+
const queue = this.queueMap.get(queueName);
|
|
132
|
+
if (!queue)
|
|
133
|
+
throw Error(`queue not found ${queueName}`);
|
|
134
|
+
const worker = new bullmq_1.Worker(queueName, async (job) => {
|
|
135
|
+
const ctx = this.app.createAnonymousContext({
|
|
136
|
+
jobId: job.id,
|
|
137
|
+
job,
|
|
138
|
+
from: processor,
|
|
139
|
+
});
|
|
140
|
+
try {
|
|
141
|
+
ctx.logger.info(`start process job ${job.id} from ${processor.name}`);
|
|
142
|
+
const isPassed = await this.app
|
|
143
|
+
.getFramework()
|
|
144
|
+
.runGuard(ctx, processor, 'execute');
|
|
145
|
+
if (!isPassed) {
|
|
146
|
+
throw new core_1.MidwayInvokeForbiddenError('execute', processor);
|
|
147
|
+
}
|
|
148
|
+
const service = await ctx.requestContext.getAsync(processor);
|
|
149
|
+
const fn = await this.applyMiddleware(async (ctx) => {
|
|
150
|
+
return await core_1.Utils.toAsyncFunction(service.execute.bind(service))(job.data, job);
|
|
151
|
+
});
|
|
152
|
+
const result = await Promise.resolve(await fn(ctx));
|
|
153
|
+
ctx.logger.info(`complete process job ${job.id} from ${processor.name}`);
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
ctx.logger.error(err);
|
|
158
|
+
return Promise.reject(err);
|
|
159
|
+
}
|
|
160
|
+
}, (0, core_1.extend)(true, {}, this.defaultWorkerConfig, workerOptions, {
|
|
161
|
+
connection: this.connection,
|
|
162
|
+
prefix: this.prefix,
|
|
163
|
+
}));
|
|
164
|
+
this.workerMap.set(queueName, worker);
|
|
165
|
+
}
|
|
166
|
+
async runJob(queueName, jobData, options) {
|
|
167
|
+
const queue = this.queueMap.get(queueName);
|
|
168
|
+
if (queue) {
|
|
169
|
+
return await queue.runJob(jobData, options);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async getJob(queueName, jobName) {
|
|
173
|
+
const queue = this.queueMap.get(queueName);
|
|
174
|
+
if (queue) {
|
|
175
|
+
return queue.getJob(jobName);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
__decorate([
|
|
180
|
+
(0, core_1.Logger)('bullMQLogger'),
|
|
181
|
+
__metadata("design:type", Object)
|
|
182
|
+
], BullMQFramework.prototype, "bullMQLogger", void 0);
|
|
183
|
+
BullMQFramework = __decorate([
|
|
184
|
+
(0, core_1.Framework)()
|
|
185
|
+
], BullMQFramework);
|
|
186
|
+
exports.BullMQFramework = BullMQFramework;
|
|
187
|
+
//# sourceMappingURL=framework.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.BullMQQueue = exports.Framework = exports.Configuration = void 0;
|
|
18
|
+
var configuration_1 = require("./configuration");
|
|
19
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.BullConfiguration; } });
|
|
20
|
+
var framework_1 = require("./framework");
|
|
21
|
+
Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.BullMQFramework; } });
|
|
22
|
+
Object.defineProperty(exports, "BullMQQueue", { enumerable: true, get: function () { return framework_1.BullMQQueue; } });
|
|
23
|
+
__exportStar(require("./decorator"), exports);
|
|
24
|
+
__exportStar(require("./interface"), exports);
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
2
|
+
import { WorkerOptions, QueueOptions, Job, Worker } from 'bullmq';
|
|
3
|
+
export interface IProcessor {
|
|
4
|
+
execute(data: any): 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;
|
|
17
|
+
}
|
|
18
|
+
export interface Application extends IMidwayApplication<Context> {
|
|
19
|
+
}
|
|
20
|
+
export type NextFunction = BaseNextFunction;
|
|
21
|
+
export interface Context extends IMidwayContext {
|
|
22
|
+
jobId: string;
|
|
23
|
+
job: Job;
|
|
24
|
+
from: new (...args: any[]) => IProcessor;
|
|
25
|
+
}
|
|
26
|
+
export type IWorkerOptions = Omit<WorkerOptions, 'connection' | 'prefix'>;
|
|
27
|
+
export type IQueueOptions = Omit<QueueOptions, 'connection' | 'prefix'>;
|
|
28
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ConnectionOptions } from 'bullmq';
|
|
2
|
+
import { IQueueOptions, IWorkerOptions } from './dist/index';
|
|
3
|
+
export * from './dist/index';
|
|
4
|
+
export { Job } from 'bullmq';
|
|
5
|
+
|
|
6
|
+
declare module '@midwayjs/core/dist/interface' {
|
|
7
|
+
// bullmq 新引入了 worker 作为执行任务的实例,一个队列 queue 和 worker 中 connection, prefix 必须一致才能正常执行
|
|
8
|
+
// 所以 config 中 connection, prefix 单独配置
|
|
9
|
+
// eslint-disable-next-line
|
|
10
|
+
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
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/bullmq",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "midway component for bullmq",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"midway",
|
|
14
|
+
"IoC",
|
|
15
|
+
"task",
|
|
16
|
+
"bullmq",
|
|
17
|
+
"plugin"
|
|
18
|
+
],
|
|
19
|
+
"author": "guo qicong",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/**/*.js",
|
|
22
|
+
"dist/**/*.d.ts",
|
|
23
|
+
"index.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@midwayjs/core": "^3.19.0",
|
|
28
|
+
"@midwayjs/mock": "^3.19.2"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"bullmq": "5.34.6"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=12"
|
|
35
|
+
},
|
|
36
|
+
"repository": "https://github.com/midwayjs/midway.git"
|
|
37
|
+
}
|