Skip to content

Commit

Permalink
fix: runInBackground always run after setImmediate (#4296)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed May 12, 2020
1 parent 6992397 commit 64efd07
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ const proto = module.exports = {
const start = Date.now();
/* istanbul ignore next */
const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true);
// use app.toAsyncFunction to support both generator function and async function
return ctx.app.toAsyncFunction(scope)(ctx)
// use setImmediate to ensure all sync logic will run async
return new Promise(resolve => setImmediate(resolve))
// use app.toAsyncFunction to support both generator function and async function
.then(() => ctx.app.toAsyncFunction(scope)(ctx))
.then(() => {
ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
})
Expand Down
17 changes: 14 additions & 3 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ describe('test/app/extend/context.test.js', () => {
.get('/')
.expect(200)
.expect('hello');
await sleep(5000);
await app.backgroundTasksFinished();
await sleep(100);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/background run result file size: \d+/.test(log));
Expand All @@ -270,7 +271,8 @@ describe('test/app/extend/context.test.js', () => {
.get('/custom')
.expect(200)
.expect('hello');
await sleep(5000);
await app.backgroundTasksFinished();
await sleep(100);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/background run result file size: \d+/.test(log));
Expand All @@ -293,7 +295,8 @@ describe('test/app/extend/context.test.js', () => {
.get('/error')
.expect(200)
.expect('hello error');
await sleep(5000);
await app.backgroundTasksFinished();
await sleep(100);
assert(errorHadEmit);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
Expand All @@ -302,6 +305,14 @@ describe('test/app/extend/context.test.js', () => {
/\[egg:background] task:mockError fail \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});

it('should always execute after setImmediate', async () => {
const res = await app.httpRequest()
.get('/sync')
.expect(200);
assert(Number(res.text) < 99);
await app.backgroundTasksFinished();
});
});

describe('ctx.runInBackground(scope) with single process mode', () => {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/apps/ctx-background/app/controller/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = async ctx => {
const start = Date.now();
ctx.runInBackground(async () => {
const start = Date.now();
while(Date.now() - start < 100) {
}
});
ctx.body = Date.now() - start;
};
2 changes: 2 additions & 0 deletions test/fixtures/apps/ctx-background/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ module.exports = app => {
app.get('/custom', app.controller.custom);
app.get('/app_background', app.controller.app);
app.get('/error', app.controller.error);
app.get('/sync', app.controller.sync);

};

0 comments on commit 64efd07

Please sign in to comment.