@zenweb/schedule 3.6.1 → 4.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 CHANGED
@@ -2,10 +2,27 @@
2
2
 
3
3
  [ZenWeb](https://www.npmjs.com/package/zenweb)
4
4
 
5
- 定时任务
5
+ 基于请求的定时任务,如同定时请求固定接口,可以使用中间件和请求上下文。
6
6
 
7
- ```ts
8
- import { Context } from 'zenweb';
7
+ ## 依赖模块
8
+
9
+ - @zenweb/inject
10
+ - @zenweb/router
11
+
12
+ ## 快速使用
13
+
14
+ ```bash npm2yarn
15
+ npm install @zenweb/schedule
16
+ ```
17
+
18
+ ```ts title="src/index.ts"
19
+ import modSchedule from '@zenweb/schedule';
20
+ // ...
21
+ app.setup(modSchedule());
22
+ // ...
23
+ ```
24
+
25
+ ```ts title="src/schedule/echo.ts"
9
26
  import { schedule } from '@zenweb/schedule';
10
27
 
11
28
  export class EchoScheduler {
@@ -14,15 +31,19 @@ export class EchoScheduler {
14
31
  console.log('task echo');
15
32
  return 'ok';
16
33
  }
17
-
18
- @schedule({ rule: '*/4 * * * * *' })
19
- err(ctx: Context) {
20
- ctx.errrrr();
21
- }
22
34
  }
23
35
  ```
24
36
 
25
- ## Changelog
37
+ ## 配置项
38
+
39
+ | 配置项 | 类型 | 默认值 | 功能 |
40
+ | ----- | --- | ----- | ---- |
41
+ | paths | `string[]` | `['./app/schedule']` | 定时任务控制器加载目录
42
+ | patterns | `string` | **/*.{ts,js} | 定时任务控制器文件匹配规则
43
+ | disabled | `boolean` | `false` | 是否禁用定时器,可以通过环境变量 ZENWEB_SCHEDULE_DISABLED=1 控制
44
+
45
+ ## Core 挂载项
26
46
 
27
- ### 3.6.1
28
- - null 检查
47
+ | 挂载项 | 类型 | 功能 |
48
+ | ----- | --- | ---- |
49
+ | scheduleRegister | ScheduleRegister | ScheduleRegister 实例
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { SetupFunction } from '@zenweb/core';
2
- import { schedule, ScheduleRegister } from './register';
3
- import { ScheduleOption } from './types';
2
+ import { schedule, ScheduleRegister } from './register.js';
3
+ import { ScheduleOption } from './types.js';
4
4
  export { ScheduleOption, schedule };
5
5
  export default function setup(opt?: ScheduleOption): SetupFunction;
6
6
  declare module '@zenweb/core' {
package/dist/index.js CHANGED
@@ -1,25 +1,25 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.schedule = void 0;
4
- const globby = require("globby");
5
- const path = require("path");
6
- const register_1 = require("./register");
7
- Object.defineProperty(exports, "schedule", { enumerable: true, get: function () { return register_1.schedule; } });
8
- function setup(opt) {
1
+ import path from 'node:path';
2
+ import { globby } from 'globby';
3
+ import { schedule, ScheduleRegister } from './register.js';
4
+ export { schedule };
5
+ export default function setup(opt) {
9
6
  const option = Object.assign({
10
- paths: [path.join(process.cwd(), 'app', 'schedule')],
7
+ paths: ['./app/schedule'],
11
8
  disabled: process.env.ZENWEB_SCHEDULE_DISABLED === '1',
12
9
  }, opt);
13
10
  return async function schedule(setup) {
14
11
  setup.assertModuleExists('inject', '@zenweb/inject');
15
12
  setup.assertModuleExists('router', '@zenweb/router');
16
13
  setup.debug('option: %o', option);
17
- const scheduleRegister = new register_1.ScheduleRegister(setup.core, option);
14
+ const scheduleRegister = new ScheduleRegister(setup.core, option);
18
15
  setup.defineCoreProperty('scheduleRegister', { value: scheduleRegister });
19
16
  if (option.paths && option.paths.length) {
20
- for (const d of option.paths) {
17
+ for (let d of option.paths) {
18
+ if (d.startsWith('./')) {
19
+ d = path.join(process.cwd(), d.slice(2));
20
+ }
21
21
  for (const file of await globby(option.patterns || '**/*.{ts,js}', { cwd: d, absolute: true })) {
22
- const mod = require(file.slice(0, -3));
22
+ const mod = await import(file);
23
23
  for (const i of Object.values(mod)) {
24
24
  if (typeof i === 'function') {
25
25
  scheduleRegister.register(i);
@@ -36,4 +36,3 @@ function setup(opt) {
36
36
  });
37
37
  };
38
38
  }
39
- exports.default = setup;
@@ -1,6 +1,6 @@
1
1
  import { Core, Middleware } from '@zenweb/core';
2
2
  import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
3
- import { ScheduleOption } from './types';
3
+ import { ScheduleOption } from './types.js';
4
4
  interface ScheduleMethodOption {
5
5
  rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
6
6
  middleware?: Middleware | Middleware[];
@@ -8,7 +8,7 @@ interface ScheduleMethodOption {
8
8
  /**
9
9
  * 定时任务设定
10
10
  */
11
- export declare function schedule(opt: ScheduleMethodOption): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: any[] | PropertyDescriptor | undefined) => void;
11
+ export declare function schedule(opt: ScheduleMethodOption): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void;
12
12
  export declare class ScheduleRegister {
13
13
  private core;
14
14
  private router;
package/dist/register.js CHANGED
@@ -1,28 +1,27 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ScheduleRegister = exports.schedule = void 0;
4
- const http_1 = require("http");
5
- const node_schedule_1 = require("node-schedule");
6
- const crypto_1 = require("crypto");
7
- const decorator_make_1 = require("decorator-make");
8
- const router_1 = require("@zenweb/router");
9
- const inject_1 = require("@zenweb/inject");
10
- const scheduleDecorator = (0, decorator_make_1.makeMethodDecorator)();
1
+ /// <reference types="@zenweb/inject" />
2
+ import { ServerResponse } from 'node:http';
3
+ import { scheduleJob } from 'node-schedule';
4
+ import { randomUUID } from 'node:crypto';
5
+ import { makeMethodDecorator } from 'decorator-make';
6
+ import { Router } from '@zenweb/router';
7
+ const scheduleDecorator = makeMethodDecorator();
11
8
  /**
12
9
  * 定时任务设定
13
10
  */
14
- function schedule(opt) {
11
+ export function schedule(opt) {
15
12
  return scheduleDecorator.wrap((descriptor, target) => {
16
- const path = `${(0, crypto_1.randomUUID)()}/${target.constructor.name}.${descriptor.handle.name}`;
13
+ const path = `${randomUUID()}/${target.constructor.name}.${descriptor.handle.name}`;
17
14
  return Object.assign({ path }, descriptor, opt);
18
15
  });
19
16
  }
20
- exports.schedule = schedule;
21
- class ScheduleRegister {
17
+ export class ScheduleRegister {
18
+ core;
19
+ router;
20
+ option;
21
+ jobs = [];
22
22
  constructor(core, option) {
23
- this.jobs = [];
24
23
  this.core = core;
25
- this.router = new router_1.Router({
24
+ this.router = new Router({
26
25
  prefix: '/__schedule/',
27
26
  });
28
27
  this.option = option;
@@ -33,7 +32,6 @@ class ScheduleRegister {
33
32
  register(target) {
34
33
  const methods = scheduleDecorator.getMethods(target.prototype);
35
34
  if (methods.length > 0) {
36
- (0, inject_1.scope)('prototype', false)(target);
37
35
  for (const item of methods) {
38
36
  // 添加到路由中
39
37
  this.router.post(item.path, ...(item.middleware ?
@@ -45,7 +43,7 @@ class ScheduleRegister {
45
43
  continue;
46
44
  }
47
45
  // 启用定时器
48
- const job = (0, node_schedule_1.scheduleJob)(item.rule, () => {
46
+ const job = scheduleJob(item.rule, () => {
49
47
  const path = '/__schedule/' + item.path;
50
48
  const request = Object.assign({
51
49
  headers: {
@@ -65,7 +63,7 @@ class ScheduleRegister {
65
63
  remotePort: 7001,
66
64
  },
67
65
  });
68
- const response = new http_1.ServerResponse(request);
66
+ const response = new ServerResponse(request);
69
67
  this.core.app.callback()(request, response);
70
68
  });
71
69
  this.jobs.push(job);
@@ -81,4 +79,3 @@ class ScheduleRegister {
81
79
  }
82
80
  }
83
81
  }
84
- exports.ScheduleRegister = ScheduleRegister;
package/dist/types.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- /// <reference types="node" />
2
- import { ServerResponse, IncomingMessage } from 'http';
1
+ import { ServerResponse, IncomingMessage } from 'node:http';
3
2
  export type RequestCallback = (request: IncomingMessage, response: ServerResponse) => void;
4
3
  export type JobCallback = (request: IncomingMessage, response: ServerResponse, callback: RequestCallback) => void;
5
4
  export interface ScheduleOption {
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@zenweb/schedule",
3
- "version": "3.6.1",
3
+ "type": "module",
4
+ "version": "4.0.0",
4
5
  "description": "Zenweb Schedule module",
5
6
  "exports": "./dist/index.js",
6
7
  "types": "./dist/index.d.ts",
@@ -9,8 +10,8 @@
9
10
  ],
10
11
  "scripts": {
11
12
  "build": "rimraf dist && tsc",
12
- "prepublishOnly": "npm run build",
13
- "dev": "cd example && cross-env DEBUG=* ts-node app"
13
+ "prepack": "npm run build",
14
+ "dev": "cd example && node --env-file=.env --loader=ts-node/esm app.ts"
14
15
  },
15
16
  "author": {
16
17
  "name": "YeFei",
@@ -34,18 +35,18 @@
34
35
  "url": "https://github.com/yefei/zenweb-schedule/issues"
35
36
  },
36
37
  "devDependencies": {
37
- "@zenweb/core": "^3.5.1",
38
- "@zenweb/inject": "^3.18.0",
39
- "@zenweb/router": "^3.3.0",
40
- "cross-env": "^7.0.3",
38
+ "@types/node": "^22.7.5",
39
+ "@zenweb/core": "^5.1.0",
40
+ "@zenweb/inject": "^5.1.0",
41
+ "@zenweb/router": "^5.0.0",
41
42
  "rimraf": "^4.3.1",
42
43
  "ts-node": "^10.9.1",
43
- "typescript": "^4.9.5"
44
+ "typescript": "^5.6.3"
44
45
  },
45
46
  "dependencies": {
46
- "@types/node-schedule": "^2.1.0",
47
- "decorator-make": "^1.3.0",
48
- "globby": "11.0.4",
47
+ "@types/node-schedule": "^2.1.7",
48
+ "decorator-make": "^1.5.0",
49
+ "globby": "^14.0.2",
49
50
  "node-schedule": "^2.1.1"
50
51
  }
51
52
  }