From 5513456e2c702fdc1b7a500f8d8d58048d1041fa Mon Sep 17 00:00:00 2001 From: Yiyu He Date: Mon, 20 Nov 2017 14:20:17 +0800 Subject: [PATCH] feat: runInBackground use location as scope name when anonymous (#1683) --- app/extend/context.js | 4 +++- lib/application.js | 2 ++ package.json | 10 +++++----- test/app/extend/application.test.js | 3 ++- test/app/extend/context.test.js | 4 ++++ .../fixtures/apps/ctx-background/app/controller/app.js | 5 +++++ .../apps/ctx-background/app/controller/home.js | 4 ++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/extend/context.js b/app/extend/context.js index 1597e842bf..5a82c4585b 100644 --- a/app/extend/context.js +++ b/app/extend/context.js @@ -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'); @@ -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]) { @@ -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(() => { diff --git a/lib/application.js b/lib/application.js index b58304dd31..59d46b045a 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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'); @@ -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); } diff --git a/package.json b/package.json index 802b4ca0c1..1b610f987e 100644 --- a/package.json +++ b/package.json @@ -15,15 +15,15 @@ "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", @@ -31,7 +31,7 @@ "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", @@ -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", diff --git a/test/app/extend/application.test.js b/test/app/extend/application.test.js index 9c79eb3b10..870c14ac0f 100644 --- a/test/app/extend/application.test.js +++ b/test/app/extend/application.test.js @@ -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')) ); }); }); diff --git a/test/app/extend/context.test.js b/test/app/extend/context.test.js index 57005a9c3c..b66ba43d16 100644 --- a/test/app/extend/context.test.js +++ b/test/app/extend/context.test.js @@ -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* () { diff --git a/test/fixtures/apps/ctx-background/app/controller/app.js b/test/fixtures/apps/ctx-background/app/controller/app.js index 9ae45184b4..ffedc8c4de 100644 --- a/test/fixtures/apps/ctx-background/app/controller/app.js +++ b/test/fixtures/apps/ctx-background/app/controller/app.js @@ -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); + }); }; diff --git a/test/fixtures/apps/ctx-background/app/controller/home.js b/test/fixtures/apps/ctx-background/app/controller/home.js index 6855af7126..f9b35f0d1b 100644 --- a/test/fixtures/apps/ctx-background/app/controller/home.js +++ b/test/fixtures/apps/ctx-background/app/controller/home.js @@ -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); + }); };