Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Boot implements ILifecycleBoot {
this.#agent = agent;
}

async didLoad(): Promise<void> {
async configDidLoad(): Promise<void> {
// register built-in strategy
this.#agent.schedule.use('worker', WorkerStrategy);
this.#agent.schedule.use('all', AllStrategy);
Expand All @@ -25,7 +25,7 @@ export default class Boot implements ILifecycleBoot {
// get job info from worker
this.#agent.schedule.onJobFinish(info);
});
debug('didLoad');
debug('configDidLoad');
}

async serverDidReady(): Promise<void> {
Expand Down
56 changes: 3 additions & 53 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 All @@ -16,7 +14,7 @@ export default class Boot implements ILifecycleBoot {
this.#logger = app.getLogger('scheduleLogger');
}

async didLoad(): Promise<void> {
async configDidLoad(): Promise<void> {
const scheduleWorker = this.#app.scheduleWorker;
await scheduleWorker.init();

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');
debug('configDidLoad');
}
}
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;

6 changes: 3 additions & 3 deletions test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ describe('test/schedule.test.ts', () => {

it('should handler error', async () => {
app = mm.cluster({ baseDir: 'customTypeError', workers: 2 });
// app.debug();
app.debug();
await app.ready();
await sleep(1000);
app.expect('code', 1);
await sleep(3000);
// app.expect('code', 1);
app.expect('stderr', /should provide clusterId/);
});
});
Expand Down
Loading