context.js 635 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. // try to use eggUtils.getCalleeFromStack
  3. // ignore it if egg-core module not found
  4. let eggUtils;
  5. try {
  6. eggUtils = require('egg-core').utils;
  7. if (!eggUtils) {
  8. // try to support egg-core@3
  9. eggUtils = require('egg-core/lib/utils');
  10. }
  11. } catch (_) {
  12. // ignore eggUtils
  13. }
  14. module.exports = {
  15. runInBackground(scope) {
  16. /* istanbul ignore next */
  17. const taskName = scope._name || scope.name || (eggUtils && eggUtils.getCalleeFromStack(true));
  18. if (taskName) {
  19. scope._name = taskName;
  20. }
  21. const promise = this._runInBackground(scope);
  22. this.app._backgroundTasks.push(promise);
  23. },
  24. };