diff --git a/bin/goldilocks.js b/bin/goldilocks.js index 8522f81..51d77d2 100755 --- a/bin/goldilocks.js +++ b/bin/goldilocks.js @@ -41,15 +41,12 @@ function createStorage(filename, filetype) { } function read() { - return fs.readFileAsync(filename) - .catch(function (err) { - if (err.code === 'ENOENT') { - return ''; - } - return PromiseA.reject(err); - }) - .then(parse) - ; + return fs.readFileAsync(filename).then(parse).catch(function (err) { + if (err.code === 'ENOENT') { + return ''; + } + return PromiseA.reject(err); + }); } var result = { @@ -253,25 +250,29 @@ function run(args) { var workers = {}; var cachedConfig; + function updateConfig(config) { + fillConfig(config, args).then(function (config) { + cachedConfig = config; + console.log('changed config', config); + Object.keys(workers).forEach(function (key) { + workers[key].send(cachedConfig); + }); + }); + } + + process.on('SIGHUP', function () { + configStorage.read().then(updateConfig).catch(function (err) { + console.error('error updating config after SIGHUP', err); + }); + }); + cluster.on('message', function (worker, message) { if (message.type !== 'com.daplie.goldilocks/config') { return; } - configStorage.save(message.changes) - .then(function (config) { - return fillConfig(config, 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); - }) - ; + configStorage.save(message.changes).then(updateConfig).catch(function (err) { + console.error('error changing config', err); + }); }); cluster.on('online', function (worker) {