@zenweb/schedule 3.0.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 +3 -1
- package/dist/register.d.ts +3 -1
- package/dist/register.js +18 -4
- package/dist/types.d.ts +8 -0
- package/package.json +5 -2
package/dist/index.js
CHANGED
|
@@ -8,10 +8,12 @@ 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');
|
|
14
15
|
setup.checkCoreProperty('router', '@zenweb/router');
|
|
16
|
+
setup.checkCoreProperty('log', '@zenweb/log');
|
|
15
17
|
setup.debug('option: %o', option);
|
|
16
18
|
setup.defineCoreProperty('schedule', { value: true });
|
|
17
19
|
if (option.paths && option.paths.length) {
|
|
@@ -20,7 +22,7 @@ function setup(option) {
|
|
|
20
22
|
const mod = require(file.slice(0, -3));
|
|
21
23
|
for (const i of Object.values(mod)) {
|
|
22
24
|
if (typeof i === 'function') {
|
|
23
|
-
(0, register_1.registerSchedule)(setup.core, i);
|
|
25
|
+
(0, register_1.registerSchedule)(setup.core, i, option);
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
}
|
package/dist/register.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import '@zenweb/inject';
|
|
2
2
|
import '@zenweb/router';
|
|
3
|
+
import '@zenweb/log';
|
|
3
4
|
import { Middleware } from 'koa';
|
|
4
5
|
import { Core } from '@zenweb/core';
|
|
5
6
|
import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
|
|
7
|
+
import { ScheduleOption } from './types';
|
|
6
8
|
interface ScheduleMethodOption {
|
|
7
9
|
rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
|
|
8
10
|
middleware?: Middleware | Middleware[];
|
|
@@ -27,5 +29,5 @@ export declare function schedule(opt: ScheduleMethodOption): (target: any, prope
|
|
|
27
29
|
/**
|
|
28
30
|
* 添加定时任务到路由并启动定时器
|
|
29
31
|
*/
|
|
30
|
-
export declare function registerSchedule(core: Core, target: any): void;
|
|
32
|
+
export declare function registerSchedule(core: Core, target: any, option: ScheduleOption): void;
|
|
31
33
|
export {};
|
package/dist/register.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.registerSchedule = exports.schedule = exports.addJob = exports.getJobs = void 0;
|
|
4
4
|
require("@zenweb/inject");
|
|
5
5
|
require("@zenweb/router");
|
|
6
|
+
require("@zenweb/log");
|
|
6
7
|
const http_1 = require("http");
|
|
7
8
|
const node_schedule_1 = require("node-schedule");
|
|
8
9
|
const crypto_1 = require("crypto");
|
|
@@ -11,11 +12,21 @@ const JOBS = Symbol('Schedule#jobs');
|
|
|
11
12
|
/**
|
|
12
13
|
* 安全检查,防止外部调用
|
|
13
14
|
*/
|
|
14
|
-
function safeCheck(ctx, next) {
|
|
15
|
+
async function safeCheck(ctx, next) {
|
|
16
|
+
const startTime = Date.now();
|
|
17
|
+
ctx.log.info('start');
|
|
15
18
|
if (!ctx.req.socket.remoteAddress || !ctx.req.socket.remoteAddress.endsWith(SAFE_IP)) {
|
|
16
19
|
ctx.throw(403);
|
|
17
20
|
}
|
|
18
|
-
|
|
21
|
+
try {
|
|
22
|
+
await next();
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
ctx.log.child({ err }).error('error');
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
ctx.log.info('end', Date.now() - startTime, 'ms');
|
|
29
|
+
}
|
|
19
30
|
}
|
|
20
31
|
/**
|
|
21
32
|
* 取得对象中的任务列表
|
|
@@ -50,7 +61,7 @@ exports.schedule = schedule;
|
|
|
50
61
|
/**
|
|
51
62
|
* 添加定时任务到路由并启动定时器
|
|
52
63
|
*/
|
|
53
|
-
function registerSchedule(core, target) {
|
|
64
|
+
function registerSchedule(core, target, option) {
|
|
54
65
|
const jobs = getJobs(target.prototype);
|
|
55
66
|
if (jobs.length > 0) {
|
|
56
67
|
for (const item of jobs) {
|
|
@@ -58,8 +69,11 @@ function registerSchedule(core, target) {
|
|
|
58
69
|
core.router.post(item.path, safeCheck, ...(item.middleware ?
|
|
59
70
|
(Array.isArray(item.middleware) ? item.middleware : [item.middleware]) : []), async (ctx) => {
|
|
60
71
|
const cls = await ctx.injector.getInstance(target);
|
|
61
|
-
ctx.injector.apply(cls, item);
|
|
72
|
+
await ctx.injector.apply(cls, item);
|
|
62
73
|
});
|
|
74
|
+
if (option.disabled) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
63
77
|
// 启用定时器
|
|
64
78
|
(0, node_schedule_1.scheduleJob)(item.rule, function callback() {
|
|
65
79
|
const request = Object.assign({
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/schedule",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Zenweb Schedule module",
|
|
5
5
|
"exports": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -35,7 +35,10 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@zenweb/core": "^2.4.1",
|
|
37
37
|
"@zenweb/inject": "^3.5.0",
|
|
38
|
-
"@zenweb/
|
|
38
|
+
"@zenweb/log": "^2.3.1",
|
|
39
|
+
"@zenweb/router": "^3.0.0",
|
|
40
|
+
"ts-node": "^10.9.1",
|
|
41
|
+
"typescript": "^4.8.2"
|
|
39
42
|
},
|
|
40
43
|
"dependencies": {
|
|
41
44
|
"@types/node-schedule": "^1.3.2",
|