@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 +32 -11
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -13
- package/dist/register.d.ts +2 -2
- package/dist/register.js +17 -20
- package/dist/types.d.ts +1 -2
- package/dist/types.js +1 -2
- package/package.json +12 -11
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
|
-
|
|
8
|
-
|
|
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
|
-
##
|
|
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
|
-
|
|
28
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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: [
|
|
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
|
|
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 (
|
|
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 =
|
|
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;
|
package/dist/register.d.ts
CHANGED
|
@@ -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?:
|
|
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
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
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 = `${
|
|
13
|
+
const path = `${randomUUID()}/${target.constructor.name}.${descriptor.handle.name}`;
|
|
17
14
|
return Object.assign({ path }, descriptor, opt);
|
|
18
15
|
});
|
|
19
16
|
}
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
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 =
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
"
|
|
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
|
-
"
|
|
13
|
-
"dev": "cd example &&
|
|
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
|
-
"@
|
|
38
|
-
"@zenweb/
|
|
39
|
-
"@zenweb/
|
|
40
|
-
"
|
|
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": "^
|
|
44
|
+
"typescript": "^5.6.3"
|
|
44
45
|
},
|
|
45
46
|
"dependencies": {
|
|
46
|
-
"@types/node-schedule": "^2.1.
|
|
47
|
-
"decorator-make": "^1.
|
|
48
|
-
"globby": "
|
|
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
|
}
|