Skip to content

Commit

Permalink
feat: runInBackground use location as scope name when anonymous (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed Nov 20, 2017
1 parent 212b077 commit 5513456
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const delegate = require('delegates');
const { assign } = require('utility');
const eggUtils = require('egg-core').utils;

const HELPER = Symbol('Context#helper');
const LOCALS = Symbol('Context#locals');
Expand All @@ -10,6 +11,7 @@ const COOKIES = Symbol('Context#cookies');
const CONTEXT_LOGGERS = Symbol('Context#logger');
const CONTEXT_HTTPCLIENT = Symbol('Context#httpclient');


const proto = module.exports = {
get cookies() {
if (!this[COOKIES]) {
Expand Down Expand Up @@ -195,7 +197,7 @@ const proto = module.exports = {
const ctx = this;
const start = Date.now();
/* istanbul ignore next */
const taskName = scope.name || '-';
const taskName = scope.name || scope._name || eggUtils.getCalleeFromStack(true);
// use app.toAsyncFunction to support both generator function and async function
ctx.app.toAsyncFunction(scope)(ctx)
.then(() => {
Expand Down
2 changes: 2 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;
const cluster = require('cluster-client');
const { assign } = require('utility');
const eggUtils = require('egg-core').utils;

const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
Expand Down Expand Up @@ -161,6 +162,7 @@ class Application extends EggApplication {
*/
runInBackground(scope) {
const ctx = this.createAnonymousContext();
if (!scope.name) scope._name = eggUtils.getCalleeFromStack(true);
ctx.runInBackground(scope);
}

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
"dependencies": {
"@types/accepts": "^1.3.3",
"@types/koa": "^2.0.40",
"@types/koa-router": "^7.0.24",
"@types/koa-router": "^7.0.25",
"accepts": "^1.3.4",
"agentkeepalive": "^3.3.0",
"cluster-client": "^1.7.1",
"debug": "^3.1.0",
"delegates": "^1.0.0",
"egg-cluster": "^1.12.4",
"egg-cluster": "^1.12.5",
"egg-cookies": "^2.2.1",
"egg-core": "^4.0.0",
"egg-core": "^4.1.0",
"egg-development": "^2.0.0",
"egg-i18n": "^2.0.0",
"egg-jsonp": "^2.0.0",
"egg-logger": "^1.6.0",
"egg-logrotator": "^3.0.0",
"egg-multipart": "^2.0.0",
"egg-onerror": "^2.0.0",
"egg-schedule": "^3.0.0",
"egg-schedule": "^3.1.0",
"egg-security": "^2.0.0",
"egg-session": "^3.0.0",
"egg-static": "^2.0.0",
Expand All @@ -52,7 +52,7 @@
},
"devDependencies": {
"address": "^1.0.3",
"autod": "^3.0.0",
"autod": "^3.0.1",
"autod-egg": "^1.0.0",
"coffee": "^4.1.0",
"egg-alinode": "^1.0.3",
Expand Down
3 changes: 2 additions & 1 deletion test/app/extend/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ describe('test/app/extend/application.test.js', () => {
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/mock background run at app result file size: \d+/.test(log));
assert(/mock background run at app anonymous result file size: \d+/.test(log));
assert(
/\[egg:background] task:saveUserInfo success \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
/\[egg:background] task:.*?app[\/\\]controller[\/\\]app\.js:\d+:\d+ success \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});
});
Expand Down
4 changes: 4 additions & 0 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,13 @@ describe('test/app/extend/context.test.js', () => {
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));
assert(/background run anonymous result file size: \d+/.test(log));
assert(
/\[egg:background] task:saveUserInfo success \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
assert(
/\[egg:background] task:.*?app[\/\\]controller[\/\\]home\.js:\d+:\d+ success \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});

it('should run background task error', function* () {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/apps/ctx-background/app/controller/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ module.exports = function* () {
const buf = yield fs.readFile(__filename);
ctx.logger.warn('mock background run at app result file size: %s', buf.length);
});

this.app.runInBackground(async ctx => {
const buf = await fs.readFile(__filename);
ctx.logger.warn('mock background run at app anonymous result file size: %s', buf.length);
});
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/ctx-background/app/controller/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ module.exports = function* () {
const buf = yield fs.readFile(__filename);
ctx.logger.warn('background run result file size: %s', buf.length);
});
this.runInBackground(async ctx => {
const buf = await fs.readFile(__filename);
ctx.logger.warn('mock background run anonymous result file size: %s', buf.length);
});
};

0 comments on commit 5513456

Please sign in to comment.