Package Exports
- @eggjs/tegg-background-task
- @eggjs/tegg-background-task/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@eggjs/tegg-background-task) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@eggjs/tegg-background-task
install
npm i --save @eggjs/tegg-background-taskUsage
import { BackgroundTaskHelper } from '@eggjs/tegg-background-task';
@ContextProto()
export default class BackgroundService {
@Inject()
private readonly backgroundTaskHelper:BackgroundTaskHelper
async backgroundAdd() {
this.backgroundTaskHelper.run(async () => {
// do the background task
});
}
}Background
tegg release the request context after request is done. So use the process.nextTick, setTimeout, setInterval in request is not safe.
Please use the backgroundTaskHelper, the release process will wait all the background tasks are done.
Timeout
The release process will wait tasks done, but not forever. The default timeout is 5s, if task will cost more than 5s, two ways to resolve
- use the
SingletonPrototo do the work,SingletonProtonever release - set longer timeout to
backgroundTaskHelper.timeout