@midwayjs/bull 3.0.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.
- package/README.md +126 -0
- package/dist/config/config.default.d.ts +16 -0
- package/dist/config/config.default.js +22 -0
- package/dist/configuration.d.ts +8 -0
- package/dist/configuration.js +50 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +8 -0
- package/dist/decorator.d.ts +5 -0
- package/dist/decorator.js +69 -0
- package/dist/framework.d.ts +26 -0
- package/dist/framework.js +113 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +24 -0
- package/dist/interface.d.ts +26 -0
- package/dist/interface.js +3 -0
- package/index.d.ts +13 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @midwayjs/bull
|
|
2
|
+
|
|
3
|
+
## 简介
|
|
4
|
+
|
|
5
|
+
midwayjs/bull 是为了能解决任务系列的模块,例如分布式定时任务、延迟任务调度。例如订单2小时后失效、每日定时的数据处理等工作。
|
|
6
|
+
|
|
7
|
+
## 安装方法
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
tnpm install @midwayjs/bull -S
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用方法
|
|
14
|
+
|
|
15
|
+
在Configuration.ts 导入组件
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
|
|
19
|
+
import * as bull from '@midwayjs/bull';
|
|
20
|
+
|
|
21
|
+
@Configuration({
|
|
22
|
+
imports: [bull],
|
|
23
|
+
importConfigs: [
|
|
24
|
+
join(__dirname, 'config')
|
|
25
|
+
]
|
|
26
|
+
})
|
|
27
|
+
export class AutoConfiguration{
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
配置:
|
|
32
|
+
|
|
33
|
+
在 config.default.ts 文件中配置对应的模块信息:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
export const bull = {
|
|
37
|
+
defaultQueueOptions: {
|
|
38
|
+
redis: `redis://127.0.0.1:6379`,
|
|
39
|
+
prefix: 'midway-task',
|
|
40
|
+
}
|
|
41
|
+
defaultJobOptions: {
|
|
42
|
+
repeat: {
|
|
43
|
+
tz: "Asia/Shanghai"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 业务代码编写方式
|
|
50
|
+
|
|
51
|
+
定义一个任务处理器
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
@Processor('test')
|
|
55
|
+
export class UserServiceProcessor {
|
|
56
|
+
@Inject()
|
|
57
|
+
helloService: HelloService;
|
|
58
|
+
|
|
59
|
+
async execute(data: any) {
|
|
60
|
+
console.log(this.helloService.getName());
|
|
61
|
+
|
|
62
|
+
// console data
|
|
63
|
+
console.log(data);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
在需要的地方手动执行
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import * as bull from '@midwayjs/bull';
|
|
72
|
+
|
|
73
|
+
@Configuration({
|
|
74
|
+
imports: [bull],
|
|
75
|
+
importConfigs: [
|
|
76
|
+
join(__dirname, 'config')
|
|
77
|
+
]
|
|
78
|
+
})
|
|
79
|
+
export class AutoConfiguration {
|
|
80
|
+
@Inject()
|
|
81
|
+
bullFramework: bull.BullFramework;
|
|
82
|
+
|
|
83
|
+
async onServerReady() {
|
|
84
|
+
bullFramework.getQueue('test').runJob({
|
|
85
|
+
name: 'test'
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
定时执行任务:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
@Processor('test', {
|
|
96
|
+
repeat: {
|
|
97
|
+
cron: '0 0 0 * * *'
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
export class UserServiceProcessor {
|
|
101
|
+
@Inject()
|
|
102
|
+
helloService: HelloService;
|
|
103
|
+
|
|
104
|
+
async execute(data: any) {
|
|
105
|
+
console.log(this.helloService.getName());
|
|
106
|
+
|
|
107
|
+
// console data
|
|
108
|
+
console.log(data);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 其他
|
|
114
|
+
|
|
115
|
+
关于task任务的配置:
|
|
116
|
+
```
|
|
117
|
+
* * * * * *
|
|
118
|
+
┬ ┬ ┬ ┬ ┬ ┬
|
|
119
|
+
│ │ │ │ │ |
|
|
120
|
+
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
|
|
121
|
+
│ │ │ │ └───── month (1 - 12)
|
|
122
|
+
│ │ │ └────────── day of month (1 - 31)
|
|
123
|
+
│ │ └─────────────── hour (0 - 23)
|
|
124
|
+
│ └──────────────────── minute (0 - 59)
|
|
125
|
+
└───────────────────────── second (0 - 59, optional)
|
|
126
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const bull: {
|
|
2
|
+
defaultQueueOptions: {
|
|
3
|
+
prefix: string;
|
|
4
|
+
};
|
|
5
|
+
contextLoggerApplyLogger: string;
|
|
6
|
+
contextLoggerFormat: (info: any) => string;
|
|
7
|
+
defaultConcurrency: number;
|
|
8
|
+
};
|
|
9
|
+
export declare const midwayLogger: {
|
|
10
|
+
clients: {
|
|
11
|
+
bullLogger: {
|
|
12
|
+
fileLogName: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=config.default.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midwayLogger = exports.bull = void 0;
|
|
4
|
+
exports.bull = {
|
|
5
|
+
defaultQueueOptions: {
|
|
6
|
+
prefix: 'midway-task',
|
|
7
|
+
},
|
|
8
|
+
contextLoggerApplyLogger: 'bullLogger',
|
|
9
|
+
contextLoggerFormat: info => {
|
|
10
|
+
const { jobId, triggerName } = info.ctx;
|
|
11
|
+
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${triggerName}] ${info.message}`;
|
|
12
|
+
},
|
|
13
|
+
defaultConcurrency: 1,
|
|
14
|
+
};
|
|
15
|
+
exports.midwayLogger = {
|
|
16
|
+
clients: {
|
|
17
|
+
bullLogger: {
|
|
18
|
+
fileLogName: 'midway-bull.log',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=config.default.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BullFramework } from './framework';
|
|
2
|
+
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
3
|
+
export declare class BullConfiguration {
|
|
4
|
+
framework: BullFramework;
|
|
5
|
+
decoratorService: MidwayDecoratorService;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
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 decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const DefaultConfig = require("./config/config.default");
|
|
15
|
+
const framework_1 = require("./framework");
|
|
16
|
+
const core_1 = require("@midwayjs/core");
|
|
17
|
+
const constants_1 = require("./constants");
|
|
18
|
+
let BullConfiguration = class BullConfiguration {
|
|
19
|
+
async init() {
|
|
20
|
+
this.decoratorService.registerPropertyHandler(constants_1.BULL_QUEUE_KEY, (propertyName, meta) => {
|
|
21
|
+
return this.framework.getQueue(meta.queueName);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
__decorate([
|
|
26
|
+
(0, decorator_1.Inject)(),
|
|
27
|
+
__metadata("design:type", framework_1.BullFramework)
|
|
28
|
+
], BullConfiguration.prototype, "framework", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, decorator_1.Inject)(),
|
|
31
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
32
|
+
], BullConfiguration.prototype, "decoratorService", void 0);
|
|
33
|
+
__decorate([
|
|
34
|
+
(0, decorator_1.Init)(),
|
|
35
|
+
__metadata("design:type", Function),
|
|
36
|
+
__metadata("design:paramtypes", []),
|
|
37
|
+
__metadata("design:returntype", Promise)
|
|
38
|
+
], BullConfiguration.prototype, "init", null);
|
|
39
|
+
BullConfiguration = __decorate([
|
|
40
|
+
(0, decorator_1.Configuration)({
|
|
41
|
+
namespace: 'task',
|
|
42
|
+
importConfigs: [
|
|
43
|
+
{
|
|
44
|
+
default: DefaultConfig,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
})
|
|
48
|
+
], BullConfiguration);
|
|
49
|
+
exports.BullConfiguration = BullConfiguration;
|
|
50
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BULL_PROCESSOR_KEY = exports.BULL_QUEUE_KEY = void 0;
|
|
4
|
+
// task
|
|
5
|
+
exports.BULL_QUEUE_KEY = 'bull:queue';
|
|
6
|
+
exports.BULL_PROCESSOR_KEY = 'bull:processor';
|
|
7
|
+
// export const BULL_REPEATABLE_PROCESSOR_KEY = 'bull:repeatableProcessor';
|
|
8
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { JobOptions } from 'bull';
|
|
2
|
+
export declare function Processor(queueName: string, Options?: JobOptions): ClassDecorator;
|
|
3
|
+
export declare function Processor(queueName: string, concurrency?: number, jobOptions?: JobOptions): ClassDecorator;
|
|
4
|
+
export declare function InjectQueue(queueName: string): PropertyDecorator;
|
|
5
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectQueue = exports.Processor = void 0;
|
|
4
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
function Processor(queueName, concurrency, jobOptions) {
|
|
7
|
+
return function (target) {
|
|
8
|
+
if (typeof concurrency !== 'number') {
|
|
9
|
+
jobOptions = concurrency;
|
|
10
|
+
concurrency = 1;
|
|
11
|
+
}
|
|
12
|
+
(0, decorator_1.saveModule)(constants_1.BULL_PROCESSOR_KEY, target);
|
|
13
|
+
(0, decorator_1.saveClassMetadata)(constants_1.BULL_PROCESSOR_KEY, {
|
|
14
|
+
queueName,
|
|
15
|
+
concurrency,
|
|
16
|
+
jobOptions,
|
|
17
|
+
}, target);
|
|
18
|
+
(0, decorator_1.Provide)()(target);
|
|
19
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Request)(target);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.Processor = Processor;
|
|
23
|
+
function InjectQueue(queueName) {
|
|
24
|
+
return (0, decorator_1.createCustomPropertyDecorator)(constants_1.BULL_QUEUE_KEY, {
|
|
25
|
+
queueName,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exports.InjectQueue = InjectQueue;
|
|
29
|
+
//
|
|
30
|
+
// export function Queue(queueOptions?: QueueOptions): ClassDecorator;
|
|
31
|
+
// export function Queue(
|
|
32
|
+
// queueName?: string,
|
|
33
|
+
// queueOptions?: QueueOptions
|
|
34
|
+
// ): ClassDecorator;
|
|
35
|
+
// export function Queue(
|
|
36
|
+
// queueName?: string,
|
|
37
|
+
// concurrency?: number,
|
|
38
|
+
// queueOptions?: QueueOptions
|
|
39
|
+
// ): ClassDecorator;
|
|
40
|
+
// export function Queue(
|
|
41
|
+
// queueName?: any,
|
|
42
|
+
// concurrency?: any,
|
|
43
|
+
// queueOptions?: QueueOptions
|
|
44
|
+
// ): ClassDecorator {
|
|
45
|
+
// return function (target) {
|
|
46
|
+
// if (typeof queueName !== 'string') {
|
|
47
|
+
// queueOptions = queueName;
|
|
48
|
+
// queueName = `${target.name}:execute`;
|
|
49
|
+
// concurrency = 1;
|
|
50
|
+
// }
|
|
51
|
+
// if (typeof concurrency !== 'number') {
|
|
52
|
+
// queueOptions = concurrency;
|
|
53
|
+
// concurrency = 1;
|
|
54
|
+
// }
|
|
55
|
+
// saveModule(BULL_QUEUE_KEY, target);
|
|
56
|
+
// saveClassMetadata(
|
|
57
|
+
// BULL_QUEUE_KEY,
|
|
58
|
+
// {
|
|
59
|
+
// queueName,
|
|
60
|
+
// queueOptions,
|
|
61
|
+
// concurrency,
|
|
62
|
+
// },
|
|
63
|
+
// target
|
|
64
|
+
// );
|
|
65
|
+
// Provide()(target);
|
|
66
|
+
// Scope(ScopeEnum.Singleton)(target);
|
|
67
|
+
// };
|
|
68
|
+
// }
|
|
69
|
+
//# sourceMappingURL=decorator.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BaseFramework, IMidwayBootstrapOptions } from '@midwayjs/core';
|
|
2
|
+
import { Application, Context, IProcessor, IQueue, IQueueManager } from './interface';
|
|
3
|
+
import { Job, JobOptions, QueueOptions } from 'bull';
|
|
4
|
+
import * as Bull from 'bull';
|
|
5
|
+
export declare class BullQueue extends Bull implements IQueue<Job> {
|
|
6
|
+
constructor(queueName: string, queueOptions: QueueOptions);
|
|
7
|
+
runJob(data: Record<string, any>, options?: JobOptions): Promise<Job<any>>;
|
|
8
|
+
getQueueName(): string;
|
|
9
|
+
}
|
|
10
|
+
export declare class BullFramework extends BaseFramework<Application, Context, any> implements IQueueManager<BullQueue, Job> {
|
|
11
|
+
private bullDefaultQueueConfig;
|
|
12
|
+
private bullDefaultConcurrency;
|
|
13
|
+
private queueMap;
|
|
14
|
+
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
15
|
+
configure(): any;
|
|
16
|
+
getFrameworkName(): string;
|
|
17
|
+
run(): Promise<void>;
|
|
18
|
+
protected beforeStop(): Promise<void>;
|
|
19
|
+
createQueue(name: string, queueOptions?: QueueOptions): BullQueue;
|
|
20
|
+
getQueue(name: string): BullQueue;
|
|
21
|
+
ensureQueue(name: string): BullQueue;
|
|
22
|
+
addProcessor(processor: new (...args: any[]) => IProcessor, queueName: string | BullQueue, concurrency?: number): Promise<void>;
|
|
23
|
+
runJob(queueName: string, jobData: any, options?: JobOptions): Promise<void>;
|
|
24
|
+
getJob(queueName: string, jobName: string): Promise<Job>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=framework.d.ts.map
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.BullFramework = exports.BullQueue = void 0;
|
|
10
|
+
const core_1 = require("@midwayjs/core");
|
|
11
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
12
|
+
const Bull = require("bull");
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
class BullQueue extends Bull {
|
|
15
|
+
constructor(queueName, queueOptions) {
|
|
16
|
+
super(queueName, queueOptions);
|
|
17
|
+
}
|
|
18
|
+
async runJob(data, options) {
|
|
19
|
+
return this.add(data || {}, options);
|
|
20
|
+
}
|
|
21
|
+
getQueueName() {
|
|
22
|
+
return this.name;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.BullQueue = BullQueue;
|
|
26
|
+
let BullFramework = class BullFramework extends core_1.BaseFramework {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
29
|
+
this.queueMap = new Map();
|
|
30
|
+
}
|
|
31
|
+
async applicationInitialize(options) {
|
|
32
|
+
this.app = {};
|
|
33
|
+
this.bullDefaultQueueConfig = this.configService.getConfiguration('bull.defaultQueueOptions');
|
|
34
|
+
this.bullDefaultConcurrency = this.configService.getConfiguration('bull.defaultConcurrency');
|
|
35
|
+
}
|
|
36
|
+
configure() {
|
|
37
|
+
return this.configService.getConfiguration('bull');
|
|
38
|
+
}
|
|
39
|
+
getFrameworkName() {
|
|
40
|
+
return 'bull';
|
|
41
|
+
}
|
|
42
|
+
async run() {
|
|
43
|
+
var _a;
|
|
44
|
+
const processorModules = (0, decorator_1.listModule)(constants_1.BULL_PROCESSOR_KEY);
|
|
45
|
+
for (const mod of processorModules) {
|
|
46
|
+
const options = (0, decorator_1.getClassMetadata)(constants_1.BULL_PROCESSOR_KEY, mod);
|
|
47
|
+
this.ensureQueue(options.queueName);
|
|
48
|
+
await this.addProcessor(mod, options.queueName, options.concurrency);
|
|
49
|
+
if ((_a = options.jobOptions) === null || _a === void 0 ? void 0 : _a.repeat) {
|
|
50
|
+
await this.runJob(options.queueName, {}, options.jobOptions);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async beforeStop() {
|
|
55
|
+
// loop queueMap and stop all queue
|
|
56
|
+
for (const queue of this.queueMap.values()) {
|
|
57
|
+
await queue.close();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
createQueue(name, queueOptions = {}) {
|
|
61
|
+
const queue = new BullQueue(name, (0, core_1.extend)(true, this.bullDefaultQueueConfig, queueOptions));
|
|
62
|
+
this.queueMap.set(name, queue);
|
|
63
|
+
return queue;
|
|
64
|
+
}
|
|
65
|
+
getQueue(name) {
|
|
66
|
+
return this.queueMap.get(name);
|
|
67
|
+
}
|
|
68
|
+
ensureQueue(name) {
|
|
69
|
+
if (!this.queueMap.has(name)) {
|
|
70
|
+
this.createQueue(name);
|
|
71
|
+
}
|
|
72
|
+
return this.queueMap.get(name);
|
|
73
|
+
}
|
|
74
|
+
async addProcessor(processor, queueName, concurrency) {
|
|
75
|
+
const queue = typeof queueName === 'string' ? this.queueMap.get(queueName) : queueName;
|
|
76
|
+
queue.process(concurrency !== null && concurrency !== void 0 ? concurrency : this.bullDefaultConcurrency, async (job) => {
|
|
77
|
+
const ctx = this.app.createAnonymousContext({
|
|
78
|
+
jobId: job.id,
|
|
79
|
+
job,
|
|
80
|
+
triggerName: (0, decorator_1.getProviderName)(processor),
|
|
81
|
+
triggerUUID: (0, decorator_1.getProviderUUId)(processor),
|
|
82
|
+
});
|
|
83
|
+
const service = await ctx.requestContext.getAsync(processor);
|
|
84
|
+
const fn = await this.applyMiddleware(async (ctx) => {
|
|
85
|
+
return await decorator_1.Utils.toAsyncFunction(service.execute.bind(service))(job.data, job);
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
return Promise.resolve(await fn(ctx));
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
ctx.logger.error(err);
|
|
92
|
+
return Promise.reject(err);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async runJob(queueName, jobData, options) {
|
|
97
|
+
const queue = this.queueMap.get(queueName);
|
|
98
|
+
if (queue) {
|
|
99
|
+
await queue.runJob(jobData, options);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async getJob(queueName, jobName) {
|
|
103
|
+
const queue = this.queueMap.get(queueName);
|
|
104
|
+
if (queue) {
|
|
105
|
+
return queue.getJob(jobName);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
BullFramework = __decorate([
|
|
110
|
+
(0, decorator_1.Framework)()
|
|
111
|
+
], BullFramework);
|
|
112
|
+
exports.BullFramework = BullFramework;
|
|
113
|
+
//# sourceMappingURL=framework.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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.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.BullFramework; } });
|
|
22
|
+
__exportStar(require("./decorator"), exports);
|
|
23
|
+
__exportStar(require("./interface"), exports);
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
2
|
+
import { JobId, Job } from 'bull';
|
|
3
|
+
export interface IProcessor {
|
|
4
|
+
execute(data: any): any;
|
|
5
|
+
}
|
|
6
|
+
export interface IQueue<Job> {
|
|
7
|
+
runJob(data: Record<string, any>, options?: unknown): any;
|
|
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): any;
|
|
13
|
+
getJob(queueName: string, jobName: string): Promise<Job>;
|
|
14
|
+
createQueue(queueName: string, queueOptions?: unknown): Queue;
|
|
15
|
+
getQueue(queueName: string): Queue;
|
|
16
|
+
}
|
|
17
|
+
export interface Application extends IMidwayApplication<Context> {
|
|
18
|
+
}
|
|
19
|
+
export declare type NextFunction = BaseNextFunction;
|
|
20
|
+
export interface Context extends IMidwayContext {
|
|
21
|
+
jobId: JobId;
|
|
22
|
+
job: Job;
|
|
23
|
+
triggerName: string;
|
|
24
|
+
triggerUUID: string;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as bull from 'bull';
|
|
2
|
+
export * from './dist/index';
|
|
3
|
+
|
|
4
|
+
declare module '@midwayjs/core/dist/interface' {
|
|
5
|
+
// eslint-disable-next-line
|
|
6
|
+
interface MidwayConfig {
|
|
7
|
+
bull?: {
|
|
8
|
+
defaultQueueOptions?: bull.QueueOptions;
|
|
9
|
+
defaultJobOptions?: bull.JobOptions;
|
|
10
|
+
defaultConcurrency?: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/bull",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "midway component for bull",
|
|
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
|
+
"bull",
|
|
17
|
+
"plugin"
|
|
18
|
+
],
|
|
19
|
+
"author": "",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/**/*.js",
|
|
22
|
+
"dist/**/*.d.ts",
|
|
23
|
+
"index.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@midwayjs/core": "^3.4.13",
|
|
28
|
+
"@midwayjs/decorator": "^3.4.11",
|
|
29
|
+
"@midwayjs/mock": "^3.4.13"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"bull": "4.8.5",
|
|
33
|
+
"@types/bull": "3.15.9"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=12"
|
|
37
|
+
}
|
|
38
|
+
}
|