implemented syncing config back to the workers

This commit is contained in:
tigerbot 2017-06-09 12:40:39 -06:00
parent cad8dd686e
commit d12c06999e
2 changed files with 47 additions and 13 deletions

View File

@ -137,6 +137,7 @@ function createConfigStorage(args) {
; ;
} }
var tcpProm;
function fillConfig(config, args) { function fillConfig(config, args) {
config.debug = config.debug || args.debug; config.debug = config.debug || args.debug;
@ -203,15 +204,19 @@ function fillConfig(config, args) {
config.tunnel = args.tunnel || config.tunnel; config.tunnel = args.tunnel || config.tunnel;
var tcpProm; if (Array.isArray(config.tcp.bind)) {
if (config.tcp.bind) { return PromiseA.resolve(config);
tcpProm = PromiseA.resolve(); }
} else {
// We need to make sure we only check once, because even though our workers can
// all bind on the same port witout issue we cannot. This will lead to failure
// to determine which ports will work once the first worker starts.
if (!tcpProm) {
tcpProm = new PromiseA(function (resolve, reject) { tcpProm = new PromiseA(function (resolve, reject) {
require('../lib/check-ports').checkTcpPorts(function (failed, bound) { require('../lib/check-ports').checkTcpPorts(function (failed, bound) {
config.tcp.bind = Object.keys(bound); var result = Object.keys(bound).map(Number);
if (config.tcp.bind.length) { if (result.length > 0) {
resolve(); resolve(result);
} else { } else {
reject(failed); reject(failed);
} }
@ -219,9 +224,12 @@ function fillConfig(config, args) {
}); });
} }
return tcpProm return tcpProm.then(
.then(function () { return config; }) function (bound) {
.catch(function (failed) { config.tcp.bind = bound;
return config;
},
function (failed) {
Object.keys(failed).forEach(function (key) { Object.keys(failed).forEach(function (key) {
console.log('[error bind]', key, failed[key].code); console.log('[error bind]', key, failed[key].code);
}); });
@ -243,6 +251,10 @@ function run(args) {
}) })
.then(function (config) { .then(function (config) {
cachedConfig = config; cachedConfig = config;
console.log('changed config', config);
Object.keys(workers).forEach(function (key) {
workers[key].send(cachedConfig);
});
}) })
.catch(function (err) { .catch(function (err) {
console.error('error changing config', err); console.error('error changing config', err);

View File

@ -1,7 +1,25 @@
'use strict'; 'use strict';
// TODO needs some sort of config-sync var config;
process.on('message', function (conf) {
// Everything that uses the config should be reading it when relevant rather than
// just at the beginning, so we keep the reference for the main object and just
// change all of its properties to match the new config.
function update(conf) {
var newKeys = Object.keys(conf);
Object.keys(config).forEach(function (key) {
if (newKeys.indexOf(key) < 0) {
delete config[key];
} else {
config[key] = conf[key];
}
});
console.log('config', JSON.stringify(config));
}
function create(conf) {
config = conf;
var deps = { var deps = {
messenger: process messenger: process
// Note that if a custom createConnections is used it will be called with different // Note that if a custom createConnections is used it will be called with different
@ -22,4 +40,8 @@ process.on('message', function (conf) {
}; };
require('./goldilocks.js').create(deps, conf); require('./goldilocks.js').create(deps, conf);
}); process.removeListener('message', create);
process.on('message', update);
}
process.on('message', create);