Skip to content

Commit

Permalink
feat: pick commit from 3.x (#166)
Browse files Browse the repository at this point in the history
* feat: add timing data for loader (#160)
* refactor: add timing to extend (#162)
* fix: Loader can be instantiated without timing (#161)
* fix: convert symbol to string (#163)
* fix: timing can be started multiple times (#164)
  • Loading branch information
popomore committed May 22, 2018
1 parent a43dcb9 commit bb24396
Show file tree
Hide file tree
Showing 42 changed files with 425 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
coverage
benchmark
fixtures
.vscode
.vscode
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ coverage
npm-debug.log
.vscode
.DS_Store
yarn.lock
yarn.lock
test/fixtures/egg/node_modules/egg-core
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: false
language: node_js
node_js:
- '8'
- '9'
- '10'
install:
- npm i npminstall && npminstall
script:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '9'
- nodejs_version: '10'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
39 changes: 25 additions & 14 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const co = require('co');
const BaseContextClass = require('./utils/base_context_class');
const utils = require('./utils');
const Router = require('./utils/router');

const Timing = require('./utils/timing');

const DEPRECATE = Symbol('EggCore#deprecate');
const CLOSESET = Symbol('EggCore#closeSet');
Expand All @@ -19,7 +19,7 @@ const CLOSE_PROMISE = Symbol('EggCore#closePromise');
const ROUTER = Symbol('EggCore#router');
const EGG_LOADER = Symbol.for('egg#loader');
const INIT_READY = Symbol('EggCore#initReady');
const EGG_READY_TIMEOUT_ENV = Symbol('EggCore#eggReadyTimeoutEnv');


class EggCore extends KoaApplication {

Expand All @@ -43,16 +43,17 @@ class EggCore extends KoaApplication {

super();

this.timing = new Timing();

// register a close set
this[CLOSESET] = new Set();
// cache deprecate object by file
this[DEPRECATE] = new Map();

// get app timeout from env or use default timeout 10 second
const eggReadyTimeoutEnv = process.env.EGG_READY_TIMEOUT_ENV;
this[EGG_READY_TIMEOUT_ENV] = Number.parseInt(eggReadyTimeoutEnv || 10000);
this[INIT_READY]();

assert(Number.isInteger(this[EGG_READY_TIMEOUT_ENV]), `process.env.EGG_READY_TIMEOUT_ENV ${eggReadyTimeoutEnv} should be able to parseInt.`);
this.timing.start('Application Start');
this.ready(() => this.timing.end('Application Start'));

/**
* @member {Object} EggCore#options
Expand Down Expand Up @@ -124,8 +125,6 @@ class EggCore extends KoaApplication {
logger: this.console,
serverScope: options.serverScope,
});

this[INIT_READY]();
}

/**
Expand Down Expand Up @@ -216,16 +215,26 @@ class EggCore extends KoaApplication {

// get filename from stack
const name = utils.getCalleeFromStack(true);
const timingkey = 'Before Start in ' + utils.getResolvedFilename(name, this.options.baseDir);

this.timing.start(timingkey);

const done = this.readyCallback(name);

// ensure scope executes after load completed
process.nextTick(() => {
utils.callFn(scope).then(() => done(), done);
utils.callFn(scope).then(() => {
done();
this.timing.end(timingkey);
}, err => {
done(err);
this.timing.end(timingkey);
});
});
}

/**
* Close all, it will close
* Close all, it wisll close
* - callbacks registered by beforeClose
* - emit `close` event
* - remove add listeners
Expand Down Expand Up @@ -281,6 +290,10 @@ class EggCore extends KoaApplication {
* });
*/

// get app timeout from env or use default timeout 10 second
const eggReadyTimeoutEnv = Number.parseInt(process.env.EGG_READY_TIMEOUT_ENV || 10000);
assert(Number.isInteger(eggReadyTimeoutEnv), `process.env.EGG_READY_TIMEOUT_ENV ${process.env.EGG_READY_TIMEOUT_ENV} should be able to parseInt.`);

/**
* If a client starts asynchronously, you can register `readyCallback`,
* then the application will wait for the callback to ready
Expand All @@ -294,15 +307,13 @@ class EggCore extends KoaApplication {
* const done = app.readyCallback('mysql');
* mysql.ready(done);
*/
require('ready-callback')({ timeout: this[EGG_READY_TIMEOUT_ENV] }).mixin(this);
require('ready-callback')({ timeout: eggReadyTimeoutEnv }).mixin(this);

this.on('ready_stat', data => {
this.console.info('[egg:core:ready_stat] end ready task %s, remain %j', data.id, data.remain);
}).on('ready_timeout', id => {
this.console.warn('[egg:core:ready_timeout] %s seconds later %s was still unable to finish.', this[EGG_READY_TIMEOUT_ENV] / 1000, id);
this.console.warn('[egg:core:ready_timeout] %s seconds later %s was still unable to finish.', eggReadyTimeoutEnv / 1000, id);
});

this.ready(() => debug('egg emit ready, application started'));
}

/**
Expand Down
32 changes: 30 additions & 2 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const FileLoader = require('./file_loader');
const ContextLoader = require('./context_loader');
const utility = require('utility');
const utils = require('../utils');
const Timing = require('../utils/timing');

const REQUIRE_COUNT = Symbol('EggLoader#requireCount');


class EggLoader {
Expand All @@ -30,6 +33,8 @@ class EggLoader {
assert(this.options.logger, 'options.logger is required');

this.app = this.options.app;
this.timing = this.app.timing || new Timing();
this[REQUIRE_COUNT] = 0;

/**
* @member {Object} EggLoader#pkg
Expand Down Expand Up @@ -286,10 +291,25 @@ class EggLoader {
return null;
}

const ret = utils.loadFile(filepath);
// function(arg1, args, ...) {}
if (inject.length === 0) inject = [ this.app ];
return isFunction(ret) ? ret(...inject) : ret;

let ret = this.requireFile(filepath);
ret = isFunction(ret) ? ret(...inject) : ret;
return ret;
}

/**
* @param {String} filepath - fullpath
* @return {Object} exports
* @private
*/
requireFile(filepath) {
const timingKey = `Require(${this[REQUIRE_COUNT]++}) ${utils.getResolvedFilename(filepath, this.options.baseDir)}`;
this.timing.start(timingKey);
const ret = utils.loadFile(filepath);
this.timing.end(timingKey);
return ret;
}

/**
Expand Down Expand Up @@ -355,7 +375,11 @@ class EggLoader {
target,
inject: this.app,
}, opt);

const timingKey = `Load "${String(property)}" to Application`;
this.timing.start(timingKey);
new FileLoader(opt).load();
this.timing.end(timingKey);
}

/**
Expand All @@ -371,7 +395,11 @@ class EggLoader {
property,
inject: this.app,
}, opt);

const timingKey = `Load "${String(property)}" to Context`;
this.timing.start(timingKey);
new ContextLoader(opt).load();
this.timing.end(timingKey);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/loader/mixin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const extend = require('extend2');
const assert = require('assert');


const SET_CONFIG_META = Symbol('Loader#setConfigMeta');

module.exports = {
Expand All @@ -18,6 +19,7 @@ module.exports = {
* @since 1.0.0
*/
loadConfig() {
this.timing.start('Load Config');
this.configMeta = {};

const target = {};
Expand Down Expand Up @@ -50,6 +52,7 @@ module.exports = {
target.appMiddleware = target.appMiddlewares = target.middleware || [];

this.config = target;
this.timing.end('Load Config');
},

_preloadAppConfig() {
Expand Down
3 changes: 3 additions & 0 deletions lib/loader/mixin/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const utility = require('utility');
const utils = require('../../utils');
const FULLPATH = require('../file_loader').FULLPATH;


module.exports = {

/**
Expand All @@ -14,6 +15,7 @@ module.exports = {
* @since 1.0.0
*/
loadController(opt) {
this.timing.start('Load Controller');
opt = Object.assign({
caseStyle: 'lower',
directory: path.join(this.options.baseDir, 'app/controller'),
Expand Down Expand Up @@ -46,6 +48,7 @@ module.exports = {

this.loadToApp(controllerBase, 'controller', opt);
this.options.logger.info('[egg:loader] Controller loaded: %s', controllerBase);
this.timing.end('Load Controller');
},

};
Expand Down
5 changes: 5 additions & 0 deletions lib/loader/mixin/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');


module.exports = {

/**
Expand All @@ -21,16 +22,20 @@ module.exports = {
* @since 1.0.0
*/
loadCustomApp() {
this.timing.start('Load app.js');
this.getLoadUnits()
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'app'))));
this.timing.end('Load app.js');
},

/**
* Load agent.js, same as {@link EggLoader#loadCustomApp}
*/
loadCustomAgent() {
this.timing.start('Load agent.js');
this.getLoadUnits()
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'agent'))));
this.timing.end('Load agent.js');
},

};
4 changes: 3 additions & 1 deletion lib/loader/mixin/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = {
* @since 1.0.0
*/
loadExtend(name, proto) {
this.timing.start(`Load extend/${name}.js`);
// All extend files
const filepaths = this.getExtendFilePaths(name);
// if use mm.env and serverEnv is not unittest
Expand All @@ -109,7 +110,7 @@ module.exports = {
deprecate(`app/extend/${name}/index.js is deprecated, use app/extend/${name}.js instead`);
}

const ext = this.loadFile(filepath);
const ext = this.requireFile(filepath);

const properties = Object.getOwnPropertyNames(ext)
.concat(Object.getOwnPropertySymbols(ext));
Expand Down Expand Up @@ -145,5 +146,6 @@ module.exports = {
}
debug('merge %j to %s from %s', Object.keys(ext), name, filepath);
}
this.timing.end(`Load extend/${name}.js`);
},
};
3 changes: 3 additions & 0 deletions lib/loader/mixin/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const debug = require('debug')('egg-core:middleware');
const pathMatching = require('egg-path-matching');
const utils = require('../../utils');


module.exports = {

/**
Expand All @@ -30,6 +31,7 @@ module.exports = {
* @since 1.0.0
*/
loadMiddleware(opt) {
this.timing.start('Load Middleware');
const app = this.app;

// load middleware to app.middleware
Expand Down Expand Up @@ -85,6 +87,7 @@ module.exports = {
}

this.options.logger.info('[egg:loader] Loaded middleware from %j', middlewarePaths);
this.timing.end('Load Middleware');
},

};
Expand Down
5 changes: 5 additions & 0 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const debug = require('debug')('egg-core:plugin');
const sequencify = require('../../utils/sequencify');
const loadFile = require('../../utils').loadFile;


module.exports = {

/**
Expand Down Expand Up @@ -55,6 +56,8 @@ module.exports = {
* @since 1.0.0
*/
loadPlugin() {
this.timing.start('Load Plugin');

// loader plugins from application
const appPlugins = this.readPluginConfigs(path.join(this.options.baseDir, 'config/plugin.default'));
debug('Loaded app plugins: %j', Object.keys(appPlugins));
Expand Down Expand Up @@ -135,6 +138,8 @@ module.exports = {
* @since 1.0.0
*/
this.plugins = enablePlugins;

this.timing.end('Load Plugin');
},

/*
Expand Down
3 changes: 3 additions & 0 deletions lib/loader/mixin/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');


module.exports = {

/**
Expand All @@ -11,7 +12,9 @@ module.exports = {
* @since 1.0.0
*/
loadRouter() {
this.timing.start('Load Router');
// 加载 router.js
this.loadFile(this.resolveModule(path.join(this.options.baseDir, 'app/router')));
this.timing.end('Load Router');
},
};
Loading

0 comments on commit bb24396

Please sign in to comment.