Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schedule should execute after app ready #60

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
with:
os: 'ubuntu-latest'
version: '14, 16, 18'
version: '14, 16, 18, 20'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ run/

test/fixtures/symlink/runDir/app/schedule/realFile.js
test/fixtures/symlink/runDir/app/schedule/tsRealFile.ts
package-lock.json
53 changes: 29 additions & 24 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module.exports = app => {
}

// register schedule event
app.messenger.on('egg-schedule', info => {
app.messenger.on('egg-schedule', async info => {
await app.ready();
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

const { id, key } = info;
const schedule = scheduleWorker.scheduleItems[key];

Expand All @@ -43,30 +45,33 @@ module.exports = app => {

const start = Date.now();

// execute
return app.ctxStorage.run(ctx, () => {
return schedule.task(ctx, ...info.args)
.catch(err => {
return is.error(err) ? err : new Error(err);
})
.then(err => {
const success = !is.error(err);
const rt = Date.now() - start;

const msg = `[Job#${id}] ${key} execute ${success ? 'succeed' : 'failed'}, used ${rt}ms.`;
logger[success ? 'info' : 'error'](msg, success ? '' : err);

Object.assign(info, {
success,
workerId: process.pid,
rt,
message: err && err.message,
});

// notify agent job finish
app.messenger.sendToAgent('egg-schedule', info);
});
let success;
let e;
try {
// execute
await app.ctxStorage.run(ctx, async () => {
return await schedule.task(ctx, ...info.args);
});
success = true;
} catch (err) {
success = false;
e = is.error(err) ? err : new Error(err);
}

const rt = Date.now() - start;

const msg = `[Job#${id}] ${key} execute ${success ? 'succeed' : 'failed'}, used ${rt}ms.`;
logger[success ? 'info' : 'error'](msg, success ? '' : e);

Object.assign(info, {
success,
workerId: process.pid,
rt,
message: e && e.message,
});

// notify agent job finish
app.messenger.sendToAgent('egg-schedule', info);
});

// for test purpose
Expand Down
Loading