diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 22a8662..f38bb73 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -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' diff --git a/.gitignore b/.gitignore index 8ae5946..8448054 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ run/ test/fixtures/symlink/runDir/app/schedule/realFile.js test/fixtures/symlink/runDir/app/schedule/tsRealFile.ts +package-lock.json diff --git a/app.js b/app.js index 8a1f1b7..aadf4e3 100644 --- a/app.js +++ b/app.js @@ -16,10 +16,11 @@ module.exports = app => { } // register schedule event - app.messenger.on('egg-schedule', info => { + app.messenger.on('egg-schedule', async info => { const { id, key } = info; + logger.debug(`[Job#${id}] ${key} await app ready`); + await app.ready(); const schedule = scheduleWorker.scheduleItems[key]; - logger.debug(`[Job#${id}] ${key} task received by app`); if (!schedule) { @@ -43,30 +44,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