Skip to content

Commit 16fda00

Browse files
committed
fix: wait egg background task done before destroy tegg ctx
1 parent 6b31708 commit 16fda00

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

plugin/tegg/app.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import './lib/AppLoadUnit';
22
import './lib/AppLoadUnitInstance';
33
import './lib/EggCompatibleObject';
4-
import { Application } from 'egg';
4+
import { Application, Context } from 'egg';
5+
import { BackgroundTaskHelper, PrototypeUtil } from '@eggjs/tegg';
6+
import { EggPrototype } from '@eggjs/tegg-metadata';
57
import { EggContextCompatibleHook } from './lib/EggContextCompatibleHook';
68
import { CompatibleUtil } from './lib/CompatibleUtil';
79
import { ModuleHandler } from './lib/ModuleHandler';
@@ -28,6 +30,29 @@ export default class App {
2830
}
2931

3032
async didLoad() {
33+
const eggRunInBackground = this.app.context.runInBackground;
34+
this.app.context.runInBackground = function runInBackground(this: Context, scope: (ctx: Context) => Promise<any>) {
35+
let resolveBackgroundTask;
36+
const backgroundTaskPromise = new Promise(resolve => {
37+
resolveBackgroundTask = resolve;
38+
});
39+
const newScope = async () => {
40+
try {
41+
await scope(this);
42+
} finally {
43+
resolveBackgroundTask();
44+
}
45+
};
46+
Reflect.apply(eggRunInBackground, this, [ newScope ]);
47+
48+
const proto = PrototypeUtil.getClazzProto(BackgroundTaskHelper);
49+
const eggObject = this.app.eggContainerFactory.getEggObject(proto as EggPrototype);
50+
const backgroundTaskHelper = eggObject.obj as BackgroundTaskHelper;
51+
backgroundTaskHelper.run(async () => {
52+
await backgroundTaskPromise;
53+
});
54+
};
55+
3156
await this.app.moduleHandler.ready();
3257
this.compatibleHook = new EggContextCompatibleHook(this.app.moduleHandler);
3358
this.app.eggContextLifecycleUtil.registerLifecycle(this.compatibleHook);

plugin/tegg/lib/EggContextCompatibleHook.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LifecycleHook, ObjectInitType } from '@eggjs/tegg';
1+
import { BackgroundTaskHelper, LifecycleHook, ObjectInitType, PrototypeUtil } from '@eggjs/tegg';
22
import { EggContainerFactory, EggContext, EggContextLifecycleContext } from '@eggjs/tegg-runtime';
33
import { EggPrototype } from '@eggjs/tegg-metadata';
44
import { ModuleHandler } from './ModuleHandler';
@@ -38,6 +38,13 @@ export class EggContextCompatibleHook implements LifecycleHook<EggContextLifecyc
3838
await Promise.all(this.initProtoList.map(async proto => {
3939
await EggContainerFactory.getOrCreateEggObject(proto);
4040
}));
41+
} else {
42+
// Use for ctx.runInBackground.
43+
// BackgroundTaskHelper should get by sync,
44+
// or tegg context may be destroyed before background task run.
45+
// So create it in preCreate.
46+
const protoObj = PrototypeUtil.getClazzProto(BackgroundTaskHelper);
47+
await EggContainerFactory.getOrCreateEggObject(protoObj as EggPrototype);
4148
}
4249
}
4350
}

plugin/tegg/test/app/extend/context.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'assert';
22
import path from 'path';
33
import mm from 'egg-mock';
44
import { Application } from 'egg';
5+
import sleep from 'mz-modules/sleep';
56
import AppService from '../../fixtures/apps/egg-app/modules/multi-module-service/AppService';
67
import PersistenceService from '../../fixtures/apps/egg-app/modules/multi-module-repo/PersistenceService';
78

@@ -50,4 +51,17 @@ describe('test/app/extend/context.test.ts', () => {
5051
});
5152
});
5253
});
54+
55+
describe('runInBackground', () => {
56+
it('should notify background task helper', async () => {
57+
let backgroundIsDone = false;
58+
await app.mockModuleContextScope(async ctx => {
59+
ctx.runInBackground(async () => {
60+
await sleep(100);
61+
backgroundIsDone = true;
62+
});
63+
});
64+
assert(backgroundIsDone);
65+
});
66+
});
5367
});

0 commit comments

Comments
 (0)