@eggjs/schedule 5.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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +313 -0
  3. package/dist/commonjs/agent.d.ts +7 -0
  4. package/dist/commonjs/agent.js +32 -0
  5. package/dist/commonjs/app/extend/agent.d.ts +2 -0
  6. package/dist/commonjs/app/extend/agent.js +29 -0
  7. package/dist/commonjs/app/extend/application.d.ts +2 -0
  8. package/dist/commonjs/app/extend/application.js +16 -0
  9. package/dist/commonjs/app.d.ts +6 -0
  10. package/dist/commonjs/app.js +131 -0
  11. package/dist/commonjs/config/config.default.d.ts +2 -0
  12. package/dist/commonjs/config/config.default.js +17 -0
  13. package/dist/commonjs/index.d.ts +1 -0
  14. package/dist/commonjs/index.js +18 -0
  15. package/dist/commonjs/lib/load_schedule.d.ts +3 -0
  16. package/dist/commonjs/lib/load_schedule.js +71 -0
  17. package/dist/commonjs/lib/schedule.d.ts +31 -0
  18. package/dist/commonjs/lib/schedule.js +89 -0
  19. package/dist/commonjs/lib/schedule_worker.d.ts +10 -0
  20. package/dist/commonjs/lib/schedule_worker.js +22 -0
  21. package/dist/commonjs/lib/strategy/all.d.ts +4 -0
  22. package/dist/commonjs/lib/strategy/all.js +11 -0
  23. package/dist/commonjs/lib/strategy/base.d.ts +30 -0
  24. package/dist/commonjs/lib/strategy/base.js +79 -0
  25. package/dist/commonjs/lib/strategy/timer.d.ts +20 -0
  26. package/dist/commonjs/lib/strategy/timer.js +99 -0
  27. package/dist/commonjs/lib/strategy/worker.d.ts +4 -0
  28. package/dist/commonjs/lib/strategy/worker.js +11 -0
  29. package/dist/commonjs/lib/types.d.ts +53 -0
  30. package/dist/commonjs/lib/types.js +3 -0
  31. package/dist/commonjs/package.json +3 -0
  32. package/dist/esm/agent.d.ts +7 -0
  33. package/dist/esm/agent.js +29 -0
  34. package/dist/esm/app/extend/agent.d.ts +2 -0
  35. package/dist/esm/app/extend/agent.js +27 -0
  36. package/dist/esm/app/extend/application.d.ts +2 -0
  37. package/dist/esm/app/extend/application.js +14 -0
  38. package/dist/esm/app.d.ts +6 -0
  39. package/dist/esm/app.js +125 -0
  40. package/dist/esm/config/config.default.d.ts +2 -0
  41. package/dist/esm/config/config.default.js +15 -0
  42. package/dist/esm/index.d.ts +1 -0
  43. package/dist/esm/index.js +2 -0
  44. package/dist/esm/lib/load_schedule.d.ts +3 -0
  45. package/dist/esm/lib/load_schedule.js +65 -0
  46. package/dist/esm/lib/schedule.d.ts +31 -0
  47. package/dist/esm/lib/schedule.js +85 -0
  48. package/dist/esm/lib/schedule_worker.d.ts +10 -0
  49. package/dist/esm/lib/schedule_worker.js +18 -0
  50. package/dist/esm/lib/strategy/all.d.ts +4 -0
  51. package/dist/esm/lib/strategy/all.js +7 -0
  52. package/dist/esm/lib/strategy/base.d.ts +30 -0
  53. package/dist/esm/lib/strategy/base.js +75 -0
  54. package/dist/esm/lib/strategy/timer.d.ts +20 -0
  55. package/dist/esm/lib/strategy/timer.js +92 -0
  56. package/dist/esm/lib/strategy/worker.d.ts +4 -0
  57. package/dist/esm/lib/strategy/worker.js +7 -0
  58. package/dist/esm/lib/types.d.ts +53 -0
  59. package/dist/esm/lib/types.js +2 -0
  60. package/dist/esm/package.json +3 -0
  61. package/dist/package.json +4 -0
  62. package/package.json +91 -0
  63. package/src/agent.ts +36 -0
  64. package/src/app/extend/agent.ts +30 -0
  65. package/src/app/extend/application.ts +16 -0
  66. package/src/app.ts +142 -0
  67. package/src/config/config.default.ts +17 -0
  68. package/src/index.ts +1 -0
  69. package/src/lib/load_schedule.ts +74 -0
  70. package/src/lib/schedule.ts +100 -0
  71. package/src/lib/schedule_worker.ts +24 -0
  72. package/src/lib/strategy/all.ts +7 -0
  73. package/src/lib/strategy/base.ts +91 -0
  74. package/src/lib/strategy/timer.ts +106 -0
  75. package/src/lib/strategy/worker.ts +7 -0
  76. package/src/lib/types.ts +58 -0
