2017-08-14 07:54:19 +00:00
|
|
|
const cluster = require('cluster');
|
2016-12-09 14:54:58 +00:00
|
|
|
const fs = require('fs');
|
2017-02-25 13:49:44 +00:00
|
|
|
|
2016-12-09 14:54:58 +00:00
|
|
|
/**
|
2017-09-05 06:33:42 +00:00
|
|
|
* get raw config object from json
|
2017-08-09 14:44:24 +00:00
|
|
|
* @param file
|
2017-02-25 13:49:44 +00:00
|
|
|
* @returns {object}
|
2016-12-09 14:54:58 +00:00
|
|
|
*/
|
2017-08-09 14:44:24 +00:00
|
|
|
function obtainConfig(file) {
|
2017-04-15 09:01:34 +00:00
|
|
|
let json;
|
|
|
|
try {
|
2017-09-05 06:33:42 +00:00
|
|
|
const jsonFile = fs.readFileSync(file);
|
|
|
|
json = JSON.parse(jsonFile);
|
2017-04-15 09:01:34 +00:00
|
|
|
} catch (err) {
|
2017-08-09 14:44:24 +00:00
|
|
|
throw Error(`fail to load/parse your '${file}': ${err.message}`);
|
2017-02-25 13:49:44 +00:00
|
|
|
}
|
2017-04-15 09:01:34 +00:00
|
|
|
return json;
|
2016-12-09 14:54:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 07:47:51 +00:00
|
|
|
module.exports = function bootstrap(configPath, {core: {Hub, Config}}) {
|
2017-03-29 02:52:28 +00:00
|
|
|
try {
|
2017-08-09 14:44:24 +00:00
|
|
|
Config.init(obtainConfig(configPath));
|
2017-08-14 09:25:00 +00:00
|
|
|
if (cluster.isMaster && __WORKERS__ > 0) {
|
2017-08-14 07:54:19 +00:00
|
|
|
for (let i = 0; i < __WORKERS__; ++i) {
|
|
|
|
cluster.fork();
|
|
|
|
}
|
|
|
|
console.log(`==> [bootstrap] started ${__WORKERS__} workers`);
|
|
|
|
} else {
|
|
|
|
const hub = new Hub();
|
|
|
|
hub.on('close', () => process.exit(0));
|
|
|
|
hub.run();
|
|
|
|
process.on('SIGINT', () => hub.terminate());
|
2017-04-15 09:01:34 +00:00
|
|
|
}
|
2017-03-29 02:52:28 +00:00
|
|
|
} catch (err) {
|
2017-04-13 08:43:54 +00:00
|
|
|
console.error(err);
|
2017-03-29 02:52:28 +00:00
|
|
|
process.exit(-1);
|
2017-01-02 15:18:26 +00:00
|
|
|
}
|
2016-12-09 14:54:58 +00:00
|
|
|
};
|