Skip to content

Commit 0b1eee0

Browse files
authored
feat: add backgroundTask.timeout config (#101)
1 parent 5033b51 commit 0b1eee0

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,8 @@ export default class BackgroundService {
632632

633633
- **推荐方式:将异步任务转发给单例对象(SingletonProto)来执行,单例对象永远不会释放**
634634
- 调整超时时间,对 `backgroundTaskHelper.timeout` 进行赋值即可
635+
- 如果将超时时间设置为 `Infinity`,框架将不会超时
636+
- 可以在 config 文件中指定 `backgroundTask.timeout` 来全局覆盖默认的超时时间
635637

636638
### 动态注入
637639

core/background-task/src/BackgroundTaskHelper.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
22
import { AccessLevel, ContextProto, Inject } from '@eggjs/core-decorator';
3-
import type { EggLogger } from 'egg';
3+
import type { EggLogger, EggAppConfig } from 'egg';
44
import { EggObjectLifecycle } from '@eggjs/tegg-lifecycle';
55
import { ContextHandler, EggContextLifecycleUtil } from '@eggjs/tegg-runtime';
66

@@ -14,6 +14,9 @@ export class BackgroundTaskHelper implements EggObjectLifecycle {
1414
// default timeout for async task
1515
timeout = 5000;
1616

17+
@Inject()
18+
config: EggAppConfig;
19+
1720
private backgroundTasks: Array<Promise<void>> = [];
1821

1922
async init() {
@@ -24,6 +27,9 @@ export class BackgroundTaskHelper implements EggObjectLifecycle {
2427
await this.doPreDestroy();
2528
},
2629
});
30+
if (this.config.backgroundTask?.timeout) {
31+
this.timeout = this.config.backgroundTask.timeout;
32+
}
2733
}
2834

2935
run(fn: () => Promise<void>) {
@@ -52,18 +58,21 @@ export class BackgroundTaskHelper implements EggObjectLifecycle {
5258
async doPreDestroy(): Promise<void> {
5359
// quick quit
5460
if (!this.backgroundTasks.length) return;
55-
56-
const { promise: timeout, resolve } = this.sleep();
5761
const backgroundTasks = this.backgroundTasks.slice();
62+
if (this.timeout <= 0 || this.timeout === Infinity) {
63+
await Promise.all(backgroundTasks);
64+
} else {
65+
const { promise: timeout, resolve } = this.sleep();
5866

59-
await Promise.race([
60-
// not block the pre destroy process too long
61-
timeout,
62-
// ensure all background task are done before destroy the context
63-
Promise.all(backgroundTasks),
64-
]);
65-
// always resolve the sleep promise
66-
resolve();
67+
await Promise.race([
68+
// not block the pre destroy process too long
69+
timeout,
70+
// ensure all background task are done before destroy the context
71+
Promise.all(backgroundTasks),
72+
]);
73+
// always resolve the sleep promise
74+
resolve();
75+
}
6776
if (this.backgroundTasks.length !== backgroundTasks.length) {
6877
this.backgroundTasks = this.backgroundTasks.slice(backgroundTasks.length);
6978
return this.doPreDestroy();

plugin/tegg/test/BackgroundTask.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,11 @@ describe('test/BackgroundTask.test.ts', () => {
6767
const lifecycleList = EggContextLifecycleUtil.getObjectLifecycleList(teggCtx!);
6868
assert(lifecycleList.length === 0);
6969
});
70+
71+
it('config should work', async () => {
72+
await app.mockModuleContextScope(async ctx => {
73+
const backgroundTaskHelper = await ctx.getEggObject(BackgroundTaskHelper);
74+
assert(backgroundTaskHelper.timeout === Infinity);
75+
});
76+
});
7077
});

plugin/tegg/test/fixtures/apps/background-app/config/config.default.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = function(appInfo) {
1515
ignoreJSON: false,
1616
}
1717
},
18+
backgroundTask: {
19+
timeout: Infinity,
20+
},
1821
};
1922
return config;
2023
};

plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class BackgroundService {
2323
private readonly countService: CountService;
2424

2525
async backgroundAdd(delay = 1000) {
26+
this.backgroundTaskHelper.timeout = 5000;
2627
this.backgroundTaskHelper.run(async () => {
2728
await sleep(delay);
2829
assert(this.testObj.ok);

0 commit comments

Comments
 (0)