From fbe7dc4fa0917430a650054a46c4d72509593afe Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Nov 2016 10:15:46 -0300 Subject: [PATCH] ref start --- bws.js | 58 ++++++++++++++++++++----------------------------------- config.js | 4 ++-- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/bws.js b/bws.js index 7adc32d..db61478 100755 --- a/bws.js +++ b/bws.js @@ -57,53 +57,37 @@ function startInstance(cb) { expressApp.start(config, function(err) { if (err) { log.error('Could not start BWS instance', err); - return cb(err); + return; } server.listen(port); - return cb(); - }); -}; - -var logStart = function(err) { - if (err) { - log.error('Error:' + err); + var instanceInfo = cluster.worker ? ' [Instance:' + cluster.worker.id + ']' : ''; + log.info('BWS running ' + instanceInfo); return; - } - - if (cluster.worker) - log.info('BWS Instance ' + cluster.worker.id + ' running'); - else - log.info('BWS running'); + }); }; +if (config.cluster && cluster.isMaster) { -if (config.cluster) { + // Count the machine's CPUs + var instances = config.clusterInstances || require('os').cpus().length; - if (cluster.isMaster) { + log.info('Starting ' + instances + ' instances'); - // Count the machine's CPUs - var instances = config.clusterInstances || require('os').cpus().length; - - log.info('Starting ' + instances + ' instances on port:' + port); - - // Create a worker for each CPU - for (var i = 0; i < instances; i += 1) { - cluster.fork(); - } - - // Listen for dying workers - cluster.on('exit', function(worker) { - // Replace the dead worker, - log.error('Worker ' + worker.id + ' died :('); - cluster.fork(); - }); - // Code to run if we're in a worker process - } else { - startInstance(logStart); + // Create a worker for each CPU + for (var i = 0; i < instances; i += 1) { + cluster.fork(); } + + // Listen for dying workers + cluster.on('exit', function(worker) { + // Replace the dead worker, + log.error('Worker ' + worker.id + ' died :('); + cluster.fork(); + }); + // Code to run if we're in a worker process } else { - log.info('Starting on port: ' + port); - startInstance(logStart); + log.info('Listening on port: ' + port); + startInstance(); }; diff --git a/config.js b/config.js index 77355bb..6c0bc4e 100644 --- a/config.js +++ b/config.js @@ -3,8 +3,8 @@ var config = { disableLogs: false, port: 3232, - // comment this to use a single process - cluster: true, + // Uncomment to make BWS a forking server + // cluster: true, // Uncomment to set the number or process (will use the nr of availalbe CPUs by default) // clusterInstances: 4,