Skip to content

Commit

Permalink
fix: optimize backgroud output (#47)
Browse files Browse the repository at this point in the history
* fix: optimize backgroud output

* fix: lint

* fix: lint

Co-authored-by: wanghx <[email protected]>
  • Loading branch information
whxaxes and wanghx authored Aug 10, 2022
1 parent 41b67b2 commit 6d978c5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
7 changes: 6 additions & 1 deletion plugin/tegg/lib/EggAppLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application } from 'egg';
import { Loader } from '@eggjs/tegg-metadata';
import { Loader, TeggError } from '@eggjs/tegg-metadata';
import {
AccessLevel,
EggProtoImplClass, InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute,
Expand Down Expand Up @@ -46,6 +46,11 @@ export class EggAppLoader implements Loader {
private buildCtxClazz(name: string): EggProtoImplClass {
const temp = {
[name]: function(ctx) {
if (!ctx) {
// ctx has been destroyed, throw humanize error info
throw TeggError.create(`Can not read property \`${name}\` because egg ctx has been destroyed`, 'read_after_ctx_destroyed');
}

return ctx[name];
} as any,
};
Expand Down
15 changes: 14 additions & 1 deletion plugin/tegg/test/BackgroundTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import assert from 'assert';
import path from 'path';
import { CountService } from './fixtures/apps/background-app/modules/multi-module-background/CountService';
import sleep from 'mz-modules/sleep';
import fs from 'fs';

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

after(async () => {
Expand All @@ -21,7 +23,7 @@ describe('test/BackgroundTask.test.ts', () => {
return path.join(__dirname, '..');
});
app = mm.app({
baseDir: path.join(__dirname, 'fixtures/apps/background-app'),
baseDir: appDir,
framework: require.resolve('egg'),
});
await app.ready();
Expand All @@ -38,4 +40,15 @@ describe('test/BackgroundTask.test.ts', () => {
await sleep(1000);
assert(countService.count === 1);
});

it('background timeout with humanize error info', async () => {
app.mockCsrf();
await app.httpRequest()
.get('/backgroudTimeout')
.expect(200);

await sleep(7000);
const errorLog = fs.readFileSync(path.resolve(appDir, 'logs/egg-app/common-error.log'), 'utf-8');
assert(errorLog.includes('Can not read property `testObj` because egg ctx has been destroyed ['));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export default class App extends Controller {
this.ctx.status = 200;
this.ctx.body = 'done';
}

async backgroudTimeout() {
const backgroundService = await this.ctx.getEggObject(BackgroundService);
await backgroundService.backgroundAdd(6000);
this.ctx.status = 200;
this.ctx.body = 'done';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
testObj: {
ok: true,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Application } from 'egg';

module.exports = (app: Application) => {
app.router.get('/background', app.controller.app.background);
app.router.get('/backgroudTimeout', app.controller.app.backgroudTimeout);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AccessLevel, ContextProto, Inject } from '@eggjs/tegg';
import { BackgroundTaskHelper } from '@eggjs/tegg-background-task';
import { CountService } from './CountService';
import sleep from 'mz-modules/sleep';
import assert from 'assert';

@ContextProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -10,12 +11,16 @@ export default class BackgroundService {
@Inject()
private readonly backgroundTaskHelper:BackgroundTaskHelper;

@Inject()
testObj: any;

@Inject()
private readonly countService: CountService;

async backgroundAdd() {
async backgroundAdd(delay = 1000) {
this.backgroundTaskHelper.run(async () => {
await sleep(1000);
await sleep(delay);
assert(this.testObj.ok);
this.countService.count += 1;
});
}
Expand Down

0 comments on commit 6d978c5

Please sign in to comment.