@eggjs/schedule 6.0.0-beta.20 → 6.0.0-beta.22
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.d.ts +8 -14
- package/dist/agent.js +34 -34
- package/dist/app/extend/agent.d.ts +18 -4
- package/dist/app/extend/agent.js +30 -7
- package/dist/app/extend/application.d.ts +8 -4
- package/dist/app/extend/application.js +16 -5
- package/dist/app/extend/application.unittest.d.ts +4 -5
- package/dist/app/extend/application.unittest.js +51 -6
- package/dist/app.d.ts +6 -12
- package/dist/app.js +77 -69
- package/dist/config/config.default.d.ts +17 -2
- package/dist/config/config.default.js +12 -12
- package/dist/index.d.ts +8 -7
- package/dist/index.js +9 -12
- package/dist/lib/load_schedule.d.ts +3 -8
- package/dist/lib/load_schedule.js +66 -3
- package/dist/lib/schedule.d.ts +31 -4
- package/dist/lib/schedule.js +85 -4
- package/dist/lib/schedule_worker.d.ts +10 -4
- package/dist/lib/schedule_worker.js +18 -4
- package/dist/lib/strategy/all.d.ts +3 -9
- package/dist/lib/strategy/all.js +7 -5
- package/dist/lib/strategy/base.d.ts +32 -4
- package/dist/lib/strategy/base.js +75 -3
- package/dist/lib/strategy/timer.d.ts +20 -4
- package/dist/lib/strategy/timer.js +94 -4
- package/dist/lib/strategy/worker.d.ts +3 -9
- package/dist/lib/strategy/worker.js +7 -5
- package/dist/lib/types.d.ts +17 -3
- package/dist/lib/types.js +2 -3
- package/dist/types.d.ts +16 -2
- package/dist/types.js +2 -3
- package/package.json +7 -7
- package/dist/agent-BiclCXUu.js +0 -32
- package/dist/agent-DUNNeo72.d.ts +0 -100
- package/dist/all-DY7LEzzF.js +0 -11
- package/dist/application-Be3vYRp0.d.ts +0 -22
- package/dist/application-CtPD5smr.js +0 -18
- package/dist/application.unittest-BySM_r-a.js +0 -46
- package/dist/application.unittest-DOurfxcE.d.ts +0 -8
- package/dist/base-BQ53Qv2t.js +0 -73
- package/dist/config.default-Oe7rctMS.d.ts +0 -20
- package/dist/load_schedule-BwGbsGkz.js +0 -61
- package/dist/schedule-C0pZBoiR.js +0 -77
- package/dist/schedule_worker-DsbRF__K.js +0 -22
- package/dist/timer-B-5D2wim.js +0 -71
- package/dist/types-C43iyhAd.js +0 -1
- package/dist/types-CPNMyF89.js +0 -1
- package/dist/types-D3XkzB3g.d.ts +0 -21
- package/dist/types-VUtK3imu.d.ts +0 -17
- package/dist/worker-Dc570kZo.js +0 -11
package/dist/agent-DUNNeo72.d.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { EggScheduleConfig } from "./config.default-Oe7rctMS.js";
|
|
2
|
-
import { EggScheduleItem, EggScheduleJobInfo } from "./types-D3XkzB3g.js";
|
|
3
|
-
import { CronExpression } from "cron-parser";
|
|
4
|
-
import safeTimers from "safe-timers";
|
|
5
|
-
import { Agent, EggLogger } from "egg";
|
|
6
|
-
|
|
7
|
-
//#region src/lib/strategy/base.d.ts
|
|
8
|
-
declare class BaseStrategy {
|
|
9
|
-
protected agent: ScheduleAgent;
|
|
10
|
-
protected scheduleConfig: EggScheduleConfig;
|
|
11
|
-
protected key: string;
|
|
12
|
-
protected logger: EggLogger;
|
|
13
|
-
protected closed: boolean;
|
|
14
|
-
count: number;
|
|
15
|
-
constructor(scheduleConfig: EggScheduleConfig, agent: ScheduleAgent, key: string);
|
|
16
|
-
/** keep compatibility */
|
|
17
|
-
get schedule(): EggScheduleConfig;
|
|
18
|
-
start(): Promise<void>;
|
|
19
|
-
close(): Promise<void>;
|
|
20
|
-
onJobStart(_info: EggScheduleJobInfo): void;
|
|
21
|
-
onJobFinish(_info: EggScheduleJobInfo): void;
|
|
22
|
-
/**
|
|
23
|
-
* trigger one worker
|
|
24
|
-
*
|
|
25
|
-
* @param {...any} args - pass to job task
|
|
26
|
-
*/
|
|
27
|
-
sendOne(...args: any[]): void;
|
|
28
|
-
/**
|
|
29
|
-
* trigger all worker
|
|
30
|
-
*
|
|
31
|
-
* @param {...any} args - pass to job task
|
|
32
|
-
*/
|
|
33
|
-
sendAll(...args: any[]): void;
|
|
34
|
-
getSeqId(): string;
|
|
35
|
-
}
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/lib/strategy/timer.d.ts
|
|
38
|
-
declare abstract class TimerStrategy extends BaseStrategy {
|
|
39
|
-
#private;
|
|
40
|
-
protected cronInstance?: CronExpression;
|
|
41
|
-
constructor(scheduleConfig: EggScheduleConfig, agent: ScheduleAgent, key: string);
|
|
42
|
-
protected handler(): void;
|
|
43
|
-
start(): Promise<void>;
|
|
44
|
-
onJobStart(): void;
|
|
45
|
-
/**
|
|
46
|
-
* calculate next tick
|
|
47
|
-
*
|
|
48
|
-
* @return {Number|undefined} time interval, if out of range then return `undefined`
|
|
49
|
-
*/
|
|
50
|
-
protected getNextTick(): number | undefined;
|
|
51
|
-
protected safeTimeout(handler: () => void, delay: number, ...args: any[]): number | safeTimers.Timeout;
|
|
52
|
-
}
|
|
53
|
-
//#endregion
|
|
54
|
-
//#region src/lib/schedule.d.ts
|
|
55
|
-
declare class Schedule {
|
|
56
|
-
#private;
|
|
57
|
-
closed: boolean;
|
|
58
|
-
constructor(agent: ScheduleAgent);
|
|
59
|
-
/**
|
|
60
|
-
* register a custom Schedule Strategy
|
|
61
|
-
* @param {String} type - strategy type
|
|
62
|
-
* @param {Strategy} clz - Strategy class
|
|
63
|
-
*/
|
|
64
|
-
use(type: string, clz: typeof BaseStrategy): void;
|
|
65
|
-
/**
|
|
66
|
-
* load all schedule jobs, then initialize and register special strategy
|
|
67
|
-
*/
|
|
68
|
-
init(): Promise<void>;
|
|
69
|
-
registerSchedule(scheduleItem: EggScheduleItem): void;
|
|
70
|
-
unregisterSchedule(key: string): boolean;
|
|
71
|
-
/**
|
|
72
|
-
* job finish event handler
|
|
73
|
-
*
|
|
74
|
-
* @param {Object} info - { id, key, success, message, workerId }
|
|
75
|
-
*/
|
|
76
|
-
onJobFinish(info: EggScheduleJobInfo): void;
|
|
77
|
-
/**
|
|
78
|
-
* start schedule
|
|
79
|
-
*/
|
|
80
|
-
start(): Promise<void>;
|
|
81
|
-
close(): Promise<void>;
|
|
82
|
-
}
|
|
83
|
-
//#endregion
|
|
84
|
-
//#region src/app/extend/agent.d.ts
|
|
85
|
-
declare class ScheduleAgent extends Agent {
|
|
86
|
-
/**
|
|
87
|
-
* @member agent#ScheduleStrategy
|
|
88
|
-
*/
|
|
89
|
-
get ScheduleStrategy(): typeof BaseStrategy;
|
|
90
|
-
/**
|
|
91
|
-
* @member agent#TimerScheduleStrategy
|
|
92
|
-
*/
|
|
93
|
-
get TimerScheduleStrategy(): typeof TimerStrategy;
|
|
94
|
-
/**
|
|
95
|
-
* @member agent#schedule
|
|
96
|
-
*/
|
|
97
|
-
get schedule(): Schedule;
|
|
98
|
-
}
|
|
99
|
-
//#endregion
|
|
100
|
-
export { BaseStrategy, Schedule, ScheduleAgent, TimerStrategy };
|
package/dist/all-DY7LEzzF.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { EggScheduleItem } from "./types-D3XkzB3g.js";
|
|
2
|
-
import { Application } from "egg";
|
|
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
|
|
14
|
-
//#region src/app/extend/application.d.ts
|
|
15
|
-
declare class ScheduleApplication extends Application {
|
|
16
|
-
/**
|
|
17
|
-
* @member app#schedule
|
|
18
|
-
*/
|
|
19
|
-
get scheduleWorker(): ScheduleWorker;
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { ScheduleApplication, ScheduleWorker };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { ScheduleWorker } from "./schedule_worker-DsbRF__K.js";
|
|
2
|
-
import { Application } from "egg";
|
|
3
|
-
|
|
4
|
-
//#region src/app/extend/application.ts
|
|
5
|
-
const SCHEDULE_WORKER = Symbol("application scheduleWorker");
|
|
6
|
-
var ScheduleApplication = class extends Application {
|
|
7
|
-
/**
|
|
8
|
-
* @member app#schedule
|
|
9
|
-
*/
|
|
10
|
-
get scheduleWorker() {
|
|
11
|
-
let scheduleWorker = this[SCHEDULE_WORKER];
|
|
12
|
-
if (!scheduleWorker) this[SCHEDULE_WORKER] = scheduleWorker = new ScheduleWorker(this);
|
|
13
|
-
return scheduleWorker;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
export { ScheduleApplication };
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { ScheduleApplication } from "./application-CtPD5smr.js";
|
|
2
|
-
import { debuglog } from "node:util";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { importResolve } from "@eggjs/utils";
|
|
5
|
-
|
|
6
|
-
//#region src/app/extend/application.unittest.ts
|
|
7
|
-
const debug = debuglog("egg/schedule/app");
|
|
8
|
-
var ScheduleApplicationUnittest = class extends ScheduleApplication {
|
|
9
|
-
async runSchedule(schedulePath, ...args) {
|
|
10
|
-
debug("[runSchedule] start schedulePath: %o, args: %o", schedulePath, args);
|
|
11
|
-
const config = this.config;
|
|
12
|
-
const directory = [path.join(config.baseDir, "app/schedule"), ...config.schedule.directory];
|
|
13
|
-
if (path.isAbsolute(schedulePath)) schedulePath = importResolve(schedulePath);
|
|
14
|
-
else for (const dir of directory) {
|
|
15
|
-
const trySchedulePath = path.join(dir, schedulePath);
|
|
16
|
-
try {
|
|
17
|
-
schedulePath = importResolve(trySchedulePath);
|
|
18
|
-
break;
|
|
19
|
-
} catch (err) {
|
|
20
|
-
debug("[runSchedule] importResolve %o error: %s", trySchedulePath, err);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
debug("[runSchedule] resolve schedulePath: %o", schedulePath);
|
|
24
|
-
let schedule;
|
|
25
|
-
try {
|
|
26
|
-
schedule = this.scheduleWorker.scheduleItems[schedulePath];
|
|
27
|
-
if (!schedule) {
|
|
28
|
-
debug("[runSchedule] Cannot find schedule %o, scheduleItems: %o", schedulePath, this.scheduleWorker.scheduleItems);
|
|
29
|
-
throw new TypeError(`Cannot find schedule ${schedulePath}`);
|
|
30
|
-
}
|
|
31
|
-
} catch (err) {
|
|
32
|
-
err.message = `[@eggjs/schedule] ${err.message}`;
|
|
33
|
-
throw err;
|
|
34
|
-
}
|
|
35
|
-
const ctx = this.createAnonymousContext({
|
|
36
|
-
method: "SCHEDULE",
|
|
37
|
-
url: `/__schedule?path=${schedulePath}&${schedule.scheduleQueryString}`
|
|
38
|
-
});
|
|
39
|
-
return await this.ctxStorage.run(ctx, async () => {
|
|
40
|
-
return await schedule.task(ctx, ...args);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
//#endregion
|
|
46
|
-
export { ScheduleApplicationUnittest };
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ScheduleApplication } from "./application-Be3vYRp0.js";
|
|
2
|
-
|
|
3
|
-
//#region src/app/extend/application.unittest.d.ts
|
|
4
|
-
declare class ScheduleApplicationUnittest extends ScheduleApplication {
|
|
5
|
-
runSchedule(schedulePath: string, ...args: any[]): Promise<void>;
|
|
6
|
-
}
|
|
7
|
-
//#endregion
|
|
8
|
-
export { ScheduleApplicationUnittest };
|
package/dist/base-BQ53Qv2t.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
//#region src/lib/strategy/base.ts
|
|
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
|
-
};
|
|
71
|
-
|
|
72
|
-
//#endregion
|
|
73
|
-
export { BaseStrategy };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ParserOptions } from "cron-parser";
|
|
2
|
-
import { PartialEggConfig } from "egg";
|
|
3
|
-
|
|
4
|
-
//#region src/config/config.default.d.ts
|
|
5
|
-
interface EggScheduleConfig {
|
|
6
|
-
type?: 'worker' | 'all';
|
|
7
|
-
interval?: string | number;
|
|
8
|
-
cron?: string;
|
|
9
|
-
cronOptions?: ParserOptions;
|
|
10
|
-
immediate?: boolean;
|
|
11
|
-
disable?: boolean;
|
|
12
|
-
env?: string[];
|
|
13
|
-
/**
|
|
14
|
-
* custom additional directory, full path
|
|
15
|
-
*/
|
|
16
|
-
directory: string[];
|
|
17
|
-
}
|
|
18
|
-
declare const _default: PartialEggConfig;
|
|
19
|
-
//#endregion
|
|
20
|
-
export { EggScheduleConfig, _default };
|
|
@@ -1,61 +0,0 @@
|
|
|
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 };
|
|
@@ -1,77 +0,0 @@
|
|
|
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 };
|
|
@@ -1,22 +0,0 @@
|
|
|
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 };
|
package/dist/timer-B-5D2wim.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { BaseStrategy } from "./base-BQ53Qv2t.js";
|
|
2
|
-
import assert from "node:assert";
|
|
3
|
-
import cronParser from "cron-parser";
|
|
4
|
-
import { ms } from "humanize-ms";
|
|
5
|
-
import safeTimers from "safe-timers";
|
|
6
|
-
import { logDate } from "utility";
|
|
7
|
-
|
|
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
|
-
export { TimerStrategy };
|
package/dist/types-C43iyhAd.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types-CPNMyF89.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types-D3XkzB3g.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { EggScheduleConfig } from "./config.default-Oe7rctMS.js";
|
|
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
|
|
21
|
-
export { EggScheduleItem, EggScheduleJobInfo, EggScheduleTask };
|
package/dist/types-VUtK3imu.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { EggScheduleConfig } from "./config.default-Oe7rctMS.js";
|
|
2
|
-
|
|
3
|
-
//#region src/types.d.ts
|
|
4
|
-
declare module 'egg' {
|
|
5
|
-
interface EggAppConfig {
|
|
6
|
-
/**
|
|
7
|
-
* Schedule Config
|
|
8
|
-
* @see https://www.eggjs.org/zh-CN/basics/schedule
|
|
9
|
-
*/
|
|
10
|
-
schedule: EggScheduleConfig;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
declare module '@eggjs/mock' {
|
|
14
|
-
interface MockApplication {
|
|
15
|
-
runSchedule(schedulePath: string, ...args: any[]): Promise<any>;
|
|
16
|
-
}
|
|
17
|
-
}
|
package/dist/worker-Dc570kZo.js
DELETED