diff --git a/src/app.ts b/src/app.ts index 463347e..dee8f28 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,10 +1,8 @@ import { debuglog } from 'node:util'; -import path from 'node:path'; import type { Application, ILifecycleBoot, EggLogger, } from 'egg'; -import { importResolve } from '@eggjs/utils'; -import { EggScheduleItem, EggScheduleJobInfo } from './lib/types.js'; +import type { EggScheduleJobInfo } from './lib/types.js'; const debug = debuglog('@eggjs/schedule/app'); @@ -89,54 +87,6 @@ export default class Boot implements ILifecycleBoot { message: e?.message, } as EggScheduleJobInfo); }); - - // for test purpose - const config = this.#app.config; - const directory = [ - path.join(config.baseDir, 'app/schedule'), - ...config.schedule.directory, - ]; - const runSchedule = async (schedulePath: string, ...args: any[]) => { - debug('[runSchedule] start schedulePath: %o, args: %o', schedulePath, args); - - // resolve real path - if (path.isAbsolute(schedulePath)) { - schedulePath = importResolve(schedulePath); - } else { - for (const dir of directory) { - const trySchedulePath = path.join(dir, schedulePath); - try { - schedulePath = importResolve(trySchedulePath); - break; - } catch (err) { - debug('[runSchedule] importResolve %o error: %s', trySchedulePath, err); - } - } - } - - debug('[runSchedule] resolve schedulePath: %o', schedulePath); - let schedule: EggScheduleItem; - try { - schedule = scheduleWorker.scheduleItems[schedulePath]; - if (!schedule) { - throw new Error(`Cannot find schedule ${schedulePath}`); - } - } catch (err: any) { - err.message = `[@eggjs/schedule] ${err.message}`; - throw err; - } - - // run with anonymous context - const ctx = this.#app.createAnonymousContext({ - method: 'SCHEDULE', - url: `/__schedule?path=${schedulePath}&${schedule.scheduleQueryString}`, - }); - return await this.#app.ctxStorage.run(ctx, async () => { - return await schedule.task(ctx, ...args); - }); - }; - Reflect.set(this.#app, 'runSchedule', runSchedule); - debug('didLoad'); } } diff --git a/src/app/extend/application.unittest.ts b/src/app/extend/application.unittest.ts new file mode 100644 index 0000000..ca6604c --- /dev/null +++ b/src/app/extend/application.unittest.ts @@ -0,0 +1,57 @@ +import { debuglog } from 'node:util'; +import path from 'node:path'; +import { importResolve } from '@eggjs/utils'; +import type { ScheduleWorker } from '../../lib/schedule_worker.js'; +import type { EggScheduleItem } from '../../lib/types.js'; + +const debug = debuglog('@eggjs/schedule/app'); + +export default { + async runSchedule(schedulePath: string, ...args: any[]) { + debug('[runSchedule] start schedulePath: %o, args: %o', schedulePath, args); + // for test purpose + const config = this.config; + const directory = [ + path.join(config.baseDir, 'app/schedule'), + ...config.schedule.directory, + ]; + + // resolve real path + if (path.isAbsolute(schedulePath)) { + schedulePath = importResolve(schedulePath); + } else { + for (const dir of directory) { + const trySchedulePath = path.join(dir, schedulePath); + try { + schedulePath = importResolve(trySchedulePath); + break; + } catch (err) { + debug('[runSchedule] importResolve %o error: %s', trySchedulePath, err); + } + } + } + + debug('[runSchedule] resolve schedulePath: %o', schedulePath); + const scheduleWorker: ScheduleWorker = this.scheduleWorker; + let schedule: EggScheduleItem; + try { + schedule = scheduleWorker.scheduleItems[schedulePath]; + if (!schedule) { + throw new TypeError(`Cannot find schedule ${schedulePath}`); + } + } catch (err: any) { + err.message = `[@eggjs/schedule] ${err.message}`; + throw err; + } + + // run with anonymous context + const ctx = this.createAnonymousContext({ + method: 'SCHEDULE', + url: `/__schedule?path=${schedulePath}&${schedule.scheduleQueryString}`, + }); + return await this.ctxStorage.run(ctx, async () => { + return await schedule.task(ctx, ...args); + }); + }, +} as any; +