-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed express through throng and using the cluster library directly
- Loading branch information
Showing
2 changed files
with
58 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,31 @@ | ||
var express = require('express'); | ||
var cluster = require('cluster'); | ||
var os = require('os');//no need to download anything | ||
|
||
var PORT = process.env.PORT || 8001; | ||
if (cluster.isMaster) { | ||
|
||
if(cluster.isMaster) { | ||
var numWorkers = os.cpus().length; | ||
console.log('Master cluster setting up ' + numWorkers + ' workers...'); | ||
var cpuCount = require('os').cpus().length; | ||
|
||
for(var i = 0; i < numWorkers; i++) { | ||
cluster.fork(); | ||
} | ||
for (var i = 0; i < cpuCount; i += 1) { | ||
cluster.fork(); | ||
} | ||
|
||
cluster.on('online', function(worker) { | ||
console.log('Worker ' + worker.process.pid + ' is online'); | ||
}); | ||
cluster.on('exit', function (worker) { | ||
console.log('Worker %d died :(', worker.id); | ||
cluster.fork(); | ||
}); | ||
|
||
cluster.on('exit', function(worker, code, signal) { | ||
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal); | ||
console.log('Starting a new worker'); | ||
cluster.fork(); | ||
}); | ||
cluster.on('online', function(worker) { | ||
console.log('Worker ' + worker.process.pid + ' is online'); | ||
}); | ||
} else { | ||
var app = require('express')(); | ||
|
||
app.listen(PORT, function() { | ||
console.log('Express server listening on %d, in %s mode', PORT, app.get('env')); | ||
}); | ||
var express = require('express'); | ||
var app = express(); | ||
|
||
app.get('/', function (request, response) { | ||
console.log('Request to worker %d', cluster.worker.id); | ||
response.send('Worker ' + cluster.worker.id); | ||
}); | ||
|
||
// // Start server | ||
// server.listen(config.port, config.ip, function () { | ||
// console.log('Express server listening on %d, in %s mode', config.port, app.get('env')); | ||
// }); | ||
app.listen(3131); | ||
console.log('Worker %d running!', cluster.worker.id); | ||
} |