@zenweb/schedule 2.3.5 → 3.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 CHANGED
@@ -2,19 +2,8 @@
2
2
 
3
3
  [ZenWeb](https://www.npmjs.com/package/zenweb)
4
4
 
5
- ## Quick start
6
- ```
7
- $ npm i @zenweb/schedule
8
- ```
5
+ 定时任务
9
6
 
10
- app/index.js
11
- ```js
12
- app.setup('@zenweb/schedule');
13
- ```
7
+ ## 安装
14
8
 
15
- app/schedule/echo.js
16
- ```js
17
- app.schedule.job('*/1 * * * * *', ctx => {
18
- console.log('task echo');
19
- });
20
- ```
9
+ 安装定时任务模块依赖于 @zenweb/inject @zenweb/router 模块
package/dist/index.d.ts CHANGED
@@ -1,10 +1,5 @@
1
1
  import { SetupFunction } from '@zenweb/core';
2
- import { ScheduleRegister } from './register';
2
+ import { schedule } from './register';
3
3
  import { ScheduleOption } from './types';
4
- export * from './types';
4
+ export { ScheduleOption, schedule };
5
5
  export default function setup(option?: ScheduleOption): SetupFunction;
6
- declare module '@zenweb/core' {
7
- interface Core {
8
- schedule: ScheduleRegister;
9
- }
10
- }
package/dist/index.js CHANGED
@@ -1,38 +1,31 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.schedule = void 0;
13
4
  const globby = require("globby");
14
5
  const path = require("path");
15
6
  const register_1 = require("./register");
16
- __exportStar(require("./types"), exports);
7
+ Object.defineProperty(exports, "schedule", { enumerable: true, get: function () { return register_1.schedule; } });
17
8
  function setup(option) {
9
+ option = Object.assign({
10
+ paths: [path.join(process.cwd(), 'app', 'schedule')],
11
+ }, option);
18
12
  return async function schedule(setup) {
19
- option = Object.assign({
20
- paths: [path.join(process.cwd(), 'app', 'schedule')],
21
- }, option);
13
+ setup.checkCoreProperty('injector', '@zenweb/inject');
14
+ setup.checkCoreProperty('router', '@zenweb/router');
22
15
  setup.debug('option: %o', option);
23
- const register = new register_1.ScheduleRegister(setup.core, option);
24
- setup.defineCoreProperty('schedule', { value: register });
16
+ setup.defineCoreProperty('schedule', { value: true });
25
17
  if (option.paths && option.paths.length) {
26
18
  for (const d of option.paths) {
27
- let count = 0;
28
- for (const m of await globby(d, { cwd: d, absolute: true })) {
29
- require(m);
30
- count++;
19
+ for (const file of await globby(option.patterns || '**/*.{ts,js}', { cwd: d, absolute: true })) {
20
+ const mod = require(file.slice(0, -3));
21
+ for (const i of Object.values(mod)) {
22
+ if (typeof i === 'function') {
23
+ (0, register_1.registerSchedule)(setup.core, i);
24
+ }
25
+ }
31
26
  }
32
- setup.debug('load: %s %o files', d, count);
33
27
  }
34
28
  }
35
29
  };
36
30
  }
37
31
  exports.default = setup;
38
- //# sourceMappingURL=index.js.map
@@ -1,15 +1,31 @@
1
- /// <reference types="koa__router" />
1
+ import '@zenweb/inject';
2
2
  import '@zenweb/router';
3
3
  import { Middleware } from 'koa';
4
4
  import { Core } from '@zenweb/core';
5
- import { RequestCallback, ScheduleOption } from './types';
6
- import { Router } from '@zenweb/router';
7
5
  import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
8
- export declare class ScheduleRegister {
9
- option: ScheduleOption;
10
- router: Router;
11
- callback: RequestCallback;
12
- private _index;
13
- constructor(core: Core, option?: ScheduleOption);
14
- job(rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number, ...middleware: Middleware[]): import("node-schedule").Job;
6
+ interface ScheduleMethodOption {
7
+ rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
8
+ middleware?: Middleware | Middleware[];
15
9
  }
10
+ interface JobItem extends ScheduleMethodOption {
11
+ path: string;
12
+ handle: (...args: any[]) => Promise<void> | void;
13
+ params: any[];
14
+ }
15
+ /**
16
+ * 取得对象中的任务列表
17
+ */
18
+ export declare function getJobs(target: any): JobItem[];
19
+ /**
20
+ * 在对象中添加任务配置
21
+ */
22
+ export declare function addJob(target: any, item: JobItem): void;
23
+ /**
24
+ * 定时任务设定
25
+ */
26
+ export declare function schedule(opt: ScheduleMethodOption): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
27
+ /**
28
+ * 添加定时任务到路由并启动定时器
29
+ */
30
+ export declare function registerSchedule(core: Core, target: any): void;
31
+ export {};
package/dist/register.js CHANGED
@@ -1,10 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ScheduleRegister = void 0;
3
+ exports.registerSchedule = exports.schedule = exports.addJob = exports.getJobs = void 0;
4
+ require("@zenweb/inject");
4
5
  require("@zenweb/router");
5
6
  const http_1 = require("http");
6
7
  const node_schedule_1 = require("node-schedule");
8
+ const crypto_1 = require("crypto");
7
9
  const SAFE_IP = '127.0.0.1';
10
+ const JOBS = Symbol('Schedule#jobs');
8
11
  /**
9
12
  * 安全检查,防止外部调用
10
13
  */
@@ -14,47 +17,73 @@ function safeCheck(ctx, next) {
14
17
  }
15
18
  return next();
16
19
  }
17
- class ScheduleRegister {
18
- constructor(core, option) {
19
- this.option = option || {};
20
- this.router = core.router;
21
- this.callback = core.koa.callback();
22
- this._index = 0;
23
- }
24
- job(rule, ...middleware) {
25
- // 注册到路由
26
- const path = `/___schedule/job/${this._index++}`;
27
- this.router.post(path, safeCheck, ...middleware);
28
- // 注册到 scheduleJob
29
- const callback = () => {
30
- const request = Object.assign({
31
- headers: {
20
+ /**
21
+ * 取得对象中的任务列表
22
+ */
23
+ function getJobs(target) {
24
+ return Reflect.getMetadata(JOBS, target) || [];
25
+ }
26
+ exports.getJobs = getJobs;
27
+ /**
28
+ * 在对象中添加任务配置
29
+ */
30
+ function addJob(target, item) {
31
+ const list = [...getJobs(target), item];
32
+ Reflect.defineMetadata(JOBS, list, target);
33
+ }
34
+ exports.addJob = addJob;
35
+ /**
36
+ * 定时任务设定
37
+ */
38
+ function schedule(opt) {
39
+ return function (target, propertyKey, descriptor) {
40
+ const path = `/__schedule/${(0, crypto_1.randomUUID)()}/${target.constructor.name}.${propertyKey}`;
41
+ const params = Reflect.getOwnMetadata('design:paramtypes', target, propertyKey) || [];
42
+ addJob(target, Object.assign({
43
+ path,
44
+ handle: descriptor.value,
45
+ params,
46
+ }, opt));
47
+ };
48
+ }
49
+ exports.schedule = schedule;
50
+ /**
51
+ * 添加定时任务到路由并启动定时器
52
+ */
53
+ function registerSchedule(core, target) {
54
+ const jobs = getJobs(target.prototype);
55
+ if (jobs.length > 0) {
56
+ for (const item of jobs) {
57
+ // 添加到路由中
58
+ core.router.post(item.path, safeCheck, ...(item.middleware ?
59
+ (Array.isArray(item.middleware) ? item.middleware : [item.middleware]) : []), async (ctx) => {
60
+ const cls = await ctx.injector.getInstance(target);
61
+ await ctx.injector.apply(cls, item);
62
+ });
63
+ // 启用定时器
64
+ (0, node_schedule_1.scheduleJob)(item.rule, function callback() {
65
+ const request = Object.assign({
66
+ headers: {
67
+ host: '127.0.0.1',
68
+ },
69
+ query: {},
70
+ querystring: '',
32
71
  host: '127.0.0.1',
33
- },
34
- query: {},
35
- querystring: '',
36
- host: '127.0.0.1',
37
- hostname: '127.0.0.1',
38
- protocol: 'http',
39
- secure: 'false',
40
- method: 'POST',
41
- url: path,
42
- path: path,
43
- socket: {
44
- remoteAddress: SAFE_IP,
45
- remotePort: 7001,
46
- },
72
+ hostname: '127.0.0.1',
73
+ protocol: 'http',
74
+ secure: 'false',
75
+ method: 'POST',
76
+ url: item.path,
77
+ path: item.path,
78
+ socket: {
79
+ remoteAddress: SAFE_IP,
80
+ remotePort: 7001,
81
+ },
82
+ });
83
+ const response = new http_1.ServerResponse(request);
84
+ core.koa.callback()(request, response);
47
85
  });
48
- const response = new http_1.ServerResponse(request);
49
- if (this.option.jobCallback) {
50
- this.option.jobCallback(request, response, this.callback);
51
- }
52
- else {
53
- this.callback(request, response);
54
- }
55
- };
56
- return (0, node_schedule_1.scheduleJob)(rule, callback);
86
+ }
57
87
  }
58
88
  }
59
- exports.ScheduleRegister = ScheduleRegister;
60
- //# sourceMappingURL=register.js.map
89
+ exports.registerSchedule = registerSchedule;
package/dist/types.d.ts CHANGED
@@ -3,6 +3,12 @@ import { ServerResponse, IncomingMessage } from 'http';
3
3
  export declare type RequestCallback = (request: IncomingMessage, response: ServerResponse) => void;
4
4
  export declare type JobCallback = (request: IncomingMessage, response: ServerResponse, callback: RequestCallback) => void;
5
5
  export interface ScheduleOption {
6
+ /**
7
+ * 加载 schedule 文件目录,默认: ./app/schedule
8
+ */
6
9
  paths?: string[];
7
- jobCallback?: JobCallback;
10
+ /**
11
+ * 文件匹配规则,默认: ** /*.{ts,js}
12
+ */
13
+ patterns?: string;
8
14
  }
package/dist/types.js CHANGED
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/schedule",
3
- "version": "2.3.5",
3
+ "version": "3.0.1",
4
4
  "description": "Zenweb Schedule module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -8,7 +8,8 @@
8
8
  "dist"
9
9
  ],
10
10
  "scripts": {
11
- "prepublishOnly": "rm -fr dist && tsc"
11
+ "prepublishOnly": "rm -fr dist && tsc",
12
+ "dev": "cd example && DEBUG=* ts-node app"
12
13
  },
13
14
  "author": {
14
15
  "name": "YeFei",
@@ -32,9 +33,9 @@
32
33
  "url": "https://github.com/yefei/zenweb-schedule/issues"
33
34
  },
34
35
  "devDependencies": {
35
- "@zenweb/core": "^2.3.2",
36
- "@zenweb/router": "^2.3.0",
37
- "zenweb": "^2.3.1"
36
+ "@zenweb/core": "^2.4.1",
37
+ "@zenweb/inject": "^3.5.0",
38
+ "@zenweb/router": "^3.0.0"
38
39
  },
39
40
  "dependencies": {
40
41
  "@types/node-schedule": "^1.3.2",
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iCAAiC;AAEjC,6BAA8B;AAC9B,yCAA8C;AAE9C,0CAAwB;AAExB,SAAwB,KAAK,CAAC,MAAuB;IACnD,OAAO,KAAK,UAAU,QAAQ,CAAC,KAAK;QAClC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YACrB,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;SACrD,EAAE,MAAM,CAAC,CAAC;QACX,KAAK,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;QAClC,MAAM,QAAQ,GAAG,IAAI,2BAAgB,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC1D,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC1D,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE;YACvC,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,KAAK,EAAE;gBAC5B,IAAI,KAAK,GAAG,CAAC,CAAC;gBACd,KAAK,MAAM,CAAC,IAAI,MAAM,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,EAAE;oBAC3D,OAAO,C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