blinksocks/bin/bootstrap.js

39 lines
930 B
JavaScript
Raw Normal View History

const cluster = require('cluster');
2016-12-09 14:54:58 +00:00
const fs = require('fs');
2016-12-09 14:54:58 +00:00
/**
2017-09-05 06:33:42 +00:00
* get raw config object from json
* @param file
* @returns {object}
2016-12-09 14:54:58 +00:00
*/
function obtainConfig(file) {
let json;
try {
2017-09-05 06:33:42 +00:00
const jsonFile = fs.readFileSync(file);
json = JSON.parse(jsonFile);
} catch (err) {
throw Error(`fail to load/parse your '${file}': ${err.message}`);
}
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}}) {
try {
Config.init(obtainConfig(configPath));
if (cluster.isMaster && __WORKERS__ > 0) {
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());
}
} catch (err) {
console.error(err);
process.exit(-1);
2017-01-02 15:18:26 +00:00
}
2016-12-09 14:54:58 +00:00
};