forked from cliftonc/calipso
-
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 broken cluster mode, now uses v0.6+ cluster and removed Hook.IO…
… (need another way of interprocess comms)
- Loading branch information
Showing
19 changed files
with
367 additions
and
330 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
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Master server process | ||
* Initialises a number of instances of the app, based on the number of CPU's it detects | ||
* is available. | ||
* | ||
* First arg is the port | ||
* Second arg is the num worker threads; | ||
* | ||
* e.g. node server 3000 8 | ||
* | ||
*/ | ||
// Dependencies | ||
var rootpath = process.cwd() + '/', | ||
cluster = require('cluster'), | ||
path = require('path'), | ||
logo = require(path.join(rootpath, 'logo')), | ||
colors = require('colors'), | ||
port = process.env.PORT || 3000, | ||
restarts = 0, | ||
totalWorkers = 0, | ||
runningWorkers = 0; | ||
|
||
var argv = processArgs(); | ||
|
||
launchServer(); | ||
|
||
/** | ||
* Launch server instance, initially master, then each worker instance is forked. | ||
* All instances share same config. | ||
*/ | ||
function launchServer() { | ||
|
||
// Check if we are the master process | ||
if (cluster.isMaster) { | ||
|
||
//require('./app').boot(function (app) { | ||
|
||
|
||
// Load configuration | ||
var Config = require(rootpath + "lib/Config").Config, | ||
config = new Config(); | ||
config.init(); | ||
|
||
// Print the logo | ||
logo.print(); | ||
|
||
// Set the number of workers | ||
totalWorkers = config.get('server:cluster:workers') || argv.c; | ||
|
||
// Fork workers based on num cpus | ||
for (var i = 0; i < totalWorkers; i++) { | ||
forkWorker(); | ||
} | ||
|
||
// Log worker death | ||
// TODO : Auto restart with number of retries | ||
cluster.on('death', function(worker) { | ||
|
||
console.error('worker ' + worker.pid + ' died ...'); | ||
|
||
// Manage restarting of workers | ||
if(config.get('server:cluster:restartWorkers')) { | ||
if(restarts > config.get('server:cluster:maximumRestarts')) { | ||
console.error('Maximum number of restarts reached, not restarting this worker.'.red); | ||
} else { | ||
restarts++; | ||
forkWorker(); | ||
} | ||
} | ||
|
||
}); | ||
|
||
//}); | ||
|
||
} else { | ||
|
||
// We are a child worker, so bootstrap the app. | ||
require(rootpath + 'app').boot(true, function (app) { | ||
|
||
//logger.info("Worker [" + argv.m.cyan + "] with pid " + (process.pid + "").grey + " online."); | ||
app.listen(port); | ||
|
||
process.send({ cmd: 'workerStarted', pid: process.pid, port: port }); | ||
|
||
}); | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Helper function to fork a worker, we need to reset the counter in the master thread | ||
* hence the messaging back, also deal with messaging around job management from worker threads. | ||
*/ | ||
function forkWorker() { | ||
|
||
var worker = cluster.fork(); | ||
|
||
worker.on('message', function(msg) { | ||
|
||
if (msg.cmd) { | ||
|
||
if(msg.cmd == 'workerStarted') { | ||
|
||
runningWorkers++; | ||
|
||
if(runningWorkers === totalWorkers) { | ||
console.log("Calipso configured for: ".green + (global.process.env.NODE_ENV || 'development') + " environment.".green); | ||
console.log("Calipso server running ".green + runningWorkers + " workers, listening on port: ".green + port); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
/** | ||
* Process command line arguments using optimist | ||
*/ | ||
function processArgs() { | ||
return require('optimist') | ||
.usage('Launch Calipso in Clustered Mode\nUsage: $0') | ||
.describe('c', 'Number of CPUs') | ||
.alias('c', 'cpu') | ||
.default('c', require('os').cpus().length) | ||
.argv; | ||
} |
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
Oops, something went wrong.