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) {
config.debug = config.debug || args.debug;
@ -203,15 +204,19 @@ function fillConfig(config, args) {
config.tunnel = args.tunnel || config.tunnel;
var tcpProm;
if (config.tcp.bind) {
tcpProm = PromiseA.resolve();
} else {
if (Array.isArray(config.tcp.bind)) {
return PromiseA.resolve(config);
}
// 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) {
require('../lib/check-ports').checkTcpPorts(function (failed, bound) {
config.tcp.bind = Object.keys(bound);
if (config.tcp.bind.length) {
resolve();
var result = Object.keys(bound).map(Number);
if (result.length > 0) {
resolve(result);
} else {
reject(failed);
}
@ -219,9 +224,12 @@ function fillConfig(config, args) {
});
}
return tcpProm
.then(function () { return config; })
.catch(function (failed) {
return tcpProm.then(
function (bound) {
config.tcp.bind = bound;
return config;
},
function (failed) {
Object.keys(failed).forEach(function (key) {
console.log('[error bind]', key, failed[key].code);
});
@ -243,6 +251,10 @@ function run(args) {
})
.then(function (config) {
cachedConfig = config;
console.log('changed config', config);
Object.keys(workers).forEach(function (key) {
workers[key].send(cachedConfig);
});
})
.catch(function (err) {
console.error('error changing config', err);

View File

@ -1,7 +1,25 @@
'use strict';
// TODO needs some sort of config-sync
process.on('message', function (conf) {
var config;
// 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 = {
messenger: process
// 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);
});
process.removeListener('message', create);
process.on('message', update);
}
process.on('message', create);