From ff7431d5c4ea1e1d40fd7e3656dc5ab52ca55726 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 6 Aug 2018 22:49:56 +0800 Subject: [PATCH] feat: allow runInBackground reuse on plugins (#2872) see https://github.com/eggjs/egg-mock/pull/78 --- app/extend/context.js | 13 +++++++++++-- package.json | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/extend/context.js b/app/extend/context.js index 1bd8ccc912..f966abf11d 100644 --- a/app/extend/context.js +++ b/app/extend/context.js @@ -198,13 +198,22 @@ const proto = module.exports = { * ``` */ runInBackground(scope) { + // try to use custom function name first + /* istanbul ignore next */ + const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true); + scope._name = taskName; + this._runInBackground(scope); + }, + + // let plugins or frameworks to reuse _runInBackground in some cases. + // e.g.: https://github.com/eggjs/egg-mock/pull/78 + _runInBackground(scope) { const ctx = this; const start = Date.now(); - // try to use custom function name first /* istanbul ignore next */ 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) + return ctx.app.toAsyncFunction(scope)(ctx) .then(() => { ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start); }) diff --git a/package.json b/package.json index 5bf5b62e72..ef9aabe867 100644 --- a/package.json +++ b/package.json @@ -94,10 +94,10 @@ ], "scripts": { "lint": "eslint app config lib test *.js", - "test": "npm run lint -- --fix && npm run test-local", + "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", "test-local": "egg-bin test", "cov": "egg-bin cov --timeout 100000", - "ci": "npm run lint && npm run cov", + "ci": "npm run lint && egg-bin pkgfiles --check && npm run cov", "doc-server": "doctools server", "doc-build": "doctools build", "doc-deploy": "doctools deploy",