Skip to content

Commit fbe7dc4

Browse files
committed
ref start
1 parent f7e049c commit fbe7dc4

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

bws.js

+21-37
Original file line numberDiff line numberDiff line change
@@ -57,53 +57,37 @@ function startInstance(cb) {
5757
expressApp.start(config, function(err) {
5858
if (err) {
5959
log.error('Could not start BWS instance', err);
60-
return cb(err);
60+
return;
6161
}
6262

6363
server.listen(port);
64-
return cb();
65-
});
66-
};
67-
6864

69-
var logStart = function(err) {
70-
if (err) {
71-
log.error('Error:' + err);
65+
var instanceInfo = cluster.worker ? ' [Instance:' + cluster.worker.id + ']' : '';
66+
log.info('BWS running ' + instanceInfo);
7267
return;
73-
}
74-
75-
if (cluster.worker)
76-
log.info('BWS Instance ' + cluster.worker.id + ' running');
77-
else
78-
log.info('BWS running');
68+
});
7969
};
8070

71+
if (config.cluster && cluster.isMaster) {
8172

82-
if (config.cluster) {
73+
// Count the machine's CPUs
74+
var instances = config.clusterInstances || require('os').cpus().length;
8375

84-
if (cluster.isMaster) {
76+
log.info('Starting ' + instances + ' instances');
8577

86-
// Count the machine's CPUs
87-
var instances = config.clusterInstances || require('os').cpus().length;
88-
89-
log.info('Starting ' + instances + ' instances on port:' + port);
90-
91-
// Create a worker for each CPU
92-
for (var i = 0; i < instances; i += 1) {
93-
cluster.fork();
94-
}
95-
96-
// Listen for dying workers
97-
cluster.on('exit', function(worker) {
98-
// Replace the dead worker,
99-
log.error('Worker ' + worker.id + ' died :(');
100-
cluster.fork();
101-
});
102-
// Code to run if we're in a worker process
103-
} else {
104-
startInstance(logStart);
78+
// Create a worker for each CPU
79+
for (var i = 0; i < instances; i += 1) {
80+
cluster.fork();
10581
}
82+
83+
// Listen for dying workers
84+
cluster.on('exit', function(worker) {
85+
// Replace the dead worker,
86+
log.error('Worker ' + worker.id + ' died :(');
87+
cluster.fork();
88+
});
89+
// Code to run if we're in a worker process
10690
} else {
107-
log.info('Starting on port: ' + port);
108-
startInstance(logStart);
91+
log.info('Listening on port: ' + port);
92+
startInstance();
10993
};

config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var config = {
33
disableLogs: false,
44
port: 3232,
55

6-
// comment this to use a single process
7-
cluster: true,
6+
// Uncomment to make BWS a forking server
7+
// cluster: true,
88

99
// Uncomment to set the number or process (will use the nr of availalbe CPUs by default)
1010
// clusterInstances: 4,

0 commit comments

Comments
 (0)