Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 17, 2024
1 parent 6c8d855 commit 2c558cb
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ See `${appInfo.root}/logs/{app_name}/egg-schedule.log` which provided by [config
// config/config.default.ts
import { EggAppConfig } from 'egg';

export default const config = {
export default {
customLogger: {
scheduleLogger: {
// consoleLevel: 'NONE',
Expand All @@ -276,7 +276,7 @@ If you want to add additional schedule directories, you can use this config.
// config/config.default.ts
import { EggAppConfig } from 'egg';

export default const config = {
export default {
schedule: {
directory: [
'path/to/otherSchedule',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
"test": "egg-bin test",
"preci": "npm run lint && npm run prepublishOnly",
"ci": "egg-bin cov && attw --pack",
"preci": "npm run lint && npm run prepublishOnly && attw --pack",
"ci": "egg-bin cov",
"prepublishOnly": "tshy && tshy-after"
},
"author": "dead_horse",
Expand Down
4 changes: 2 additions & 2 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { debuglog } from 'node:util';
import type { Agent, ILifecycleBoot } from 'egg';
import { WorkerStrategy } from './lib/strategy/worker.js';
import { AllStrategy } from './lib/strategy/all.js';
import { ScheduleJobInfo } from './lib/types.js';
import { EggScheduleJobInfo } from './lib/types.js';

const debug = debuglog('@eggjs/schedule/agent');

Expand All @@ -21,7 +21,7 @@ export default class Boot implements ILifecycleBoot {
await this.#agent.schedule.init();

// dispatch job finish event to strategy
this.#agent.messenger.on('egg-schedule', (info: ScheduleJobInfo) => {
this.#agent.messenger.on('egg-schedule', (info: EggScheduleJobInfo) => {
// get job info from worker
this.#agent.schedule.onJobFinish(info);
});
Expand Down
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
Application, ILifecycleBoot, EggLogger,
} from 'egg';
import { importResolve } from '@eggjs/utils';
import { ScheduleItem, ScheduleJobInfo } from './lib/types.js';
import { EggScheduleItem, EggScheduleJobInfo } from './lib/types.js';

const debug = debuglog('@eggjs/schedule/app');

Expand Down Expand Up @@ -87,7 +87,7 @@ export default class Boot implements ILifecycleBoot {
workerId: process.pid,
rt,
message: e?.message,
} as ScheduleJobInfo);
} as EggScheduleJobInfo);
});

// for test purpose
Expand Down Expand Up @@ -115,7 +115,7 @@ export default class Boot implements ILifecycleBoot {
}

debug('[runSchedule] resolve schedulePath: %o', schedulePath);
let schedule: ScheduleItem;
let schedule: EggScheduleItem;
try {
schedule = scheduleWorker.scheduleItems[schedulePath];
if (!schedule) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/types.js';
10 changes: 5 additions & 5 deletions src/lib/load_schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import assert from 'node:assert';
import { stringify } from 'node:querystring';
import { isClass, isFunction, isGeneratorFunction } from 'is-type-of';
import type { EggApplicationCore, EggContext } from 'egg';
import type { ScheduleConfig, ScheduleTask, ScheduleItem } from './types.js';
import type { EggScheduleConfig, EggScheduleTask, EggScheduleItem } from './types.js';

function getScheduleLoader(app: EggApplicationCore) {
return class ScheduleLoader extends app.loader.FileLoader {
async load() {
const target = this.options.target as Record<string, ScheduleItem>;
const target = this.options.target as Record<string, EggScheduleItem>;
const items = await this.parse();
for (const item of items) {
const schedule = item.exports as { schedule: ScheduleConfig, task: ScheduleTask };
const schedule = item.exports as { schedule: EggScheduleConfig, task: EggScheduleTask };
const fullpath = item.fullpath;
const scheduleConfig = schedule.schedule;
assert(scheduleConfig, `schedule(${fullpath}): must have "schedule" and "task" properties`);
assert(isClass(schedule) || isFunction(schedule.task),
`schedule(${fullpath}: \`schedule.task\` should be function or \`schedule\` should be class`);

let task: ScheduleTask;
let task: EggScheduleTask;
if (isClass(schedule)) {
assert(!isGeneratorFunction(schedule.prototype.subscribe),
`schedule(${fullpath}): "schedule" generator function is not support, should use async function instead`);
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function loadSchedule(app: EggApplicationCore) {
];

const Loader = getScheduleLoader(app);
const schedules = {} as Record<string, ScheduleItem>;
const schedules = {} as Record<string, EggScheduleItem>;
await new Loader({
directory: dirs,
target: schedules,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debuglog } from 'node:util';
import type { Agent, EggLogger } from 'egg';
import { loadSchedule } from './load_schedule.js';
import type { ScheduleItem, ScheduleJobInfo } from './types.js';
import type { EggScheduleItem, EggScheduleJobInfo } from './types.js';
import type { BaseStrategy } from './strategy/base.js';

const debug = debuglog('@eggjs/schedule/lib/schedule');
Expand Down Expand Up @@ -39,7 +39,7 @@ export class Schedule {
}
}

registerSchedule(scheduleItem: ScheduleItem) {
registerSchedule(scheduleItem: EggScheduleItem) {
const { key, schedule } = scheduleItem;
const type = schedule.type;
if (schedule.disable) {
Expand Down Expand Up @@ -70,7 +70,7 @@ export class Schedule {
*
* @param {Object} info - { id, key, success, message, workerId }
*/
onJobFinish(info: ScheduleJobInfo) {
onJobFinish(info: EggScheduleJobInfo) {
this.#logger.debug(`[Job#${info.id}] ${info.key} finish event received by agent from worker#${info.workerId}`);
const instance = this.#strategyInstanceMap.get(info.key);
/* istanbul ignore else */
Expand Down
6 changes: 3 additions & 3 deletions src/lib/schedule_worker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Application } from 'egg';
import { loadSchedule } from './load_schedule.js';
import type { ScheduleItem } from './types.js';
import type { EggScheduleItem } from './types.js';

export class ScheduleWorker {
#app: Application;
scheduleItems: Record<string, ScheduleItem> = {};
scheduleItems: Record<string, EggScheduleItem> = {};

constructor(app: Application) {
this.#app = app;
Expand All @@ -14,7 +14,7 @@ export class ScheduleWorker {
this.scheduleItems = await loadSchedule(this.#app);
}

registerSchedule(scheduleItem: ScheduleItem) {
registerSchedule(scheduleItem: EggScheduleItem) {
this.scheduleItems[scheduleItem.key] = scheduleItem;
}

Expand Down
16 changes: 8 additions & 8 deletions src/lib/strategy/base.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { Agent, EggLogger } from 'egg';
import type { ScheduleConfig, ScheduleJobInfo } from '../types.js';
import type { EggScheduleConfig, EggScheduleJobInfo } from '../types.js';

export class BaseStrategy {
protected agent: Agent;
protected scheduleConfig: ScheduleConfig;
protected scheduleConfig: EggScheduleConfig;
protected key: string;
protected logger: EggLogger;
protected closed = false;
count = 0;

constructor(scheduleConfig: ScheduleConfig, agent: Agent, key: string) {
constructor(scheduleConfig: EggScheduleConfig, agent: Agent, key: string) {
this.agent = agent;
this.key = key;
this.scheduleConfig = scheduleConfig;
this.logger = this.agent.getLogger('scheduleLogger');
}

/** keep compatibility */
get schedule(): ScheduleConfig {
get schedule(): EggScheduleConfig {
return this.scheduleConfig;
}

Expand All @@ -30,10 +30,10 @@ export class BaseStrategy {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onJobStart(_info: ScheduleJobInfo) {}
onJobStart(_info: EggScheduleJobInfo) {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onJobFinish(_info: ScheduleJobInfo) {}
onJobFinish(_info: EggScheduleJobInfo) {}

/**
* trigger one worker
Expand All @@ -53,7 +53,7 @@ export class BaseStrategy {
key: this.key,
id: this.getSeqId(),
args,
} as ScheduleJobInfo;
} as EggScheduleJobInfo;

this.logger.info(`[Job#${info.id}] ${info.key} triggered, send random by agent`);
this.agent.messenger.sendRandom('egg-schedule', info);
Expand All @@ -78,7 +78,7 @@ export class BaseStrategy {
key: this.key,
id: this.getSeqId(),
args,
} as ScheduleJobInfo;
} as EggScheduleJobInfo;
this.logger.info(`[Job#${info.id}] ${info.key} triggered, send all by agent`);
// send to all workers
this.agent.messenger.send('egg-schedule', info);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/strategy/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { ms } from 'humanize-ms';
import safeTimers from 'safe-timers';
import { logDate } from 'utility';
import type { Agent } from 'egg';
import type { ScheduleConfig } from '../types.js';
import type { EggScheduleConfig } from '../types.js';
import { BaseStrategy } from './base.js';

export abstract class TimerStrategy extends BaseStrategy {
protected cronInstance?: CronExpression;

constructor(scheduleConfig: ScheduleConfig, agent: Agent, key: string) {
constructor(scheduleConfig: EggScheduleConfig, agent: Agent, key: string) {
super(scheduleConfig, agent, key);

const { interval, cron, cronOptions, immediate } = this.scheduleConfig;
Expand Down
28 changes: 18 additions & 10 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ScheduleWorker } from './schedule_worker.js';
* Schedule Config
* @see https://www.eggjs.org/zh-CN/basics/schedule
*/
export interface ScheduleConfig {
export interface EggScheduleConfig {
type?: 'worker' | 'all';
interval?: string | number;
cron?: string;
Expand All @@ -16,16 +16,16 @@ export interface ScheduleConfig {
env?: string[];
}

export type ScheduleTask = (ctx: any, ...args: any[]) => Promise<void>;
export type EggScheduleTask = (ctx: any, ...args: any[]) => Promise<void>;

export interface ScheduleItem {
schedule: ScheduleConfig;
export interface EggScheduleItem {
schedule: EggScheduleConfig;
scheduleQueryString: string;
task: ScheduleTask;
task: EggScheduleTask;
key: string;
}

export interface ScheduleJobInfo {
export interface EggScheduleJobInfo {
id: string;
key: string;
workerId: number;
Expand All @@ -36,15 +36,23 @@ export interface ScheduleJobInfo {
}

declare module 'egg' {
export interface ScheduleAgent {
export interface EggScheduleAgent {
schedule: Schedule;
}
export interface Agent extends ScheduleAgent {}
export interface Agent extends EggScheduleAgent {}

export interface ScheduleApplication {
export interface EggScheduleApplication {
scheduleWorker: ScheduleWorker;
/** runSchedule in unittest */
runSchedule: (schedulePath: string, ...args: any[]) => Promise<void>;
}
export interface Application extends ScheduleApplication {}
export interface Application extends EggScheduleApplication {}

export interface EggScheduleAppConfig {
schedule: {
directory: string[];
};
}

export interface EggAppConfig extends EggScheduleAppConfig {}
}
11 changes: 11 additions & 0 deletions test/fixtures/demo/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import '../../../../src/index.js';

import { EggAppConfig } from 'egg';

export default {
schedule: {
directory: [
'path/to/otherSchedule',
],
},
} as Partial<EggAppConfig>;
Empty file added test/fixtures/demo/package.json
Empty file.

0 comments on commit 2c558cb

Please sign in to comment.