@@ -10,7 +10,7 @@ const co = require('co');
1010const BaseContextClass = require ( './utils/base_context_class' ) ;
1111const utils = require ( './utils' ) ;
1212const Router = require ( './utils/router' ) ;
13-
13+ const Timing = require ( './utils/timing' ) ;
1414
1515const DEPRECATE = Symbol ( 'EggCore#deprecate' ) ;
1616const CLOSESET = Symbol ( 'EggCore#closeSet' ) ;
@@ -19,7 +19,7 @@ const CLOSE_PROMISE = Symbol('EggCore#closePromise');
1919const ROUTER = Symbol ( 'EggCore#router' ) ;
2020const EGG_LOADER = Symbol . for ( 'egg#loader' ) ;
2121const INIT_READY = Symbol ( 'EggCore#initReady' ) ;
22- const EGG_READY_TIMEOUT_ENV = Symbol ( 'EggCore#eggReadyTimeoutEnv' ) ;
22+
2323
2424class EggCore extends KoaApplication {
2525
@@ -43,16 +43,17 @@ class EggCore extends KoaApplication {
4343
4444 super ( ) ;
4545
46+ this . timing = new Timing ( ) ;
47+
4648 // register a close set
4749 this [ CLOSESET ] = new Set ( ) ;
4850 // cache deprecate object by file
4951 this [ DEPRECATE ] = new Map ( ) ;
5052
51- // get app timeout from env or use default timeout 10 second
52- const eggReadyTimeoutEnv = process . env . EGG_READY_TIMEOUT_ENV ;
53- this [ EGG_READY_TIMEOUT_ENV ] = Number . parseInt ( eggReadyTimeoutEnv || 10000 ) ;
53+ this [ INIT_READY ] ( ) ;
5454
55- assert ( Number . isInteger ( this [ EGG_READY_TIMEOUT_ENV ] ) , `process.env.EGG_READY_TIMEOUT_ENV ${ eggReadyTimeoutEnv } should be able to parseInt.` ) ;
55+ this . timing . start ( 'Application Start' ) ;
56+ this . ready ( ( ) => this . timing . end ( 'Application Start' ) ) ;
5657
5758 /**
5859 * @member {Object} EggCore#options
@@ -124,8 +125,6 @@ class EggCore extends KoaApplication {
124125 logger : this . console ,
125126 serverScope : options . serverScope ,
126127 } ) ;
127-
128- this [ INIT_READY ] ( ) ;
129128 }
130129
131130 /**
@@ -216,16 +215,26 @@ class EggCore extends KoaApplication {
216215
217216 // get filename from stack
218217 const name = utils . getCalleeFromStack ( true ) ;
218+ const timingkey = 'Before Start in ' + utils . getResolvedFilename ( name , this . options . baseDir ) ;
219+
220+ this . timing . start ( timingkey ) ;
221+
219222 const done = this . readyCallback ( name ) ;
220223
221224 // ensure scope executes after load completed
222225 process . nextTick ( ( ) => {
223- utils . callFn ( scope ) . then ( ( ) => done ( ) , done ) ;
226+ utils . callFn ( scope ) . then ( ( ) => {
227+ done ( ) ;
228+ this . timing . end ( timingkey ) ;
229+ } , err => {
230+ done ( err ) ;
231+ this . timing . end ( timingkey ) ;
232+ } ) ;
224233 } ) ;
225234 }
226235
227236 /**
228- * Close all, it will close
237+ * Close all, it wisll close
229238 * - callbacks registered by beforeClose
230239 * - emit `close` event
231240 * - remove add listeners
@@ -281,6 +290,10 @@ class EggCore extends KoaApplication {
281290 * });
282291 */
283292
293+ // get app timeout from env or use default timeout 10 second
294+ const eggReadyTimeoutEnv = Number . parseInt ( process . env . EGG_READY_TIMEOUT_ENV || 10000 ) ;
295+ assert ( Number . isInteger ( eggReadyTimeoutEnv ) , `process.env.EGG_READY_TIMEOUT_ENV ${ process . env . EGG_READY_TIMEOUT_ENV } should be able to parseInt.` ) ;
296+
284297 /**
285298 * If a client starts asynchronously, you can register `readyCallback`,
286299 * then the application will wait for the callback to ready
@@ -294,15 +307,13 @@ class EggCore extends KoaApplication {
294307 * const done = app.readyCallback('mysql');
295308 * mysql.ready(done);
296309 */
297- require ( 'ready-callback' ) ( { timeout : this [ EGG_READY_TIMEOUT_ENV ] } ) . mixin ( this ) ;
310+ require ( 'ready-callback' ) ( { timeout : eggReadyTimeoutEnv } ) . mixin ( this ) ;
298311
299312 this . on ( 'ready_stat' , data => {
300313 this . console . info ( '[egg:core:ready_stat] end ready task %s, remain %j' , data . id , data . remain ) ;
301314 } ) . on ( 'ready_timeout' , id => {
302- this . console . warn ( '[egg:core:ready_timeout] %s seconds later %s was still unable to finish.' , this [ EGG_READY_TIMEOUT_ENV ] / 1000 , id ) ;
315+ this . console . warn ( '[egg:core:ready_timeout] %s seconds later %s was still unable to finish.' , eggReadyTimeoutEnv / 1000 , id ) ;
303316 } ) ;
304-
305- this . ready ( ( ) => debug ( 'egg emit ready, application started' ) ) ;
306317 }
307318
308319 /**
0 commit comments