@eggjs/schedule 6.0.0-beta.19 → 6.0.0-beta.20
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/agent-BiclCXUu.js +32 -0
- package/dist/agent-DUNNeo72.d.ts +100 -0
- package/dist/agent.d.ts +3 -1
- package/dist/agent.js +4 -2
- package/dist/all-DY7LEzzF.js +11 -0
- package/dist/app/extend/agent.d.ts +3 -21
- package/dist/app/extend/agent.js +5 -30
- package/dist/app/extend/application.d.ts +3 -11
- package/dist/app/extend/application.js +3 -16
- package/dist/app/extend/application.unittest.d.ts +4 -7
- package/dist/app/extend/application.unittest.js +4 -44
- package/dist/app.d.ts +3 -1
- package/dist/application-Be3vYRp0.d.ts +22 -0
- package/dist/application-CtPD5smr.js +18 -0
- package/dist/application.unittest-BySM_r-a.js +46 -0
- package/dist/application.unittest-DOurfxcE.d.ts +8 -0
- package/dist/base-BQ53Qv2t.js +73 -0
- package/dist/config/config.default.d.ts +1 -19
- package/dist/config.default-Oe7rctMS.d.ts +20 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +10 -5
- package/dist/lib/load_schedule.d.ts +2 -1
- package/dist/lib/load_schedule.js +1 -59
- package/dist/lib/schedule.d.ts +3 -34
- package/dist/lib/schedule.js +2 -75
- package/dist/lib/schedule_worker.d.ts +3 -13
- package/dist/lib/schedule_worker.js +2 -20
- package/dist/lib/strategy/all.d.ts +3 -1
- package/dist/lib/strategy/all.js +3 -9
- package/dist/lib/strategy/base.d.ts +3 -35
- package/dist/lib/strategy/base.js +1 -71
- package/dist/lib/strategy/timer.d.ts +3 -23
- package/dist/lib/strategy/timer.js +2 -69
- package/dist/lib/strategy/worker.d.ts +3 -1
- package/dist/lib/strategy/worker.js +3 -9
- package/dist/lib/types.d.ts +2 -20
- package/dist/lib/types.js +2 -0
- package/dist/load_schedule-BwGbsGkz.js +61 -0
- package/dist/schedule-C0pZBoiR.js +77 -0
- package/dist/schedule_worker-DsbRF__K.js +22 -0
- package/dist/timer-B-5D2wim.js +71 -0
- package/dist/types-C43iyhAd.js +1 -0
- package/dist/types-CPNMyF89.js +1 -0
- package/dist/types-D3XkzB3g.d.ts +21 -0
- package/dist/types-VUtK3imu.d.ts +17 -0
- package/dist/types.d.ts +2 -17
- package/dist/types.js +2 -0
- package/dist/worker-Dc570kZo.js +11 -0
- package/package.json +6 -6
package/dist/lib/schedule.d.ts
CHANGED
|
@@ -1,35 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
//#region src/lib/schedule.d.ts
|
|
6
|
-
declare class Schedule {
|
|
7
|
-
#private;
|
|
8
|
-
closed: boolean;
|
|
9
|
-
constructor(agent: ScheduleAgent);
|
|
10
|
-
/**
|
|
11
|
-
* register a custom Schedule Strategy
|
|
12
|
-
* @param {String} type - strategy type
|
|
13
|
-
* @param {Strategy} clz - Strategy class
|
|
14
|
-
*/
|
|
15
|
-
use(type: string, clz: typeof BaseStrategy): void;
|
|
16
|
-
/**
|
|
17
|
-
* load all schedule jobs, then initialize and register special strategy
|
|
18
|
-
*/
|
|
19
|
-
init(): Promise<void>;
|
|
20
|
-
registerSchedule(scheduleItem: EggScheduleItem): void;
|
|
21
|
-
unregisterSchedule(key: string): boolean;
|
|
22
|
-
/**
|
|
23
|
-
* job finish event handler
|
|
24
|
-
*
|
|
25
|
-
* @param {Object} info - { id, key, success, message, workerId }
|
|
26
|
-
*/
|
|
27
|
-
onJobFinish(info: EggScheduleJobInfo): void;
|
|
28
|
-
/**
|
|
29
|
-
* start schedule
|
|
30
|
-
*/
|
|
31
|
-
start(): Promise<void>;
|
|
32
|
-
close(): Promise<void>;
|
|
33
|
-
}
|
|
34
|
-
//#endregion
|
|
1
|
+
import "../config.default-Oe7rctMS.js";
|
|
2
|
+
import "../types-D3XkzB3g.js";
|
|
3
|
+
import { Schedule } from "../agent-DUNNeo72.js";
|
|
35
4
|
export { Schedule };
|
package/dist/lib/schedule.js
CHANGED
|
@@ -1,77 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import "../load_schedule-BwGbsGkz.js";
|
|
2
|
+
import { Schedule } from "../schedule-C0pZBoiR.js";
|
|
3
3
|
|
|
4
|
-
//#region src/lib/schedule.ts
|
|
5
|
-
const debug = debuglog("egg/schedule/lib/schedule");
|
|
6
|
-
var Schedule = class {
|
|
7
|
-
closed = false;
|
|
8
|
-
#agent;
|
|
9
|
-
#logger;
|
|
10
|
-
#strategyClassMap = /* @__PURE__ */ new Map();
|
|
11
|
-
#strategyInstanceMap = /* @__PURE__ */ new Map();
|
|
12
|
-
constructor(agent) {
|
|
13
|
-
this.#agent = agent;
|
|
14
|
-
this.#logger = agent.getLogger("scheduleLogger");
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* register a custom Schedule Strategy
|
|
18
|
-
* @param {String} type - strategy type
|
|
19
|
-
* @param {Strategy} clz - Strategy class
|
|
20
|
-
*/
|
|
21
|
-
use(type, clz) {
|
|
22
|
-
this.#strategyClassMap.set(type, clz);
|
|
23
|
-
debug("use type: %o", type);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* load all schedule jobs, then initialize and register special strategy
|
|
27
|
-
*/
|
|
28
|
-
async init() {
|
|
29
|
-
const scheduleItems = await loadSchedule(this.#agent);
|
|
30
|
-
for (const scheduleItem of Object.values(scheduleItems)) this.registerSchedule(scheduleItem);
|
|
31
|
-
}
|
|
32
|
-
registerSchedule(scheduleItem) {
|
|
33
|
-
const { key, schedule } = scheduleItem;
|
|
34
|
-
const type = schedule.type;
|
|
35
|
-
if (schedule.disable) return;
|
|
36
|
-
const Strategy = this.#strategyClassMap.get(type);
|
|
37
|
-
if (!Strategy) {
|
|
38
|
-
const err = /* @__PURE__ */ new Error(`schedule type [${type}] is not defined`);
|
|
39
|
-
err.name = "EggScheduleError";
|
|
40
|
-
throw err;
|
|
41
|
-
}
|
|
42
|
-
const instance = new Strategy(schedule, this.#agent, key);
|
|
43
|
-
this.#strategyInstanceMap.set(key, instance);
|
|
44
|
-
debug("registerSchedule type: %o, config: %o, key: %o", type, schedule, key);
|
|
45
|
-
}
|
|
46
|
-
unregisterSchedule(key) {
|
|
47
|
-
debug("unregisterSchedule key: %o", key);
|
|
48
|
-
return this.#strategyInstanceMap.delete(key);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* job finish event handler
|
|
52
|
-
*
|
|
53
|
-
* @param {Object} info - { id, key, success, message, workerId }
|
|
54
|
-
*/
|
|
55
|
-
onJobFinish(info) {
|
|
56
|
-
this.#logger.debug(`[Job#${info.id}] ${info.key} finish event received by agent from worker#${info.workerId}`);
|
|
57
|
-
const instance = this.#strategyInstanceMap.get(info.key);
|
|
58
|
-
/* istanbul ignore else */
|
|
59
|
-
if (instance) instance.onJobFinish(info);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* start schedule
|
|
63
|
-
*/
|
|
64
|
-
async start() {
|
|
65
|
-
debug("start");
|
|
66
|
-
this.closed = false;
|
|
67
|
-
for (const instance of this.#strategyInstanceMap.values()) instance.start();
|
|
68
|
-
}
|
|
69
|
-
async close() {
|
|
70
|
-
this.closed = true;
|
|
71
|
-
for (const instance of this.#strategyInstanceMap.values()) await instance.close();
|
|
72
|
-
debug("close");
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
//#endregion
|
|
77
4
|
export { Schedule };
|
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
//#region src/lib/schedule_worker.d.ts
|
|
5
|
-
declare class ScheduleWorker {
|
|
6
|
-
#private;
|
|
7
|
-
scheduleItems: Record<string, EggScheduleItem>;
|
|
8
|
-
constructor(app: ScheduleApplication);
|
|
9
|
-
init(): Promise<void>;
|
|
10
|
-
registerSchedule(scheduleItem: EggScheduleItem): void;
|
|
11
|
-
unregisterSchedule(key: string): void;
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
1
|
+
import "../config.default-Oe7rctMS.js";
|
|
2
|
+
import "../types-D3XkzB3g.js";
|
|
3
|
+
import { ScheduleWorker } from "../application-Be3vYRp0.js";
|
|
14
4
|
export { ScheduleWorker };
|
|
@@ -1,22 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../load_schedule-BwGbsGkz.js";
|
|
2
|
+
import { ScheduleWorker } from "../schedule_worker-DsbRF__K.js";
|
|
2
3
|
|
|
3
|
-
//#region src/lib/schedule_worker.ts
|
|
4
|
-
var ScheduleWorker = class {
|
|
5
|
-
#app;
|
|
6
|
-
scheduleItems = {};
|
|
7
|
-
constructor(app) {
|
|
8
|
-
this.#app = app;
|
|
9
|
-
}
|
|
10
|
-
async init() {
|
|
11
|
-
this.scheduleItems = await loadSchedule(this.#app);
|
|
12
|
-
}
|
|
13
|
-
registerSchedule(scheduleItem) {
|
|
14
|
-
this.scheduleItems[scheduleItem.key] = scheduleItem;
|
|
15
|
-
}
|
|
16
|
-
unregisterSchedule(key) {
|
|
17
|
-
delete this.scheduleItems[key];
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
4
|
export { ScheduleWorker };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../../config.default-Oe7rctMS.js";
|
|
2
|
+
import "../../types-D3XkzB3g.js";
|
|
3
|
+
import { TimerStrategy } from "../../agent-DUNNeo72.js";
|
|
2
4
|
|
|
3
5
|
//#region src/lib/strategy/all.d.ts
|
|
4
6
|
declare class AllStrategy extends TimerStrategy {
|
package/dist/lib/strategy/all.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../../base-BQ53Qv2t.js";
|
|
2
|
+
import "../../timer-B-5D2wim.js";
|
|
3
|
+
import { AllStrategy } from "../../all-DY7LEzzF.js";
|
|
2
4
|
|
|
3
|
-
//#region src/lib/strategy/all.ts
|
|
4
|
-
var AllStrategy = class extends TimerStrategy {
|
|
5
|
-
handler() {
|
|
6
|
-
this.sendAll();
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
//#endregion
|
|
11
5
|
export { AllStrategy };
|
|
@@ -1,36 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { EggLogger } from "egg";
|
|
5
|
-
|
|
6
|
-
//#region src/lib/strategy/base.d.ts
|
|
7
|
-
declare class BaseStrategy {
|
|
8
|
-
protected agent: ScheduleAgent;
|
|
9
|
-
protected scheduleConfig: EggScheduleConfig;
|
|
10
|
-
protected key: string;
|
|
11
|
-
protected logger: EggLogger;
|
|
12
|
-
protected closed: boolean;
|
|
13
|
-
count: number;
|
|
14
|
-
constructor(scheduleConfig: EggScheduleConfig, agent: ScheduleAgent, key: string);
|
|
15
|
-
/** keep compatibility */
|
|
16
|
-
get schedule(): EggScheduleConfig;
|
|
17
|
-
start(): Promise<void>;
|
|
18
|
-
close(): Promise<void>;
|
|
19
|
-
onJobStart(_info: EggScheduleJobInfo): void;
|
|
20
|
-
onJobFinish(_info: EggScheduleJobInfo): void;
|
|
21
|
-
/**
|
|
22
|
-
* trigger one worker
|
|
23
|
-
*
|
|
24
|
-
* @param {...any} args - pass to job task
|
|
25
|
-
*/
|
|
26
|
-
sendOne(...args: any[]): void;
|
|
27
|
-
/**
|
|
28
|
-
* trigger all worker
|
|
29
|
-
*
|
|
30
|
-
* @param {...any} args - pass to job task
|
|
31
|
-
*/
|
|
32
|
-
sendAll(...args: any[]): void;
|
|
33
|
-
getSeqId(): string;
|
|
34
|
-
}
|
|
35
|
-
//#endregion
|
|
1
|
+
import "../../config.default-Oe7rctMS.js";
|
|
2
|
+
import "../../types-D3XkzB3g.js";
|
|
3
|
+
import { BaseStrategy } from "../../agent-DUNNeo72.js";
|
|
36
4
|
export { BaseStrategy };
|
|
@@ -1,73 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
var BaseStrategy = class {
|
|
3
|
-
agent;
|
|
4
|
-
scheduleConfig;
|
|
5
|
-
key;
|
|
6
|
-
logger;
|
|
7
|
-
closed = false;
|
|
8
|
-
count = 0;
|
|
9
|
-
constructor(scheduleConfig, agent, key) {
|
|
10
|
-
this.agent = agent;
|
|
11
|
-
this.key = key;
|
|
12
|
-
this.scheduleConfig = scheduleConfig;
|
|
13
|
-
this.logger = this.agent.getLogger("scheduleLogger");
|
|
14
|
-
}
|
|
15
|
-
/** keep compatibility */
|
|
16
|
-
get schedule() {
|
|
17
|
-
return this.scheduleConfig;
|
|
18
|
-
}
|
|
19
|
-
async start() {}
|
|
20
|
-
async close() {
|
|
21
|
-
this.closed = true;
|
|
22
|
-
}
|
|
23
|
-
onJobStart(_info) {}
|
|
24
|
-
onJobFinish(_info) {}
|
|
25
|
-
/**
|
|
26
|
-
* trigger one worker
|
|
27
|
-
*
|
|
28
|
-
* @param {...any} args - pass to job task
|
|
29
|
-
*/
|
|
30
|
-
sendOne(...args) {
|
|
31
|
-
/* istanbul ignore next */
|
|
32
|
-
if (this.agent.schedule.closed) {
|
|
33
|
-
this.logger.warn(`${this.key} skip due to schedule closed`);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
this.count++;
|
|
37
|
-
const info = {
|
|
38
|
-
key: this.key,
|
|
39
|
-
id: this.getSeqId(),
|
|
40
|
-
args
|
|
41
|
-
};
|
|
42
|
-
this.logger.info(`[Job#${info.id}] ${info.key} triggered, send random by agent`);
|
|
43
|
-
this.agent.messenger.sendRandom("egg-schedule", info);
|
|
44
|
-
this.onJobStart(info);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* trigger all worker
|
|
48
|
-
*
|
|
49
|
-
* @param {...any} args - pass to job task
|
|
50
|
-
*/
|
|
51
|
-
sendAll(...args) {
|
|
52
|
-
/* istanbul ignore next */
|
|
53
|
-
if (this.agent.schedule.closed) {
|
|
54
|
-
this.logger.warn(`${this.key} skip due to schedule closed`);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
this.count++;
|
|
58
|
-
const info = {
|
|
59
|
-
key: this.key,
|
|
60
|
-
id: this.getSeqId(),
|
|
61
|
-
args
|
|
62
|
-
};
|
|
63
|
-
this.logger.info(`[Job#${info.id}] ${info.key} triggered, send all by agent`);
|
|
64
|
-
this.agent.messenger.send("egg-schedule", info);
|
|
65
|
-
this.onJobStart(info);
|
|
66
|
-
}
|
|
67
|
-
getSeqId() {
|
|
68
|
-
return `${Date.now()}${process.hrtime().join("")}${this.count}`;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
1
|
+
import { BaseStrategy } from "../../base-BQ53Qv2t.js";
|
|
71
2
|
|
|
72
|
-
//#endregion
|
|
73
3
|
export { BaseStrategy };
|
|
@@ -1,24 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { CronExpression } from "cron-parser";
|
|
5
|
-
import safeTimers from "safe-timers";
|
|
6
|
-
|
|
7
|
-
//#region src/lib/strategy/timer.d.ts
|
|
8
|
-
declare abstract class TimerStrategy extends BaseStrategy {
|
|
9
|
-
#private;
|
|
10
|
-
protected cronInstance?: CronExpression;
|
|
11
|
-
constructor(scheduleConfig: EggScheduleConfig, agent: ScheduleAgent, key: string);
|
|
12
|
-
protected handler(): void;
|
|
13
|
-
start(): Promise<void>;
|
|
14
|
-
onJobStart(): void;
|
|
15
|
-
/**
|
|
16
|
-
* calculate next tick
|
|
17
|
-
*
|
|
18
|
-
* @return {Number|undefined} time interval, if out of range then return `undefined`
|
|
19
|
-
*/
|
|
20
|
-
protected getNextTick(): number | undefined;
|
|
21
|
-
protected safeTimeout(handler: () => void, delay: number, ...args: any[]): number | safeTimers.Timeout;
|
|
22
|
-
}
|
|
23
|
-
//#endregion
|
|
1
|
+
import "../../config.default-Oe7rctMS.js";
|
|
2
|
+
import "../../types-D3XkzB3g.js";
|
|
3
|
+
import { TimerStrategy } from "../../agent-DUNNeo72.js";
|
|
24
4
|
export { TimerStrategy };
|
|
@@ -1,71 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import cronParser from "cron-parser";
|
|
4
|
-
import { ms } from "humanize-ms";
|
|
5
|
-
import safeTimers from "safe-timers";
|
|
6
|
-
import { logDate } from "utility";
|
|
1
|
+
import "../../base-BQ53Qv2t.js";
|
|
2
|
+
import { TimerStrategy } from "../../timer-B-5D2wim.js";
|
|
7
3
|
|
|
8
|
-
//#region src/lib/strategy/timer.ts
|
|
9
|
-
var TimerStrategy = class extends BaseStrategy {
|
|
10
|
-
cronInstance;
|
|
11
|
-
constructor(scheduleConfig, agent, key) {
|
|
12
|
-
super(scheduleConfig, agent, key);
|
|
13
|
-
const { interval, cron, cronOptions, immediate } = this.scheduleConfig;
|
|
14
|
-
assert(interval || cron || immediate, `[@eggjs/schedule] ${this.key} \`schedule.interval\` or \`schedule.cron\` or \`schedule.immediate\` must be present`);
|
|
15
|
-
if (cron) try {
|
|
16
|
-
this.cronInstance = cronParser.parseExpression(cron, cronOptions);
|
|
17
|
-
} catch (err) {
|
|
18
|
-
throw new TypeError(`[@eggjs/schedule] ${this.key} parse cron instruction(${cron}) error: ${err.message}`, { cause: err });
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
handler() {
|
|
22
|
-
throw new TypeError(`[@eggjs/schedule] ${this.key} strategy should override \`handler()\` method`);
|
|
23
|
-
}
|
|
24
|
-
async start() {
|
|
25
|
-
/* istanbul ignore next */
|
|
26
|
-
if (this.agent.schedule.closed) return;
|
|
27
|
-
if (this.scheduleConfig.immediate) {
|
|
28
|
-
this.logger.info(`[Timer] ${this.key} next time will execute immediate`);
|
|
29
|
-
setImmediate(() => this.handler());
|
|
30
|
-
} else this.#scheduleNext();
|
|
31
|
-
}
|
|
32
|
-
#scheduleNext() {
|
|
33
|
-
/* istanbul ignore next */
|
|
34
|
-
if (this.agent.schedule.closed) return;
|
|
35
|
-
const nextTick = this.getNextTick();
|
|
36
|
-
if (nextTick) {
|
|
37
|
-
this.logger.info(`[Timer] ${this.key} next time will execute after ${nextTick}ms at ${logDate(new Date(Date.now() + nextTick))}`);
|
|
38
|
-
this.safeTimeout(() => this.handler(), nextTick);
|
|
39
|
-
} else this.logger.info(`[Timer] ${this.key} reach endDate, will stop`);
|
|
40
|
-
}
|
|
41
|
-
onJobStart() {
|
|
42
|
-
this.#scheduleNext();
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* calculate next tick
|
|
46
|
-
*
|
|
47
|
-
* @return {Number|undefined} time interval, if out of range then return `undefined`
|
|
48
|
-
*/
|
|
49
|
-
getNextTick() {
|
|
50
|
-
if (this.scheduleConfig.interval) return ms(this.scheduleConfig.interval);
|
|
51
|
-
if (this.cronInstance) {
|
|
52
|
-
const now = Date.now();
|
|
53
|
-
let nextTick;
|
|
54
|
-
do
|
|
55
|
-
try {
|
|
56
|
-
nextTick = this.cronInstance.next().getTime();
|
|
57
|
-
} catch (err) {
|
|
58
|
-
this.logger.info(`[Timer] ${this.key} cron out of the timespan range, error: %s`, err);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
while (now >= nextTick);
|
|
62
|
-
return nextTick - now;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
safeTimeout(handler, delay, ...args) {
|
|
66
|
-
return (delay < safeTimers.maxInterval ? setTimeout : safeTimers.setTimeout)(handler, delay, ...args);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
//#endregion
|
|
71
4
|
export { TimerStrategy };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../../config.default-Oe7rctMS.js";
|
|
2
|
+
import "../../types-D3XkzB3g.js";
|
|
3
|
+
import { TimerStrategy } from "../../agent-DUNNeo72.js";
|
|
2
4
|
|
|
3
5
|
//#region src/lib/strategy/worker.d.ts
|
|
4
6
|
declare class WorkerStrategy extends TimerStrategy {
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../../base-BQ53Qv2t.js";
|
|
2
|
+
import "../../timer-B-5D2wim.js";
|
|
3
|
+
import { WorkerStrategy } from "../../worker-Dc570kZo.js";
|
|
2
4
|
|
|
3
|
-
//#region src/lib/strategy/worker.ts
|
|
4
|
-
var WorkerStrategy = class extends TimerStrategy {
|
|
5
|
-
handler() {
|
|
6
|
-
this.sendOne();
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
//#endregion
|
|
11
5
|
export { WorkerStrategy };
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
//#region src/lib/types.d.ts
|
|
4
|
-
type EggScheduleTask = (ctx: any, ...args: any[]) => Promise<void>;
|
|
5
|
-
interface EggScheduleItem {
|
|
6
|
-
schedule: EggScheduleConfig;
|
|
7
|
-
scheduleQueryString: string;
|
|
8
|
-
task: EggScheduleTask;
|
|
9
|
-
key: string;
|
|
10
|
-
}
|
|
11
|
-
interface EggScheduleJobInfo {
|
|
12
|
-
id: string;
|
|
13
|
-
key: string;
|
|
14
|
-
workerId: number;
|
|
15
|
-
args: any[];
|
|
16
|
-
success?: boolean;
|
|
17
|
-
message?: string;
|
|
18
|
-
rt?: number;
|
|
19
|
-
}
|
|
20
|
-
//#endregion
|
|
1
|
+
import "../config.default-Oe7rctMS.js";
|
|
2
|
+
import { EggScheduleItem, EggScheduleJobInfo, EggScheduleTask } from "../types-D3XkzB3g.js";
|
|
21
3
|
export { EggScheduleItem, EggScheduleJobInfo, EggScheduleTask };
|
package/dist/lib/types.js
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { stringify } from "node:querystring";
|
|
4
|
+
import { isClass, isFunction, isGeneratorFunction } from "is-type-of";
|
|
5
|
+
import { importResolve } from "@eggjs/utils";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/load_schedule.ts
|
|
8
|
+
function getScheduleLoader(app) {
|
|
9
|
+
return class ScheduleLoader extends app.loader.FileLoader {
|
|
10
|
+
async load() {
|
|
11
|
+
const target = this.options.target;
|
|
12
|
+
const items = await this.parse();
|
|
13
|
+
for (const item of items) {
|
|
14
|
+
const schedule = item.exports;
|
|
15
|
+
const fullpath = item.fullpath;
|
|
16
|
+
const scheduleConfig = schedule.schedule;
|
|
17
|
+
assert(scheduleConfig, `schedule(${fullpath}): must have "schedule" and "task" properties`);
|
|
18
|
+
assert(isClass(schedule) || isFunction(schedule.task), `schedule(${fullpath}: \`schedule.task\` should be function or \`schedule\` should be class`);
|
|
19
|
+
let task;
|
|
20
|
+
if (isClass(schedule)) {
|
|
21
|
+
assert(!isGeneratorFunction(schedule.prototype.subscribe), `schedule(${fullpath}): "schedule" generator function is not support, should use async function instead`);
|
|
22
|
+
task = async (ctx, ...args) => {
|
|
23
|
+
return new schedule(ctx).subscribe(...args);
|
|
24
|
+
};
|
|
25
|
+
} else {
|
|
26
|
+
assert(!isGeneratorFunction(schedule.task), `schedule(${fullpath}): "task" generator function is not support, should use async function instead`);
|
|
27
|
+
task = schedule.task;
|
|
28
|
+
}
|
|
29
|
+
const env = app.config.env;
|
|
30
|
+
const envList = schedule.schedule.env;
|
|
31
|
+
if (Array.isArray(envList) && !envList.includes(env)) {
|
|
32
|
+
app.coreLogger.info(`[@eggjs/schedule]: ignore schedule ${fullpath} due to \`schedule.env\` not match`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const realFullpath = importResolve(fullpath);
|
|
36
|
+
target[realFullpath] = {
|
|
37
|
+
schedule: scheduleConfig,
|
|
38
|
+
scheduleQueryString: stringify(scheduleConfig),
|
|
39
|
+
task,
|
|
40
|
+
key: realFullpath
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return target;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function loadSchedule(app) {
|
|
48
|
+
const dirs = [...app.loader.getLoadUnits().map((unit) => path.join(unit.path, "app/schedule")), ...app.config.schedule.directory];
|
|
49
|
+
const Loader = getScheduleLoader(app);
|
|
50
|
+
const schedules = {};
|
|
51
|
+
await new Loader({
|
|
52
|
+
directory: dirs,
|
|
53
|
+
target: schedules,
|
|
54
|
+
inject: app
|
|
55
|
+
}).load();
|
|
56
|
+
Reflect.set(app, "schedules", schedules);
|
|
57
|
+
return schedules;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { loadSchedule };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { loadSchedule } from "./load_schedule-BwGbsGkz.js";
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/schedule.ts
|
|
5
|
+
const debug = debuglog("egg/schedule/lib/schedule");
|
|
6
|
+
var Schedule = class {
|
|
7
|
+
closed = false;
|
|
8
|
+
#agent;
|
|
9
|
+
#logger;
|
|
10
|
+
#strategyClassMap = /* @__PURE__ */ new Map();
|
|
11
|
+
#strategyInstanceMap = /* @__PURE__ */ new Map();
|
|
12
|
+
constructor(agent) {
|
|
13
|
+
this.#agent = agent;
|
|
14
|
+
this.#logger = agent.getLogger("scheduleLogger");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* register a custom Schedule Strategy
|
|
18
|
+
* @param {String} type - strategy type
|
|
19
|
+
* @param {Strategy} clz - Strategy class
|
|
20
|
+
*/
|
|
21
|
+
use(type, clz) {
|
|
22
|
+
this.#strategyClassMap.set(type, clz);
|
|
23
|
+
debug("use type: %o", type);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* load all schedule jobs, then initialize and register special strategy
|
|
27
|
+
*/
|
|
28
|
+
async init() {
|
|
29
|
+
const scheduleItems = await loadSchedule(this.#agent);
|
|
30
|
+
for (const scheduleItem of Object.values(scheduleItems)) this.registerSchedule(scheduleItem);
|
|
31
|
+
}
|
|
32
|
+
registerSchedule(scheduleItem) {
|
|
33
|
+
const { key, schedule } = scheduleItem;
|
|
34
|
+
const type = schedule.type;
|
|
35
|
+
if (schedule.disable) return;
|
|
36
|
+
const Strategy = this.#strategyClassMap.get(type);
|
|
37
|
+
if (!Strategy) {
|
|
38
|
+
const err = /* @__PURE__ */ new Error(`schedule type [${type}] is not defined`);
|
|
39
|
+
err.name = "EggScheduleError";
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
const instance = new Strategy(schedule, this.#agent, key);
|
|
43
|
+
this.#strategyInstanceMap.set(key, instance);
|
|
44
|
+
debug("registerSchedule type: %o, config: %o, key: %o", type, schedule, key);
|
|
45
|
+
}
|
|
46
|
+
unregisterSchedule(key) {
|
|
47
|
+
debug("unregisterSchedule key: %o", key);
|
|
48
|
+
return this.#strategyInstanceMap.delete(key);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* job finish event handler
|
|
52
|
+
*
|
|
53
|
+
* @param {Object} info - { id, key, success, message, workerId }
|
|
54
|
+
*/
|
|
55
|
+
onJobFinish(info) {
|
|
56
|
+
this.#logger.debug(`[Job#${info.id}] ${info.key} finish event received by agent from worker#${info.workerId}`);
|
|
57
|
+
const instance = this.#strategyInstanceMap.get(info.key);
|
|
58
|
+
/* istanbul ignore else */
|
|
59
|
+
if (instance) instance.onJobFinish(info);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* start schedule
|
|
63
|
+
*/
|
|
64
|
+
async start() {
|
|
65
|
+
debug("start");
|
|
66
|
+
this.closed = false;
|
|
67
|
+
for (const instance of this.#strategyInstanceMap.values()) instance.start();
|
|
68
|
+
}
|
|
69
|
+
async close() {
|
|
70
|
+
this.closed = true;
|
|
71
|
+
for (const instance of this.#strategyInstanceMap.values()) await instance.close();
|
|
72
|
+
debug("close");
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { Schedule };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { loadSchedule } from "./load_schedule-BwGbsGkz.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/schedule_worker.ts
|
|
4
|
+
var ScheduleWorker = class {
|
|
5
|
+
#app;
|
|
6
|
+
scheduleItems = {};
|
|
7
|
+
constructor(app) {
|
|
8
|
+
this.#app = app;
|
|
9
|
+
}
|
|
10
|
+
async init() {
|
|
11
|
+
this.scheduleItems = await loadSchedule(this.#app);
|
|
12
|
+
}
|
|
13
|
+
registerSchedule(scheduleItem) {
|
|
14
|
+
this.scheduleItems[scheduleItem.key] = scheduleItem;
|
|
15
|
+
}
|
|
16
|
+
unregisterSchedule(key) {
|
|
17
|
+
delete this.scheduleItems[key];
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { ScheduleWorker };
|