Skip to content

Commit 6d978c5

Browse files
whxaxeswanghx
andauthored
fix: optimize backgroud output (#47)
* fix: optimize backgroud output * fix: lint * fix: lint Co-authored-by: wanghx <whx89768@antgroup.com>
1 parent 41b67b2 commit 6d978c5

6 files changed

Lines changed: 40 additions & 4 deletions

File tree

plugin/tegg/lib/EggAppLoader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Application } from 'egg';
2-
import { Loader } from '@eggjs/tegg-metadata';
2+
import { Loader, TeggError } from '@eggjs/tegg-metadata';
33
import {
44
AccessLevel,
55
EggProtoImplClass, InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute,
@@ -46,6 +46,11 @@ export class EggAppLoader implements Loader {
4646
private buildCtxClazz(name: string): EggProtoImplClass {
4747
const temp = {
4848
[name]: function(ctx) {
49+
if (!ctx) {
50+
// ctx has been destroyed, throw humanize error info
51+
throw TeggError.create(`Can not read property \`${name}\` because egg ctx has been destroyed`, 'read_after_ctx_destroyed');
52+
}
53+
4954
return ctx[name];
5055
} as any,
5156
};

plugin/tegg/test/BackgroundTask.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import assert from 'assert';
33
import path from 'path';
44
import { CountService } from './fixtures/apps/background-app/modules/multi-module-background/CountService';
55
import sleep from 'mz-modules/sleep';
6+
import fs from 'fs';
67

78
describe('test/BackgroundTask.test.ts', () => {
9+
const appDir = path.join(__dirname, 'fixtures/apps/background-app');
810
let app;
911

1012
after(async () => {
@@ -21,7 +23,7 @@ describe('test/BackgroundTask.test.ts', () => {
2123
return path.join(__dirname, '..');
2224
});
2325
app = mm.app({
24-
baseDir: path.join(__dirname, 'fixtures/apps/background-app'),
26+
baseDir: appDir,
2527
framework: require.resolve('egg'),
2628
});
2729
await app.ready();
@@ -38,4 +40,15 @@ describe('test/BackgroundTask.test.ts', () => {
3840
await sleep(1000);
3941
assert(countService.count === 1);
4042
});
43+
44+
it('background timeout with humanize error info', async () => {
45+
app.mockCsrf();
46+
await app.httpRequest()
47+
.get('/backgroudTimeout')
48+
.expect(200);
49+
50+
await sleep(7000);
51+
const errorLog = fs.readFileSync(path.resolve(appDir, 'logs/egg-app/common-error.log'), 'utf-8');
52+
assert(errorLog.includes('Can not read property `testObj` because egg ctx has been destroyed ['));
53+
});
4154
});

plugin/tegg/test/fixtures/apps/background-app/app/controller/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ export default class App extends Controller {
88
this.ctx.status = 200;
99
this.ctx.body = 'done';
1010
}
11+
12+
async backgroudTimeout() {
13+
const backgroundService = await this.ctx.getEggObject(BackgroundService);
14+
await backgroundService.backgroundAdd(6000);
15+
this.ctx.status = 200;
16+
this.ctx.body = 'done';
17+
}
1118
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
testObj: {
3+
ok: true,
4+
},
5+
};

plugin/tegg/test/fixtures/apps/background-app/app/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { Application } from 'egg';
22

33
module.exports = (app: Application) => {
44
app.router.get('/background', app.controller.app.background);
5+
app.router.get('/backgroudTimeout', app.controller.app.backgroudTimeout);
56
};

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AccessLevel, ContextProto, Inject } from '@eggjs/tegg';
22
import { BackgroundTaskHelper } from '@eggjs/tegg-background-task';
33
import { CountService } from './CountService';
44
import sleep from 'mz-modules/sleep';
5+
import assert from 'assert';
56

67
@ContextProto({
78
accessLevel: AccessLevel.PUBLIC,
@@ -10,12 +11,16 @@ export default class BackgroundService {
1011
@Inject()
1112
private readonly backgroundTaskHelper:BackgroundTaskHelper;
1213

14+
@Inject()
15+
testObj: any;
16+
1317
@Inject()
1418
private readonly countService: CountService;
1519

16-
async backgroundAdd() {
20+
async backgroundAdd(delay = 1000) {
1721
this.backgroundTaskHelper.run(async () => {
18-
await sleep(1000);
22+
await sleep(delay);
23+
assert(this.testObj.ok);
1924
this.countService.count += 1;
2025
});
2126
}

0 commit comments

Comments
 (0)