@zenweb/schedule 4.0.1 → 5.1.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 +10 -6
- package/dist/register.d.ts +1 -1
- package/dist/register.js +10 -9
- package/dist/types.d.ts +6 -0
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export default function setup(opt) {
|
|
|
7
7
|
const option = Object.assign({
|
|
8
8
|
paths: ['./app/schedule'],
|
|
9
9
|
disabled: process.env.ZENWEB_SCHEDULE_DISABLED === '1',
|
|
10
|
+
prefix: '/__schedule/',
|
|
10
11
|
}, opt);
|
|
11
12
|
return async function schedule(setup) {
|
|
12
13
|
setup.assertModuleExists('inject', '@zenweb/inject');
|
|
@@ -15,17 +16,20 @@ export default function setup(opt) {
|
|
|
15
16
|
const scheduleRegister = new ScheduleRegister(setup.core, option);
|
|
16
17
|
setup.defineCoreProperty('scheduleRegister', { value: scheduleRegister });
|
|
17
18
|
if (option.paths && option.paths.length) {
|
|
18
|
-
for (let
|
|
19
|
-
if (
|
|
20
|
-
|
|
19
|
+
for (let dir of option.paths) {
|
|
20
|
+
if (dir.startsWith('./')) {
|
|
21
|
+
dir = path.join(process.cwd(), dir.slice(2));
|
|
21
22
|
}
|
|
22
|
-
for (const file of await globby(option.patterns || '**/*.{ts,js}', { cwd:
|
|
23
|
+
for (const file of await globby(option.patterns || '**/*.{ts,js}', { cwd: dir })) {
|
|
23
24
|
setup.debug('load:', file);
|
|
24
|
-
const
|
|
25
|
+
const lastDot = Math.max(0, file.lastIndexOf('.'));
|
|
26
|
+
const filename = lastDot ? file.slice(0, lastDot) : file;
|
|
27
|
+
const prefix = filename.split('/').join('/');
|
|
28
|
+
const importUrl = pathToFileURL(path.join(dir, file)).href;
|
|
25
29
|
const mod = await import(importUrl);
|
|
26
30
|
for (const i of Object.values(mod)) {
|
|
27
31
|
if (typeof i === 'function') {
|
|
28
|
-
scheduleRegister.register(i);
|
|
32
|
+
scheduleRegister.register(i, prefix);
|
|
29
33
|
}
|
|
30
34
|
}
|
|
31
35
|
}
|
package/dist/register.d.ts
CHANGED
package/dist/register.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@zenweb/inject" />
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
import { ServerResponse } from 'node:http';
|
|
3
4
|
import { scheduleJob } from 'node-schedule';
|
|
4
|
-
import { randomUUID } from 'node:crypto';
|
|
5
5
|
import { makeMethodDecorator } from 'decorator-make';
|
|
6
6
|
import { Router } from '@zenweb/router';
|
|
7
7
|
const scheduleDecorator = makeMethodDecorator();
|
|
@@ -10,7 +10,7 @@ const scheduleDecorator = makeMethodDecorator();
|
|
|
10
10
|
*/
|
|
11
11
|
export function schedule(opt) {
|
|
12
12
|
return scheduleDecorator.wrap((descriptor, target) => {
|
|
13
|
-
const path =
|
|
13
|
+
const path = `/${target.constructor.name}.${descriptor.handle.name}`;
|
|
14
14
|
return Object.assign({ path }, descriptor, opt);
|
|
15
15
|
});
|
|
16
16
|
}
|
|
@@ -22,19 +22,20 @@ export class ScheduleRegister {
|
|
|
22
22
|
constructor(core, option) {
|
|
23
23
|
this.core = core;
|
|
24
24
|
this.router = new Router({
|
|
25
|
-
prefix:
|
|
25
|
+
prefix: option.prefix,
|
|
26
26
|
});
|
|
27
27
|
this.option = option;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* 添加定时任务到路由并启动定时器
|
|
31
31
|
*/
|
|
32
|
-
register(target) {
|
|
32
|
+
register(target, prefix) {
|
|
33
33
|
const methods = scheduleDecorator.getMethods(target.prototype);
|
|
34
34
|
if (methods.length > 0) {
|
|
35
35
|
for (const item of methods) {
|
|
36
|
+
const apath = prefix ? path.join('/', prefix, item.path) : item.path;
|
|
36
37
|
// 添加到路由中
|
|
37
|
-
this.router.post(
|
|
38
|
+
this.router.post(apath, ...(item.middleware ?
|
|
38
39
|
(Array.isArray(item.middleware) ? item.middleware : [item.middleware]) : []), async (ctx) => {
|
|
39
40
|
const cls = await ctx.injector.getInstance(target);
|
|
40
41
|
await ctx.injector.apply(cls, item);
|
|
@@ -44,7 +45,7 @@ export class ScheduleRegister {
|
|
|
44
45
|
}
|
|
45
46
|
// 启用定时器
|
|
46
47
|
const job = scheduleJob(item.rule, () => {
|
|
47
|
-
const
|
|
48
|
+
const url = this.option.prefix ? path.join(this.option.prefix, apath) : apath;
|
|
48
49
|
const request = Object.assign({
|
|
49
50
|
headers: {
|
|
50
51
|
host: '127.0.0.1',
|
|
@@ -56,8 +57,8 @@ export class ScheduleRegister {
|
|
|
56
57
|
protocol: 'http',
|
|
57
58
|
secure: 'false',
|
|
58
59
|
method: 'POST',
|
|
59
|
-
url
|
|
60
|
-
path,
|
|
60
|
+
url,
|
|
61
|
+
path: url,
|
|
61
62
|
socket: {
|
|
62
63
|
remoteAddress: '127.0.0.1',
|
|
63
64
|
remotePort: 7001,
|
|
@@ -71,7 +72,7 @@ export class ScheduleRegister {
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
addToCoreRouter() {
|
|
74
|
-
this.core.router.
|
|
75
|
+
this.core.router.add(this.router);
|
|
75
76
|
}
|
|
76
77
|
destory() {
|
|
77
78
|
for (const job of this.jobs) {
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/schedule",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"description": "Zenweb Schedule module",
|
|
6
6
|
"exports": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -35,18 +35,18 @@
|
|
|
35
35
|
"url": "https://github.com/yefei/zenweb-schedule/issues"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^22.
|
|
39
|
-
"@zenweb/core": "^5.1
|
|
38
|
+
"@types/node": "^22.19.15",
|
|
39
|
+
"@zenweb/core": "^5.2.1",
|
|
40
40
|
"@zenweb/inject": "^5.1.0",
|
|
41
|
-
"@zenweb/router": "^
|
|
42
|
-
"rimraf": "^4.
|
|
43
|
-
"ts-node": "^10.9.
|
|
44
|
-
"typescript": "^5.
|
|
41
|
+
"@zenweb/router": "^6.0.3",
|
|
42
|
+
"rimraf": "^4.4.1",
|
|
43
|
+
"ts-node": "^10.9.2",
|
|
44
|
+
"typescript": "^5.9.3"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@types/node-schedule": "^2.1.
|
|
47
|
+
"@types/node-schedule": "^2.1.8",
|
|
48
48
|
"decorator-make": "^1.5.0",
|
|
49
|
-
"globby": "^14.0
|
|
49
|
+
"globby": "^14.1.0",
|
|
50
50
|
"node-schedule": "^2.1.1"
|
|
51
51
|
}
|
|
52
52
|
}
|