@@ -0,0 +1,92 @@
1
+ import assert from 'node:assert';
2
+ import { parseExpression } from 'cron-parser';
3
+ import { ms } from 'humanize-ms';
4
+ import safeTimers from 'safe-timers';
5
+ import { logDate } from 'utility';
6
+ import { BaseStrategy } from './base.js';
7
+ export class TimerStrategy extends BaseStrategy {
8
+ cronInstance;
9
+ constructor(scheduleConfig, agent, key) {
10
+ super(scheduleConfig, agent, key);
11
+ const { interval, cron, cronOptions, immediate } = this.scheduleConfig;
12
+ assert(interval || cron || immediate, `[@eggjs/schedule] ${this.key} \`schedule.interval\` or \`schedule.cron\` or \`schedule.immediate\` must be present`);
13
+ // init cron parser
14
+ if (cron) {
15
+ try {
16
+ this.cronInstance = parseExpression(cron, cronOptions);
17
+ }
18
+ catch (err) {
19
+ throw new TypeError(`[@eggjs/schedule] ${this.key} parse cron instruction(${cron}) error: ${err.message}`, { cause: err });
20
+ }
21
+ }
22
+ }
23
+ handler() {
24
+ throw new TypeError(`[@eggjs/schedule] ${this.key} strategy should override \`handler()\` method`);
25
+ }
26
+ start() {
27
+ /* istanbul ignore next */
28
+ if (this.agent.schedule.closed)
29
+ return;
30
+ if (this.scheduleConfig.immediate) {
31
+ this.logger.info(`[Timer] ${this.key} next time will execute immediate`);
32
+ setImmediate(() => this.handler());
33
+ }
34
+ else {
35
+ this.#scheduleNext();
36
+ }
37
+ }
38
+ #scheduleNext() {
39
+ /* istanbul ignore next */
40
+ if (this.agent.schedule.closed)
41
+ return;
42
+ // get next tick
43
+ const nextTick = this.getNextTick();
44
+ if (nextTick) {
45
+ this.logger.info(`[Timer] ${this.key} next time will execute after ${nextTick}ms at ${logDate(new Date(Date.now() + nextTick))}`);
46
+ this.safeTimeout(() => this.handler(), nextTick);
47
+ }
48
+ else {
49
+ this.logger.info(`[Timer] ${this.key} reach endDate, will stop`);
50
+ }
51
+ }
52
+ onJobStart() {
53
+ // Next execution will trigger task at a fix rate, regardless of its execution time.
54
+ this.#scheduleNext();
55
+ }
56
+ /**
57
+ * calculate next tick
58
+ *
59
+ * @return {Number|undefined} time interval, if out of range then return `undefined`
60
+ */
61
+ getNextTick() {
62
+ // interval-style
63
+ if (this.scheduleConfig.interval) {
64
+ return ms(this.scheduleConfig.interval);
65
+ }
66
+ // cron-style
67
+ if (this.cronInstance) {
68
+ // calculate next cron tick
69
+ const now = Date.now();
70
+ let nextTick;
71
+ // loop to find next feature time
72
+ do {
73
+ try {
74
+ const nextInterval = this.cronInstance.next();
75
+ nextTick = nextInterval.getTime();
76
+ }
77
+ catch (err) {
78
+ // Error: Out of the timespan range
79
+ this.logger.info(`[Timer] ${this.key} cron out of the timespan range, error: %s`, err);
80
+ return;
81
+ }
82
+ } while (now >= nextTick);
83
+ return nextTick - now;
84
+ }
85
+ // won\'t run here
86
+ }
87
+ safeTimeout(handler, delay, ...args) {
88
+ const fn = delay < safeTimers.maxInterval ? setTimeout : safeTimers.setTimeout;
89
+ return fn(handler, delay, ...args);
90
+ }
91
+ }
92
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGltZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL3N0cmF0ZWd5L3RpbWVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sTUFBTSxNQUFNLGFBQWEsQ0FBQztBQUNqQyxPQUFPLEVBQUUsZUFBZSxFQUF1QixNQUFNLGFBQWEsQ0FBQztBQUNuRSxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBQ2pDLE9BQU8sVUFBVSxNQUFNLGFBQWEsQ0FBQztBQUNyQyxPQUFPLEVBQUUsT0FBTyxFQUFFLE1BQU0sU0FBUyxDQUFDO0FBR2xDLE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFekMsTUFBTSxPQUFnQixhQUFjLFNBQVEsWUFBWTtJQUM1QyxZQUFZLENBQWtC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DQUFDLENBQUMsVUFBVSxDQUFDLFVBQVUsQ0FBQztRQUMvRSxPQUFPLEVBQUUsQ0FBQyxPQUFPLEVBQUUsS0FBSyxFQUFFLEdBQUcsSUFBSSxDQUFDLENBQUM7SUFDckMsQ0FBQztDQUNGIn0=
@@ -0,0 +1,4 @@
1
+ import { TimerStrategy } from './timer.js';
2
+ export declare class WorkerStrategy extends TimerStrategy {
3
+ handler(): void;
4
+ }
@@ -0,0 +1,7 @@
1
+ import { TimerStrategy } from './timer.js';
2
+ export class WorkerStrategy extends TimerStrategy {
3
+ handler() {
4
+ this.sendOne();
5
+ }
6
+ }
7
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ya2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2xpYi9zdHJhdGVneS93b3JrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGFBQWEsRUFBRSxNQUFNLFlBQVksQ0FBQztBQUUzQyxNQUFNLE9BQU8sY0FBZSxTQUFRLGFBQWE7SUFDL0MsT0FBTztRQUNMLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztJQUNqQixDQUFDO0NBQ0YifQ==
@@ -0,0 +1,53 @@
1
+ import type { ParserOptions as CronOptions } from 'cron-parser';
2
+ import type { Schedule } from './schedule.js';
3
+ import type { ScheduleWorker } from './schedule_worker.js';
4
+ /**
5
+ * Schedule Config
6
+ * @see https://www.eggjs.org/zh-CN/basics/schedule
7
+ */
8
+ export interface EggScheduleConfig {
9
+ type?: 'worker' | 'all';
10
+ interval?: string | number;
11
+ cron?: string;
12
+ cronOptions?: CronOptions;
13
+ immediate?: boolean;
14
+ disable?: boolean;
15
+ env?: string[];
16
+ }
17
+ export type EggScheduleTask = (ctx: any, ...args: any[]) => Promise<void>;
18
+ export interface EggScheduleItem {
19
+ schedule: EggScheduleConfig;
20
+ scheduleQueryString: string;
21
+ task: EggScheduleTask;
22
+ key: string;
23
+ }
24
+ export interface EggScheduleJobInfo {
25
+ id: string;
26
+ key: string;
27
+ workerId: number;
28
+ args: any[];
29
+ success?: boolean;
30
+ message?: string;
31
+ rt?: number;
32
+ }
33
+ declare module 'egg' {
34
+ interface EggScheduleAgent {
35
+ schedule: Schedule;
36
+ }
37
+ interface Agent extends EggScheduleAgent {
38
+ }
39
+ interface EggScheduleApplication {
40
+ scheduleWorker: ScheduleWorker;
41
+ /** runSchedule in unittest */
42
+ runSchedule: (schedulePath: string, ...args: any[]) => Promise<void>;
43
+ }
44
+ interface Application extends EggScheduleApplication {
45
+ }
46
+ interface EggScheduleAppConfig {
47
+ schedule: {
48
+ directory: string[];
49
+ };
50
+ }
51
+ interface EggAppConfig extends EggScheduleAppConfig {
52
+ }
53
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3R5cGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiIifQ==
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "@eggjs/schedule",
3
+ "version": "5.0.0"
4
+ }
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@eggjs/schedule",
3
+ "version": "5.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "engines": {
8
+ "node": ">=18.19.0"
9
+ },
10
+ "description": "schedule plugin for egg, support corn job.",
11
+ "eggPlugin": {
12
+ "name": "schedule",
13
+ "exports": {
14
+ "import": "./dist/esm",
15
+ "require": "./dist/commonjs"
16
+ }
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git@github.com:eggjs/egg-schedule.git"
21
+ },
22
+ "keywords": [
23
+ "egg",
24
+ "egg-plugin",
25
+ "eggPlugin",
26
+ "schedule",
27
+ "cron"
28
+ ],
29
+ "dependencies": {
30
+ "@eggjs/utils": "^4.0.3",
31
+ "cron-parser": "^4.9.0",
32
+ "humanize-ms": "^2.0.0",
33
+ "is-type-of": "^2.1.0",
34
+ "safe-timers": "^1.1.0",
35
+ "utility": "^2.1.0"
36
+ },
37
+ "devDependencies": {
38
+ "@arethetypeswrong/cli": "^0.17.1",
39
+ "@eggjs/tsconfig": "1",
40
+ "@types/mocha": "10",
41
+ "@types/node": "22",
42
+ "@types/safe-timers": "^1.1.2",
43
+ "egg": "beta",
44
+ "egg-bin": "beta",
45
+ "egg-logrotator": "^3.2.0",
46
+ "egg-mock": "beta",
47
+ "egg-tracer": "2",
48
+ "eslint": "8",
49
+ "eslint-config-egg": "14",
50
+ "tshy": "3",
51
+ "tshy-after": "1",
52
+ "typescript": "5"
53
+ },
54
+ "scripts": {
55
+ "lint": "eslint --cache src test --ext .ts",
56
+ "pretest": "npm run lint -- --fix && npm run prepublishOnly",
57
+ "test": "egg-bin test",
58
+ "preci": "npm run lint && npm run prepublishOnly && attw --pack",
59
+ "ci": "egg-bin cov",
60
+ "prepublishOnly": "tshy && tshy-after"
61
+ },
62
+ "author": "dead_horse",
63
+ "license": "MIT",
64
+ "type": "module",
65
+ "tshy": {
66
+ "exports": {
67
+ ".": "./src/index.ts",
68
+ "./package.json": "./package.json"
69
+ }
70
+ },
71
+ "exports": {
72
+ ".": {
73
+ "import": {
74
+ "types": "./dist/esm/index.d.ts",
75
+ "default": "./dist/esm/index.js"
76
+ },
77
+ "require": {
78
+ "types": "./dist/commonjs/index.d.ts",
79
+ "default": "./dist/commonjs/index.js"
80
+ }
81
+ },
82
+ "./package.json": "./package.json"
83
+ },
84
+ "files": [
85
+ "dist",
86
+ "src"
87
+ ],
88
+ "types": "./dist/commonjs/index.d.ts",
89
+ "main": "./dist/commonjs/index.js",
90
+ "module": "./dist/esm/index.js"
91
+ }
package/src/agent.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { debuglog } from 'node:util';
2
+ import type { Agent, ILifecycleBoot } from 'egg';
3
+ import { WorkerStrategy } from './lib/strategy/worker.js';
4
+ import { AllStrategy } from './lib/strategy/all.js';
5
+ import { EggScheduleJobInfo } from './lib/types.js';
6
+
7
+ const debug = debuglog('@eggjs/schedule/agent');
8
+
9
+ export default class Boot implements ILifecycleBoot {
10
+ #agent: Agent;
11
+ constructor(agent: Agent) {
12
+ this.#agent = agent;
13
+ }
14
+
15
+ async didLoad(): Promise<void> {
16
+ // register built-in strategy
17
+ this.#agent.schedule.use('worker', WorkerStrategy);
18
+ this.#agent.schedule.use('all', AllStrategy);
19
+
20
+ // wait for other plugin to register custom strategy
21
+ await this.#agent.schedule.init();
22
+
23
+ // dispatch job finish event to strategy
24
+ this.#agent.messenger.on('egg-schedule', (info: EggScheduleJobInfo) => {
25
+ // get job info from worker
26
+ this.#agent.schedule.onJobFinish(info);
27
+ });
28
+ debug('didLoad');
29
+ }
30
+
31
+ async serverDidReady(): Promise<void> {
32
+ // start schedule after worker ready
33
+ this.#agent.schedule.start();
34
+ debug('serverDidReady, schedule start');
35
+ }
36
+ }
@@ -0,0 +1,30 @@
1
+ import { BaseStrategy } from '../../lib/strategy/base.js';
2
+ import { TimerStrategy } from '../../lib/strategy/timer.js';
3
+ import { Schedule } from '../../lib/schedule.js';
4
+
5
+ const SCHEDULE = Symbol('agent#schedule');
6
+
7
+ export default {
8
+ /**
9
+ * @member agent#ScheduleStrategy
10
+ */
11
+ ScheduleStrategy: BaseStrategy,
12
+
13
+ /**
14
+ * @member agent#TimerScheduleStrategy
15
+ */
16
+ TimerScheduleStrategy: TimerStrategy,
17
+
18
+ /**
19
+ * @member agent#schedule
20
+ */
21
+ get schedule() {
22
+ if (!this[SCHEDULE]) {
23
+ this[SCHEDULE] = new Schedule(this);
24
+ this.lifecycle.registerBeforeClose(() => {
25
+ return this[SCHEDULE].close();
26
+ });
27
+ }
28
+ return this[SCHEDULE];
29
+ },
30
+ } as any;
@@ -0,0 +1,16 @@
1
+ import { ScheduleWorker } from '../../lib/schedule_worker.js';
2
+
3
+ const SCHEDULE_WORKER = Symbol('application#scheduleWorker');
4
+
5
+ export default {
6
+ /**
7
+ * @member app#schedule
8
+ */
9
+ get scheduleWorker() {
10
+ if (!this[SCHEDULE_WORKER]) {
11
+ this[SCHEDULE_WORKER] = new ScheduleWorker(this);
12
+ }
13
+ return this[SCHEDULE_WORKER];
14
+ },
15
+ } as any;
16
+
package/src/app.ts ADDED
@@ -0,0 +1,142 @@
1
+ import { debuglog } from 'node:util';
2
+ import path from 'node:path';
3
+ import type {
4
+ Application, ILifecycleBoot, EggLogger,
5
+ } from 'egg';
6
+ import { importResolve } from '@eggjs/utils';
7
+ import { EggScheduleItem, EggScheduleJobInfo } from './lib/types.js';
8
+
9
+ const debug = debuglog('@eggjs/schedule/app');
10
+
11
+ export default class Boot implements ILifecycleBoot {
12
+ #app: Application;
13
+ #logger: EggLogger;
14
+ constructor(app: Application) {
15
+ this.#app = app;
16
+ this.#logger = app.getLogger('scheduleLogger');
17
+ }
18
+
19
+ async didLoad(): Promise<void> {
20
+ const scheduleWorker = this.#app.scheduleWorker;
21
+ await scheduleWorker.init();
22
+
23
+ // log schedule list
24
+ for (const s in scheduleWorker.scheduleItems) {
25
+ const schedule = scheduleWorker.scheduleItems[s];
26
+ if (!schedule.schedule.disable) {
27
+ this.#logger.info('[@eggjs/schedule]: register schedule %s', schedule.key);
28
+ }
29
+ }
30
+
31
+ // register schedule event
32
+ this.#app.messenger.on('egg-schedule', async info => {
33
+ debug('app got "egg-schedule" message: %o', info);
34
+ const { id, key } = info;
35
+ this.#logger.debug(`[Job#${id}] ${key} await app ready`);
36
+ await this.#app.ready();
37
+ const schedule = scheduleWorker.scheduleItems[key];
38
+ this.#logger.debug(`[Job#${id}] ${key} task received by app`);
39
+
40
+ if (!schedule) {
41
+ this.#logger.warn(`[Job#${id}] ${key} unknown task`);
42
+ return;
43
+ }
44
+
45
+ /* istanbul ignore next */
46
+ if (schedule.schedule.disable) {
47
+ this.#logger.warn(`[Job#${id}] ${key} disable`);
48
+ return;
49
+ }
50
+
51
+ this.#logger.info(`[Job#${id}] ${key} executing by app`);
52
+
53
+ // run with anonymous context
54
+ const ctx = this.#app.createAnonymousContext({
55
+ method: 'SCHEDULE',
56
+ url: `/__schedule?path=${key}&${schedule.scheduleQueryString}`,
57
+ });
58
+
59
+ const start = Date.now();
60
+
61
+ let success: boolean;
62
+ let e: Error | undefined;
63
+ try {
64
+ // execute
65
+ await this.#app.ctxStorage.run(ctx, async () => {
66
+ return await schedule.task(ctx, ...info.args);
67
+ });
68
+ success = true;
69
+ } catch (err: any) {
70
+ success = false;
71
+ e = err;
72
+ }
73
+
74
+ const rt = Date.now() - start;
75
+
76
+ const msg = `[Job#${id}] ${key} execute ${success ? 'succeed' : 'failed'}, used ${rt}ms.`;
77
+ if (success) {
78
+ this.#logger.info(msg);
79
+ } else {
80
+ this.#logger.error(msg, e);
81
+ }
82
+
83
+ // notify agent job finish
84
+ this.#app.messenger.sendToAgent('egg-schedule', {
85
+ ...info,
86
+ success,
87
+ workerId: process.pid,
88
+ rt,
89
+ message: e?.message,
90
+ } as EggScheduleJobInfo);
91
+ });
92
+
93
+ // for test purpose
94
+ const config = this.#app.config;
95
+ const directory = [
96
+ path.join(config.baseDir, 'app/schedule'),
97
+ ...config.schedule.directory,
98
+ ];
99
+ const runSchedule = async (schedulePath: string, ...args: any[]) => {
100
+ debug('[runSchedule] start schedulePath: %o, args: %o', schedulePath, args);
101
+
102
+ // resolve real path
103
+ if (path.isAbsolute(schedulePath)) {
104
+ schedulePath = importResolve(schedulePath);
105
+ } else {
106
+ for (const dir of directory) {
107
+ const trySchedulePath = path.join(dir, schedulePath);
108
+ try {
109
+ schedulePath = importResolve(trySchedulePath);
110
+ break;
111
+ } catch (err) {
112
+ debug('[runSchedule] importResolve %o error: %s', trySchedulePath, err);
113
+ }
114
+ }
115
+ }
116
+
117
+ debug('[runSchedule] resolve schedulePath: %o', schedulePath);
118
+ let schedule: EggScheduleItem;
119
+ try {
120
+ schedule = scheduleWorker.scheduleItems[schedulePath];
121
+ if (!schedule) {
122
+ throw new Error(`Cannot find schedule ${schedulePath}`);
123
+ }
124
+ } catch (err: any) {
125
+ err.message = `[@eggjs/schedule] ${err.message}`;
126
+ throw err;
127
+ }
128
+
129
+ // run with anonymous context
130
+ const ctx = this.#app.createAnonymousContext({
131
+ method: 'SCHEDULE',
132
+ url: `/__schedule?path=${schedulePath}&${schedule.scheduleQueryString}`,
133
+ });
134
+ return await this.#app.ctxStorage.run(ctx, async () => {
135
+ return await schedule.task(ctx, ...args);
136
+ });
137
+ };
138
+ Reflect.set(this.#app, 'runSchedule', runSchedule);
139
+
140
+ debug('didLoad');
141
+ }
142
+ }
@@ -0,0 +1,17 @@
1
+ export default () => {
2
+ const config = {} as Record<string, any>;
3
+
4
+ config.customLogger = {
5
+ scheduleLogger: {
6
+ consoleLevel: 'NONE',
7
+ file: 'egg-schedule.log',
8
+ },
9
+ };
10
+
11
+ config.schedule = {
12
+ // custom additional directory, full path
13
+ directory: [],
14
+ };
15
+
16
+ return config;
17
+ };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/types.js';
@@ -0,0 +1,74 @@
1
+ import path from 'node:path';
2
+ import assert from 'node:assert';
3
+ import { stringify } from 'node:querystring';
4
+ import { isClass, isFunction, isGeneratorFunction } from 'is-type-of';
5
+ import { importResolve } from '@eggjs/utils';
6
+ import type { EggApplicationCore, EggContext } from 'egg';
7
+ import type { EggScheduleConfig, EggScheduleTask, EggScheduleItem } from './types.js';
8
+
9
+ function getScheduleLoader(app: EggApplicationCore) {
10
+ return class ScheduleLoader extends app.loader.FileLoader {
11
+ async load() {
12
+ const target = this.options.target as Record<string, EggScheduleItem>;
13
+ const items = await this.parse();
14
+ for (const item of items) {
15
+ const schedule = item.exports as { schedule: EggScheduleConfig, task: EggScheduleTask };
16
+ const fullpath = item.fullpath;
17
+ const scheduleConfig = schedule.schedule;
18
+ assert(scheduleConfig, `schedule(${fullpath}): must have "schedule" and "task" properties`);
19
+ assert(isClass(schedule) || isFunction(schedule.task),
20
+ `schedule(${fullpath}: \`schedule.task\` should be function or \`schedule\` should be class`);
21
+
22
+ let task: EggScheduleTask;
23
+ if (isClass(schedule)) {
24
+ assert(!isGeneratorFunction(schedule.prototype.subscribe),
25
+ `schedule(${fullpath}): "schedule" generator function is not support, should use async function instead`);
26
+ task = async (ctx: EggContext, ...args: any[]) => {
27
+ const instance = new schedule(ctx);
28
+ // s.subscribe = app.toAsyncFunction(s.subscribe);
29
+ return instance.subscribe(...args);
30
+ };
31
+ } else {
32
+ assert(!isGeneratorFunction(schedule.task),
33
+ `schedule(${fullpath}): "task" generator function is not support, should use async function instead`);
34
+ task = schedule.task;
35
+ // task = app.toAsyncFunction(schedule.task);
36
+ }
37
+
38
+ const env = app.config.env;
39
+ const envList = schedule.schedule.env;
40
+ if (Array.isArray(envList) && !envList.includes(env)) {
41
+ app.coreLogger.info(`[@eggjs/schedule]: ignore schedule ${fullpath} due to \`schedule.env\` not match`);
42
+ continue;
43
+ }
44
+
45
+ // handle symlink case
46
+ const realFullpath = importResolve(fullpath);
47
+ target[realFullpath] = {
48
+ schedule: scheduleConfig,
49
+ scheduleQueryString: stringify(scheduleConfig as any),
50
+ task,
51
+ key: realFullpath,
52
+ };
53
+ }
54
+ return target;
55
+ }
56
+ };
57
+ }
58
+
59
+ export async function loadSchedule(app: EggApplicationCore) {
60
+ const dirs = [
61
+ ...app.loader.getLoadUnits().map(unit => path.join(unit.path, 'app/schedule')),
62
+ ...app.config.schedule.directory,
63
+ ];
64
+
65
+ const Loader = getScheduleLoader(app);
66
+ const schedules = {} as Record<string, EggScheduleItem>;
67
+ await new Loader({
68
+ directory: dirs,
69
+ target: schedules,
70
+ inject: app,
71
+ }).load();
72
+ Reflect.set(app, 'schedules', schedules);
73
+ return schedules;
74
+ }