@zenweb/schedule 3.1.0 → 3.2.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/dist/index.js CHANGED
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "schedule", { enumerable: true, get: function ()
8
8
  function setup(option) {
9
9
  option = Object.assign({
10
10
  paths: [path.join(process.cwd(), 'app', 'schedule')],
11
+ disabled: process.env.ZENWEB_SCHEDULE_DISABLED === '1',
11
12
  }, option);
12
13
  return async function schedule(setup) {
13
14
  setup.checkCoreProperty('injector', '@zenweb/inject');
@@ -21,7 +22,7 @@ function setup(option) {
21
22
  const mod = require(file.slice(0, -3));
22
23
  for (const i of Object.values(mod)) {
23
24
  if (typeof i === 'function') {
24
- (0, register_1.registerSchedule)(setup.core, i);
25
+ (0, register_1.registerSchedule)(setup.core, i, option);
25
26
  }
26
27
  }
27
28
  }
@@ -4,6 +4,7 @@ import '@zenweb/log';
4
4
  import { Middleware } from 'koa';
5
5
  import { Core } from '@zenweb/core';
6
6
  import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
7
+ import { ScheduleOption } from './types';
7
8
  interface ScheduleMethodOption {
8
9
  rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
9
10
  middleware?: Middleware | Middleware[];
@@ -28,5 +29,5 @@ export declare function schedule(opt: ScheduleMethodOption): (target: any, prope
28
29
  /**
29
30
  * 添加定时任务到路由并启动定时器
30
31
  */
31
- export declare function registerSchedule(core: Core, target: any): void;
32
+ export declare function registerSchedule(core: Core, target: any, option: ScheduleOption): void;
32
33
  export {};
package/dist/register.js CHANGED
@@ -61,7 +61,7 @@ exports.schedule = schedule;
61
61
  /**
62
62
  * 添加定时任务到路由并启动定时器
63
63
  */
64
- function registerSchedule(core, target) {
64
+ function registerSchedule(core, target, option) {
65
65
  const jobs = getJobs(target.prototype);
66
66
  if (jobs.length > 0) {
67
67
  for (const item of jobs) {
@@ -71,6 +71,9 @@ function registerSchedule(core, target) {
71
71
  const cls = await ctx.injector.getInstance(target);
72
72
  await ctx.injector.apply(cls, item);
73
73
  });
74
+ if (option.disabled) {
75
+ continue;
76
+ }
74
77
  // 启用定时器
75
78
  (0, node_schedule_1.scheduleJob)(item.rule, function callback() {
76
79
  const request = Object.assign({
package/dist/types.d.ts CHANGED
@@ -11,4 +11,12 @@ export interface ScheduleOption {
11
11
  * 文件匹配规则,默认: ** /*.{ts,js}
12
12
  */
13
13
  patterns?: string;
14
+ /**
15
+ * 是否禁用定时器
16
+ * 如禁用只注册到路由而不自动执行
17
+ * 可以通过环境变量 ZENWEB_SCHEDULE_DISABLED=1 控制
18
+ *
19
+ * @default false
20
+ */
21
+ disabled?: boolean;
14
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/schedule",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Zenweb Schedule module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -36,7 +36,9 @@
36
36
  "@zenweb/core": "^2.4.1",
37
37
  "@zenweb/inject": "^3.5.0",
38
38
  "@zenweb/log": "^2.3.1",
39
- "@zenweb/router": "^3.0.0"
39
+ "@zenweb/router": "^3.0.0",
40
+ "ts-node": "^10.9.1",
41
+ "typescript": "^4.8.2"
40
42
  },
41
43
  "dependencies": {
42
44
  "@types/node-schedule": "^1.3.2",