From 6d978c5d7c339c78a90b00d2c2622f0be85ab3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Wed, 10 Aug 2022 17:55:41 +0800 Subject: [PATCH] fix: optimize backgroud output (#47) * fix: optimize backgroud output * fix: lint * fix: lint Co-authored-by: wanghx --- plugin/tegg/lib/EggAppLoader.ts | 7 ++++++- plugin/tegg/test/BackgroundTask.test.ts | 15 ++++++++++++++- .../apps/background-app/app/controller/app.ts | 7 +++++++ .../apps/background-app/app/extend/context.ts | 5 +++++ .../fixtures/apps/background-app/app/router.ts | 1 + .../multi-module-background/BackgroundService.ts | 9 +++++++-- 6 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts diff --git a/plugin/tegg/lib/EggAppLoader.ts b/plugin/tegg/lib/EggAppLoader.ts index 41d1f800..1de19ddf 100644 --- a/plugin/tegg/lib/EggAppLoader.ts +++ b/plugin/tegg/lib/EggAppLoader.ts @@ -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, @@ -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, }; diff --git a/plugin/tegg/test/BackgroundTask.test.ts b/plugin/tegg/test/BackgroundTask.test.ts index fd51be84..93966c17 100644 --- a/plugin/tegg/test/BackgroundTask.test.ts +++ b/plugin/tegg/test/BackgroundTask.test.ts @@ -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 () => { @@ -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(); @@ -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 [')); + }); }); diff --git a/plugin/tegg/test/fixtures/apps/background-app/app/controller/app.ts b/plugin/tegg/test/fixtures/apps/background-app/app/controller/app.ts index 8b83978d..5633a020 100644 --- a/plugin/tegg/test/fixtures/apps/background-app/app/controller/app.ts +++ b/plugin/tegg/test/fixtures/apps/background-app/app/controller/app.ts @@ -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'; + } } diff --git a/plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts b/plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts new file mode 100644 index 00000000..d7abb6f6 --- /dev/null +++ b/plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts @@ -0,0 +1,5 @@ +export default { + testObj: { + ok: true, + }, +}; diff --git a/plugin/tegg/test/fixtures/apps/background-app/app/router.ts b/plugin/tegg/test/fixtures/apps/background-app/app/router.ts index 4789fe93..e8b207b7 100644 --- a/plugin/tegg/test/fixtures/apps/background-app/app/router.ts +++ b/plugin/tegg/test/fixtures/apps/background-app/app/router.ts @@ -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); }; diff --git a/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts b/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts index 57ed40a8..a6225f39 100644 --- a/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts +++ b/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts @@ -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, @@ -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; }); }