-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: lifecycle base class #3581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const BaseHookClass = require('./lib/core/base_hook_class'); | ||
|
||
class EggAgentHook extends BaseHookClass { | ||
configDidLoad() { | ||
this.agent._wrapMessenger(); | ||
} | ||
} | ||
|
||
module.exports = EggAgentHook; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const INSTANCE = Symbol('BaseHookClass#instance'); | ||
|
||
class BaseHookClass { | ||
|
||
constructor(instance) { | ||
this[INSTANCE] = instance; | ||
} | ||
|
||
get logger() { | ||
return this[INSTANCE].logger; | ||
} | ||
|
||
get config() { | ||
return this[INSTANCE].config; | ||
} | ||
|
||
get app() { | ||
assert(this[INSTANCE].type === 'application', 'agent boot should not use app instance'); | ||
return this[INSTANCE]; | ||
} | ||
|
||
get agent() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这样实现的话,this.app 和 this.agent 都可以取到值?是不是可以根据 worker.type 判断一下,避免用户误用? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接在 constructor 里面根据类型来赋值吧?而不是 assert 报错。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 根据类型来赋值,开发者取到 undefined,有个断言提醒会不会好一点? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 也行 |
||
assert(this[INSTANCE].type === 'agent', 'app boot should not use agent instance'); | ||
return this[INSTANCE]; | ||
} | ||
} | ||
|
||
module.exports = BaseHookClass; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,7 @@ | |
"lib", | ||
"app", | ||
"config", | ||
"agent.js", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const mock = require('egg-mock'); | ||
const path = require('path'); | ||
const utils = require('./utils'); | ||
|
||
describe('test/agent.test.js', () => { | ||
afterEach(mock.restore); | ||
let app; | ||
|
||
before(() => { | ||
app = utils.app('apps/agent-logger-config'); | ||
return app.ready(); | ||
}); | ||
after(() => app.close()); | ||
|
||
it('agent logger config should work', () => { | ||
const fileTransport = app._agent.logger.get('file'); | ||
assert(fileTransport.options.file === path.join('/tmp/foo', 'egg-agent.log')); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
logger: { | ||
dir: '/tmp/foo', | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "agent-logger-config" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const sleep = require('mz-modules/sleep'); | ||
const BaseHookClass = require('../../../../lib/core/base_hook_class'); | ||
|
||
module.exports = class { | ||
constructor(app) { | ||
app.bootLog = []; | ||
this.app = app; | ||
module.exports = class extends BaseHookClass { | ||
atian25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constructor(agent) { | ||
super(agent); | ||
agent.bootLog = []; | ||
assert(this.config); | ||
} | ||
|
||
configDidLoad() { | ||
this.app.bootLog.push('configDidLoad'); | ||
this.agent.bootLog.push('configDidLoad'); | ||
} | ||
|
||
async didLoad() { | ||
await sleep(1); | ||
this.app.bootLog.push('didLoad'); | ||
this.agent.bootLog.push('didLoad'); | ||
} | ||
|
||
async willReady() { | ||
await sleep(1); | ||
this.app.bootLog.push('willReady'); | ||
this.agent.bootLog.push('willReady'); | ||
} | ||
|
||
async didReady() { | ||
await sleep(1); | ||
this.app.bootLog.push('didReady'); | ||
this.agent.bootLog.push('didReady'); | ||
this.logger.info('agent is ready'); | ||
} | ||
|
||
async beforeClose() { | ||
await sleep(1); | ||
this.app.bootLog.push('beforeClose'); | ||
this.agent.bootLog.push('beforeClose'); | ||
} | ||
|
||
async serverDidReady() { | ||
await sleep(1); | ||
this.app.bootLog.push('serverDidReady'); | ||
this.agent.bootLog.push('serverDidReady'); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@killagu https://github.com/linxuanwei/agent-test
插件里面挂了