Skip to content

Commit

Permalink
Fixed broken cluster mode, now uses v0.6+ cluster and removed Hook.IO…
Browse files Browse the repository at this point in the history
… (need another way of interprocess comms)
  • Loading branch information
cliftonc committed Apr 8, 2012
1 parent b47adc8 commit dcf1d2b
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 330 deletions.
48 changes: 19 additions & 29 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,15 @@ var rootpath = process.cwd() + '/',

// Local App Variables
var path = rootpath,
theme = 'default',
port = process.env.PORT || 3000;
theme = 'default',
port = process.env.PORT || 3000;

/**
* Catch All exception handler
*/
process.on('uncaughtException', function (err) {
console.log('Uncaught exception: ' + err + err.stack);
});


/**
* Placeholder for application
*/
var app, exports;
//process.on('uncaughtException', function (err) {
// console.log('Uncaught exception: ' + err + err.stack);
//});

/**
* App settings and middleware
Expand All @@ -48,6 +42,16 @@ var app, exports;
*/
function bootApplication(next) {

// Create our express instance, export for later reference
var app = express.createServer();
app.path = path;
app.isCluster = false;

// Load configuration
var Config = require(path + "/lib/Config").Config;
app.config = new Config();
app.config.init();

app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.responseTime());
Expand Down Expand Up @@ -110,31 +114,17 @@ function bootApplication(next) {
app.use(translate.translate(app.config.get('i18n:language'), app.config.get('i18n:languages'), app.config.get('i18n:additive')));

// Core calipso router
app.use(calipso.calipsoRouter(next));
app.use(calipso.calipsoRouter(app, function() { next(app) }));

}

/**
* Initial bootstrapping
*/
exports.boot = function (next,cluster) {

//Create our express instance, export for later reference
app = exports.app = express.createServer();
app.path = path;
app.isCluster = cluster;
exports.boot = function (cluster, next) {

// Load configuration
var Config = require(path + "/lib/Config").Config;
app.config = new Config();
app.config.init();

// Load application configuration
// theme = app.config.get('themes:front');
// Bootstrap application
bootApplication(function () {
next(app);
});
bootApplication(next);

};

Expand All @@ -144,7 +134,7 @@ if (!module.parent) {

logo.print();

exports.boot(function (app) {
exports.boot(false, function (app) {

if (app) {
app.listen(port);
Expand Down
6 changes: 3 additions & 3 deletions bin/calipso
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function runLauncher(appLauncher) {
break;
case 'modules':
process.chdir(path);
require(path + '/app').boot(function(app) {
require(path + '/app').boot(false, function(app) {
var modules = require(calipsoPath + '/lib/cli/Modules')
modules.moduleRouter(path,appLauncher.script.params,true,function(err) {
if(err) {
Expand All @@ -98,7 +98,7 @@ function runLauncher(appLauncher) {
break;
case 'themes':
process.chdir(path);
require(path + '/app').boot(function(app) {
require(path + '/app').boot(false, function(app) {
var themes = require(calipsoPath + '/lib/cli/Themes')
themes.themeRouter(path,appLauncher.script.params,true,function(err) {
if(err) {
Expand Down Expand Up @@ -171,7 +171,7 @@ function runServer(port) {

// Ensure we run in the local folder of the application
process.chdir(path);
require(path + '/app').boot(function(app) {
require(path + '/app').boot(false, function(app) {
app.listen(port);
console.log("Calipso server listening on port: ".green + app.address().port.toString().white.bold);
console.log("Calipso configured for ".green + (global.process.env.NODE_ENV || 'development').white.bold + " environment\r\n".green);
Expand Down
130 changes: 130 additions & 0 deletions cluster.js
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;
}
2 changes: 1 addition & 1 deletion lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Config(options) {
Config.prototype.init = function() {
this.nconf = require('nconf');
this.load(function(err) {
console.log(err);
if(err) console.error(err);
});
}

Expand Down
Loading

0 comments on commit dcf1d2b

Please sign in to comment.