@midwayjs/bull 3.0.3 → 3.0.5
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 +4 -121
- package/dist/config/config.default.d.ts +6 -2
- package/dist/config/config.default.js +7 -3
- package/dist/configuration.js +1 -1
- package/dist/decorator.js +0 -40
- package/dist/framework.d.ts +2 -1
- package/dist/framework.js +11 -5
- package/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,126 +1,9 @@
|
|
|
1
1
|
# @midwayjs/bull
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
this is a sub package for midway.
|
|
4
4
|
|
|
5
|
-
midwayjs
|
|
5
|
+
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## License
|
|
8
8
|
|
|
9
|
-
|
|
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
|
-
```
|
|
9
|
+
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
|
|
@@ -2,10 +2,14 @@ export declare const bull: {
|
|
|
2
2
|
defaultQueueOptions: {
|
|
3
3
|
prefix: string;
|
|
4
4
|
};
|
|
5
|
+
defaultJobOptions: {
|
|
6
|
+
removeOnSuccess: boolean;
|
|
7
|
+
removeOnFail: number;
|
|
8
|
+
};
|
|
9
|
+
defaultConcurrency: number;
|
|
10
|
+
clearRepeatJobWhenStart: boolean;
|
|
5
11
|
contextLoggerApplyLogger: string;
|
|
6
12
|
contextLoggerFormat: (info: any) => string;
|
|
7
|
-
defaultConcurrency: number;
|
|
8
|
-
clearJobWhenStart: boolean;
|
|
9
13
|
};
|
|
10
14
|
export declare const midwayLogger: {
|
|
11
15
|
clients: {
|
|
@@ -3,15 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.midwayLogger = exports.bull = void 0;
|
|
4
4
|
exports.bull = {
|
|
5
5
|
defaultQueueOptions: {
|
|
6
|
-
prefix: 'midway-
|
|
6
|
+
prefix: '{midway-bull}',
|
|
7
7
|
},
|
|
8
|
+
defaultJobOptions: {
|
|
9
|
+
removeOnSuccess: true,
|
|
10
|
+
removeOnFail: 10,
|
|
11
|
+
},
|
|
12
|
+
defaultConcurrency: 1,
|
|
13
|
+
clearRepeatJobWhenStart: true,
|
|
8
14
|
contextLoggerApplyLogger: 'bullLogger',
|
|
9
15
|
contextLoggerFormat: info => {
|
|
10
16
|
const { jobId, from } = info.ctx;
|
|
11
17
|
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}}] ${info.message}`;
|
|
12
18
|
},
|
|
13
|
-
defaultConcurrency: 1,
|
|
14
|
-
clearJobWhenStart: true,
|
|
15
19
|
};
|
|
16
20
|
exports.midwayLogger = {
|
|
17
21
|
clients: {
|
package/dist/configuration.js
CHANGED
package/dist/decorator.js
CHANGED
|
@@ -26,44 +26,4 @@ function InjectQueue(queueName) {
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
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
29
|
//# sourceMappingURL=decorator.js.map
|
package/dist/framework.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare class BullQueue extends Bull implements IQueue<Job> {
|
|
|
10
10
|
export declare class BullFramework extends BaseFramework<Application, Context, any> implements IQueueManager<BullQueue, Job> {
|
|
11
11
|
private bullDefaultQueueConfig;
|
|
12
12
|
private bullDefaultConcurrency;
|
|
13
|
-
private
|
|
13
|
+
private bullClearRepeatJobWhenStart;
|
|
14
14
|
private queueMap;
|
|
15
15
|
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
16
16
|
configure(): any;
|
|
@@ -20,6 +20,7 @@ export declare class BullFramework extends BaseFramework<Application, Context, a
|
|
|
20
20
|
createQueue(name: string, queueOptions?: QueueOptions): BullQueue;
|
|
21
21
|
getQueue(name: string): BullQueue;
|
|
22
22
|
ensureQueue(name: string): BullQueue;
|
|
23
|
+
getQueueList(): BullQueue[];
|
|
23
24
|
addProcessor(processor: new (...args: any[]) => IProcessor, queueName: string | BullQueue, concurrency?: number): Promise<void>;
|
|
24
25
|
runJob(queueName: string, jobData: any, options?: JobOptions): Promise<void>;
|
|
25
26
|
getJob(queueName: string, jobName: string): Promise<Job>;
|
package/dist/framework.js
CHANGED
|
@@ -32,7 +32,7 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
|
|
|
32
32
|
this.app = {};
|
|
33
33
|
this.bullDefaultQueueConfig = this.configService.getConfiguration('bull.defaultQueueOptions');
|
|
34
34
|
this.bullDefaultConcurrency = this.configService.getConfiguration('bull.defaultConcurrency');
|
|
35
|
-
this.
|
|
35
|
+
this.bullClearRepeatJobWhenStart = this.configService.getConfiguration('bull.clearRepeatJobWhenStart');
|
|
36
36
|
}
|
|
37
37
|
configure() {
|
|
38
38
|
return this.configService.getConfiguration('bull');
|
|
@@ -45,10 +45,13 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
|
|
|
45
45
|
const processorModules = (0, decorator_1.listModule)(constants_1.BULL_PROCESSOR_KEY);
|
|
46
46
|
for (const mod of processorModules) {
|
|
47
47
|
const options = (0, decorator_1.getClassMetadata)(constants_1.BULL_PROCESSOR_KEY, mod);
|
|
48
|
-
this.ensureQueue(options.queueName);
|
|
49
|
-
// clear old job when start
|
|
50
|
-
if (this.
|
|
51
|
-
await
|
|
48
|
+
const currentQueue = this.ensureQueue(options.queueName);
|
|
49
|
+
// clear old repeat job when start
|
|
50
|
+
if (this.bullClearRepeatJobWhenStart) {
|
|
51
|
+
const jobs = await currentQueue.getRepeatableJobs();
|
|
52
|
+
for (const job of jobs) {
|
|
53
|
+
await currentQueue.removeRepeatableByKey(job.key);
|
|
54
|
+
}
|
|
52
55
|
}
|
|
53
56
|
await this.addProcessor(mod, options.queueName, options.concurrency);
|
|
54
57
|
if ((_a = options.jobOptions) === null || _a === void 0 ? void 0 : _a.repeat) {
|
|
@@ -76,6 +79,9 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
|
|
|
76
79
|
}
|
|
77
80
|
return this.queueMap.get(name);
|
|
78
81
|
}
|
|
82
|
+
getQueueList() {
|
|
83
|
+
return Array.from(this.queueMap.values());
|
|
84
|
+
}
|
|
79
85
|
async addProcessor(processor, queueName, concurrency) {
|
|
80
86
|
const queue = typeof queueName === 'string' ? this.queueMap.get(queueName) : queueName;
|
|
81
87
|
queue.process(concurrency !== null && concurrency !== void 0 ? concurrency : this.bullDefaultConcurrency, async (job) => {
|
package/index.d.ts
CHANGED