@@ -10,7 +10,7 @@ import utils from './utils/index.js';
1010import type { Fun } from './utils/index.js' ;
1111import type { EggCore } from './egg.js' ;
1212
13- const debug = debuglog ( '@eggjs/core: lifecycle' ) ;
13+ const debug = debuglog ( '@eggjs/core/ lifecycle' ) ;
1414
1515export interface ILifecycleBoot {
1616 // loader auto set 'fullPath' property on boot class
@@ -62,13 +62,15 @@ export interface LifecycleOptions {
6262 logger : EggConsoleLogger ;
6363}
6464
65+ export type FunWithFullPath = Fun & { fullPath ?: string } ;
66+
6567export class Lifecycle extends EventEmitter {
6668 #init: boolean ;
6769 #readyObject: ReadyObject ;
6870 #bootHooks: ( BootImplClass | ILifecycleBoot ) [ ] ;
6971 #boots: ILifecycleBoot [ ] ;
7072 #isClosed: boolean ;
71- #closeFunctionSet: Set < Fun > ;
73+ #closeFunctionSet: Set < FunWithFullPath > ;
7274 loadReady : Ready ;
7375 bootReady : Ready ;
7476 options : LifecycleOptions ;
@@ -96,10 +98,10 @@ export class Lifecycle extends EventEmitter {
9698 this . #initReady( ) ;
9799 this
98100 . on ( 'ready_stat' , data => {
99- this . logger . info ( '[egg: core:ready_stat] end ready task %s, remain %j' , data . id , data . remain ) ;
101+ this . logger . info ( '[@eggjs/ core/lifecycle :ready_stat] end ready task %s, remain %j' , data . id , data . remain ) ;
100102 } )
101103 . on ( 'ready_timeout' , id => {
102- this . logger . warn ( '[egg: core:ready_timeout] %s seconds later %s was still unable to finish.' , this . readyTimeout / 1000 , id ) ;
104+ this . logger . warn ( '[@eggjs/ core/lifecycle :ready_timeout] %s seconds later %s was still unable to finish.' , this . readyTimeout / 1000 , id ) ;
103105 } ) ;
104106
105107 this . ready ( err => {
@@ -149,37 +151,47 @@ export class Lifecycle extends EventEmitter {
149151 this . #bootHooks. push ( bootHootOrBootClass ) ;
150152 }
151153
152- addFunctionAsBootHook < T = EggCore > ( hook : ( app : T ) => void ) {
154+ addFunctionAsBootHook < T = EggCore > ( hook : ( app : T ) => void , fullPath ?: string ) {
153155 assert ( this . #init === false , 'do not add hook when lifecycle has been initialized' ) ;
154156 // app.js is exported as a function
155157 // call this function in configDidLoad
156- this . #bootHooks. push ( class Boot implements ILifecycleBoot {
158+ class Boot implements ILifecycleBoot {
159+ static fullPath ?: string ;
157160 app : T ;
158161 constructor ( app : T ) {
159162 this . app = app ;
160163 }
161164 configDidLoad ( ) {
162165 hook ( this . app ) ;
163166 }
164- } ) ;
167+ }
168+ Boot . fullPath = fullPath ;
169+ this . #bootHooks. push ( Boot ) ;
165170 }
166171
167172 /**
168173 * init boots and trigger config did config
169174 */
170175 init ( ) {
176+ debug ( '%s init lifecycle' , this . app . type ) ;
171177 assert ( this . #init === false , 'lifecycle have been init' ) ;
172178 this . #init = true ;
173179 this . #boots = this . #bootHooks. map ( BootHootOrBootClass => {
180+ let instance = BootHootOrBootClass as ILifecycleBoot ;
174181 if ( isClass ( BootHootOrBootClass ) ) {
175- return new BootHootOrBootClass ( this . app ) ;
182+ instance = new BootHootOrBootClass ( this . app ) ;
183+ if ( ! instance . fullPath && 'fullPath' in BootHootOrBootClass ) {
184+ instance . fullPath = BootHootOrBootClass . fullPath as string ;
185+ }
176186 }
177- return BootHootOrBootClass ;
187+ debug ( '[init] add boot instance: %o' , instance . fullPath ) ;
188+ return instance ;
178189 } ) ;
179190 }
180191
181192 registerBeforeStart ( scope : Fun , name : string ) {
182- debug ( 'add registerBeforeStart, name: %o' , name ) ;
193+ debug ( '%s add registerBeforeStart, name: %o' ,
194+ this . options . app . type , name ) ;
183195 this . #registerReadyCallback( {
184196 scope,
185197 ready : this . loadReady ,
@@ -188,16 +200,24 @@ export class Lifecycle extends EventEmitter {
188200 } ) ;
189201 }
190202
191- registerBeforeClose ( fn : Fun ) {
203+ registerBeforeClose ( fn : FunWithFullPath , fullPath ?: string ) {
192204 assert ( typeof fn === 'function' , 'argument should be function' ) ;
193205 assert ( this . #isClosed === false , 'app has been closed' ) ;
206+ if ( fullPath ) {
207+ fn . fullPath = fullPath ;
208+ }
194209 this . #closeFunctionSet. add ( fn ) ;
210+ debug ( '%s register beforeClose at %o, count: %d' ,
211+ this . app . type , fullPath , this . #closeFunctionSet. size ) ;
195212 }
196213
197214 async close ( ) {
198215 // close in reverse order: first created, last closed
199216 const closeFns = Array . from ( this . #closeFunctionSet) ;
217+ debug ( '%s start trigger %d beforeClose functions' ,
218+ this . app . type , closeFns . length ) ;
200219 for ( const fn of closeFns . reverse ( ) ) {
220+ debug ( '%s trigger beforeClose at %o' , this . app . type , fn . fullPath ) ;
201221 await utils . callFn ( fn ) ;
202222 this . #closeFunctionSet. delete ( fn ) ;
203223 }
@@ -206,12 +226,14 @@ export class Lifecycle extends EventEmitter {
206226 this . removeAllListeners ( ) ;
207227 this . app . removeAllListeners ( ) ;
208228 this . #isClosed = true ;
229+ debug ( '%s closed' , this . app . type ) ;
209230 }
210231
211232 triggerConfigWillLoad ( ) {
212233 debug ( 'trigger configWillLoad start' ) ;
213234 for ( const boot of this . #boots) {
214235 if ( typeof boot . configWillLoad === 'function' ) {
236+ debug ( 'trigger configWillLoad at %o' , boot . fullPath ) ;
215237 boot . configWillLoad ( ) ;
216238 }
217239 }
@@ -223,12 +245,13 @@ export class Lifecycle extends EventEmitter {
223245 debug ( 'trigger configDidLoad start' ) ;
224246 for ( const boot of this . #boots) {
225247 if ( typeof boot . configDidLoad === 'function' ) {
248+ debug ( 'trigger configDidLoad at %o' , boot . fullPath ) ;
226249 boot . configDidLoad ( ) ;
227250 }
228251 // function boot hook register after configDidLoad trigger
229252 if ( typeof boot . beforeClose === 'function' ) {
230253 const beforeClose = boot . beforeClose . bind ( boot ) ;
231- this . registerBeforeClose ( beforeClose ) ;
254+ this . registerBeforeClose ( beforeClose , boot . fullPath ) ;
232255 }
233256 }
234257 debug ( 'trigger configDidLoad end' ) ;
@@ -274,10 +297,12 @@ export class Lifecycle extends EventEmitter {
274297 return ( async ( ) => {
275298 for ( const boot of this . #boots) {
276299 if ( typeof boot . didReady === 'function' ) {
300+ debug ( 'trigger didReady at %o' , boot . fullPath ) ;
277301 try {
278302 await boot . didReady ( err ) ;
279- } catch ( e ) {
280- this . emit ( 'error' , e ) ;
303+ } catch ( err ) {
304+ debug ( 'trigger didReady error at %o, error: %s' , boot . fullPath , err ) ;
305+ this . emit ( 'error' , err ) ;
281306 }
282307 }
283308 }
@@ -292,9 +317,11 @@ export class Lifecycle extends EventEmitter {
292317 if ( typeof boot . serverDidReady !== 'function' ) {
293318 continue ;
294319 }
320+ debug ( 'trigger serverDidReady at %o' , boot . fullPath ) ;
295321 try {
296322 await boot . serverDidReady ( ) ;
297323 } catch ( err ) {
324+ debug ( 'trigger serverDidReady error at %o, error: %s' , boot . fullPath , err ) ;
298325 this . emit ( 'error' , err ) ;
299326 }
300327 }
0 commit comments