Skip to content

Commit

Permalink
fix: patch runSchedule on application.unittest.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 17, 2024
1 parent 6b03a9b commit da4e897
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 51 deletions.
52 changes: 1 addition & 51 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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');
}
}
57 changes: 57 additions & 0 deletions src/app/extend/application.unittest.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit da4e897

Please sign in to comment.