Skip to content

Commit

Permalink
feat: allow runInBackground reuse on plugins (#2872)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Aug 6, 2018
1 parent 033fe0c commit ff7431d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ff7431d

Please sign in to comment.