@midwayjs/bull 3.0.4 → 3.6.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 - Now midwayjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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/bull 是为了能解决任务系列的模块,例如分布式定时任务、延迟任务调度。例如订单2小时后失效、每日定时的数据处理等工作。
5
+ Document: [https://midwayjs.org](https://midwayjs.org)
6
6
 
7
- ## 安装方法
7
+ ## License
8
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
- ```
9
+ [MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
@@ -3,7 +3,7 @@ export declare const bull: {
3
3
  prefix: string;
4
4
  };
5
5
  defaultJobOptions: {
6
- removeOnSuccess: boolean;
6
+ removeOnSuccess: number;
7
7
  removeOnFail: number;
8
8
  };
9
9
  defaultConcurrency: number;
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.midwayLogger = exports.bull = void 0;
4
4
  exports.bull = {
5
5
  defaultQueueOptions: {
6
- prefix: '${midway-bull}',
6
+ prefix: '{midway-bull}',
7
7
  },
8
8
  defaultJobOptions: {
9
- removeOnSuccess: true,
9
+ removeOnSuccess: 3,
10
10
  removeOnFail: 10,
11
11
  },
12
12
  defaultConcurrency: 1,
@@ -1,5 +1,5 @@
1
- import { BullFramework } from './framework';
2
1
  import { MidwayDecoratorService } from '@midwayjs/core';
2
+ import { BullFramework } from './framework';
3
3
  export declare class BullConfiguration {
4
4
  framework: BullFramework;
5
5
  decoratorService: MidwayDecoratorService;
@@ -10,10 +10,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.BullConfiguration = void 0;
13
- const decorator_1 = require("@midwayjs/decorator");
13
+ const core_1 = require("@midwayjs/core");
14
14
  const DefaultConfig = require("./config/config.default");
15
15
  const framework_1 = require("./framework");
16
- const core_1 = require("@midwayjs/core");
17
16
  const constants_1 = require("./constants");
18
17
  let BullConfiguration = class BullConfiguration {
19
18
  async init() {
@@ -23,22 +22,22 @@ let BullConfiguration = class BullConfiguration {
23
22
  }
24
23
  };
25
24
  __decorate([
26
- (0, decorator_1.Inject)(),
25
+ (0, core_1.Inject)(),
27
26
  __metadata("design:type", framework_1.BullFramework)
28
27
  ], BullConfiguration.prototype, "framework", void 0);
29
28
  __decorate([
30
- (0, decorator_1.Inject)(),
29
+ (0, core_1.Inject)(),
31
30
  __metadata("design:type", core_1.MidwayDecoratorService)
32
31
  ], BullConfiguration.prototype, "decoratorService", void 0);
33
32
  __decorate([
34
- (0, decorator_1.Init)(),
33
+ (0, core_1.Init)(),
35
34
  __metadata("design:type", Function),
36
35
  __metadata("design:paramtypes", []),
37
36
  __metadata("design:returntype", Promise)
38
37
  ], BullConfiguration.prototype, "init", null);
39
38
  BullConfiguration = __decorate([
40
- (0, decorator_1.Configuration)({
41
- namespace: 'task',
39
+ (0, core_1.Configuration)({
40
+ namespace: 'bull',
42
41
  importConfigs: [
43
42
  {
44
43
  default: DefaultConfig,
package/dist/decorator.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InjectQueue = exports.Processor = void 0;
4
- const decorator_1 = require("@midwayjs/decorator");
4
+ const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("./constants");
6
6
  function Processor(queueName, concurrency, jobOptions) {
7
7
  return function (target) {
@@ -9,61 +9,21 @@ function Processor(queueName, concurrency, jobOptions) {
9
9
  jobOptions = concurrency;
10
10
  concurrency = 1;
11
11
  }
12
- (0, decorator_1.saveModule)(constants_1.BULL_PROCESSOR_KEY, target);
13
- (0, decorator_1.saveClassMetadata)(constants_1.BULL_PROCESSOR_KEY, {
12
+ (0, core_1.saveModule)(constants_1.BULL_PROCESSOR_KEY, target);
13
+ (0, core_1.saveClassMetadata)(constants_1.BULL_PROCESSOR_KEY, {
14
14
  queueName,
15
15
  concurrency,
16
16
  jobOptions,
17
17
  }, target);
18
- (0, decorator_1.Provide)()(target);
19
- (0, decorator_1.Scope)(decorator_1.ScopeEnum.Request)(target);
18
+ (0, core_1.Provide)()(target);
19
+ (0, core_1.Scope)(core_1.ScopeEnum.Request)(target);
20
20
  };
21
21
  }
22
22
  exports.Processor = Processor;
23
23
  function InjectQueue(queueName) {
24
- return (0, decorator_1.createCustomPropertyDecorator)(constants_1.BULL_QUEUE_KEY, {
24
+ return (0, core_1.createCustomPropertyDecorator)(constants_1.BULL_QUEUE_KEY, {
25
25
  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
@@ -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
@@ -8,7 +8,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.BullFramework = exports.BullQueue = void 0;
10
10
  const core_1 = require("@midwayjs/core");
11
- const decorator_1 = require("@midwayjs/decorator");
12
11
  const Bull = require("bull");
13
12
  const constants_1 = require("./constants");
14
13
  class BullQueue extends Bull {
@@ -42,9 +41,9 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
42
41
  }
43
42
  async run() {
44
43
  var _a;
45
- const processorModules = (0, decorator_1.listModule)(constants_1.BULL_PROCESSOR_KEY);
44
+ const processorModules = (0, core_1.listModule)(constants_1.BULL_PROCESSOR_KEY);
46
45
  for (const mod of processorModules) {
47
- const options = (0, decorator_1.getClassMetadata)(constants_1.BULL_PROCESSOR_KEY, mod);
46
+ const options = (0, core_1.getClassMetadata)(constants_1.BULL_PROCESSOR_KEY, mod);
48
47
  const currentQueue = this.ensureQueue(options.queueName);
49
48
  // clear old repeat job when start
50
49
  if (this.bullClearRepeatJobWhenStart) {
@@ -79,6 +78,9 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
79
78
  }
80
79
  return this.queueMap.get(name);
81
80
  }
81
+ getQueueList() {
82
+ return Array.from(this.queueMap.values());
83
+ }
82
84
  async addProcessor(processor, queueName, concurrency) {
83
85
  const queue = typeof queueName === 'string' ? this.queueMap.get(queueName) : queueName;
84
86
  queue.process(concurrency !== null && concurrency !== void 0 ? concurrency : this.bullDefaultConcurrency, async (job) => {
@@ -88,9 +90,15 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
88
90
  from: processor,
89
91
  });
90
92
  ctx.logger.info(`start process job ${job.id} from ${processor.name}`);
93
+ const isPassed = await this.app
94
+ .getFramework()
95
+ .runGuard(ctx, processor, 'execute');
96
+ if (!isPassed) {
97
+ throw new core_1.MidwayInvokeForbiddenError('execute', processor);
98
+ }
91
99
  const service = await ctx.requestContext.getAsync(processor);
92
100
  const fn = await this.applyMiddleware(async (ctx) => {
93
- return await decorator_1.Utils.toAsyncFunction(service.execute.bind(service))(job.data, job);
101
+ return await core_1.Utils.toAsyncFunction(service.execute.bind(service))(job.data, job);
94
102
  });
95
103
  try {
96
104
  const result = await Promise.resolve(await fn(ctx));
@@ -117,7 +125,7 @@ let BullFramework = class BullFramework extends core_1.BaseFramework {
117
125
  }
118
126
  };
119
127
  BullFramework = __decorate([
120
- (0, decorator_1.Framework)()
128
+ (0, core_1.Framework)()
121
129
  ], BullFramework);
122
130
  exports.BullFramework = BullFramework;
123
131
  //# sourceMappingURL=framework.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/bull",
3
- "version": "3.0.4",
3
+ "version": "3.6.0",
4
4
  "description": "midway component for bull",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -24,15 +24,15 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "devDependencies": {
27
- "@midwayjs/core": "^3.4.13",
28
- "@midwayjs/decorator": "^3.4.11",
29
- "@midwayjs/mock": "^3.4.13"
27
+ "@midwayjs/core": "^3.6.0",
28
+ "@midwayjs/mock": "^3.6.0"
30
29
  },
31
30
  "dependencies": {
32
- "bull": "4.8.5",
33
- "@types/bull": "3.15.9"
31
+ "@types/bull": "3.15.9",
32
+ "bull": "4.10.0"
34
33
  },
35
34
  "engines": {
36
35
  "node": ">=12"
37
- }
36
+ },
37
+ "gitHead": "22643b0e8519766bb7c68b975930199fc136336e"
38
38
  }