Skip to content

Commit

Permalink
added npm stop to exit cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Williams committed Feb 2, 2015
1 parent 63127b2 commit 1ee9414
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
37 changes: 28 additions & 9 deletions bin/cluster
@@ -1,17 +1,36 @@
var cluster = require('cluster');
var fs = require('fs');

if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
fs.writeFile('./tmp/cluster.pid', process.pid, function (err) {
if (err) {
console.log('Error: unable to create cluster.pid');
process.exit(1);
} else {
console.log('Starting cluster with pid: ' + process.pid);
//ensure workers exit cleanly
process.on('SIGINT', function() {
console.log('Cluster shutting down..');
for (var id in cluster.workers) {
cluster.workers[id].kill();
}
// exit the master process
process.exit(0);
});

// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;

// Listen for dying workers
cluster.on('exit', function () {
cluster.fork();
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}

// Listen for dying workers
cluster.on('exit', function () {
cluster.fork();
});
}
});
} else {
require('./instance');
Expand Down
1 change: 0 additions & 1 deletion bin/instance
Expand Up @@ -65,6 +65,5 @@ db.connect(dbString, function() {

var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
console.log('Ready to receive connections on port: ' + server.address().port);
});
});
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -4,6 +4,7 @@
"private": true,
"scripts": {
"start": "node --stack-size=10000 ./bin/cluster",
"stop": "kill -2 $(cat tmp/cluster.pid)",
"test": "node ./node_modules/jasmine/bin/jasmine.js"
},
"dependencies": {
Expand Down

0 comments on commit 1ee9414

Please sign in to comment.