diff --git a/examples/express/express-server.js b/examples/express/express-server.js index 3364783..d8c4007 100644 --- a/examples/express/express-server.js +++ b/examples/express/express-server.js @@ -22,6 +22,8 @@ var Cluster = require('../../lib/index.js'), var serving = true; var app = express.createServer(); +var monApp = express.createServer(); + app.get('/', function(req, res) { res.send('hello'); if(!serving) { @@ -29,6 +31,13 @@ app.get('/', function(req, res) { } }); +monApp.get('/monapp', function(req, res) { + res.send('Hello from Monitor app'); + if(!serving) { + req.connection.end(); + } +}); + app.on('close', function() { serving = false; }) @@ -55,5 +64,7 @@ c.on('forked', function(pid) { }); c.listen(function(cb) { - cb(app); + // You need to pass the app. monApp is optional. + // If monApp is not passed, cluster2 creates one for you. + cb(app, monApp); }); diff --git a/lib/index.js b/lib/index.js index e14274b..4f46c01 100644 --- a/lib/index.js +++ b/lib/index.js @@ -77,15 +77,15 @@ Cluster.prototype.listen = function(createApp, cb) { master.shutdown(); } else { - initApp(function (app) { - master.listen(app, function () { + initApp(function (app, monApp) { + master.listen(app, monApp, function () { if(self.options.ecv) { ecv.enable(app, self.options, self, function (data) { return true; }); } if(cb) { - cb(app); + cb(app, monApp); } }); }); @@ -113,7 +113,7 @@ Cluster.prototype.listen = function(createApp, cb) { } function initApp(cb) { - createApp.call(null, function (app) { + createApp.call(null, function (app, monApp) { // If the port is already occupied, this will exit to prevent node workers from multiple // masters hanging around together var ports = _.isArray(self.options.port) ? self.options.port : [self.options.port]; @@ -123,7 +123,7 @@ Cluster.prototype.listen = function(createApp, cb) { // TODO: cb should be called after all the "busy-port" tests finish. Currently working // as time spent before starting the server is greater the tests' time. Ideally, cb should // be called within the callback. - cb(app); + cb(app, monApp); }); } diff --git a/lib/monitor.js b/lib/monitor.js index a5b926f..8057c80 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -28,7 +28,7 @@ var Monitor = module.exports = function Monitor(options) { this.options = options || {port: 8081, stats: {}, path: '/'}; this.stats = this.options.stats; - var app = express.createServer(); + var app = options.monitor || express.createServer(); var self = this; app.set('views', __dirname + '/../public/views'); diff --git a/lib/process.js b/lib/process.js index 8f7fb7d..57aee87 100644 --- a/lib/process.js +++ b/lib/process.js @@ -157,8 +157,18 @@ var Process = module.exports = function Process(options) { } } -Process.prototype.listen = function(app, cb) { - var self = this; +Process.prototype.listen = function() { + var self = this, app, monApp, cb; + + if(arguments.length === 3) { + app = arguments[0]; + monApp = arguments[1]; + cb = arguments[2]; + } + else if (arguments.length === 2) { + app = arguments[0]; + cb = arguments[1]; + } if(cluster.isMaster) { this.stats.pid = process.pid; this.stats.start = new Date(); @@ -168,10 +178,12 @@ Process.prototype.listen = function(app, cb) { // Monitor to serve log files and other stats - typically on an internal port var monitor = new Monitor({ + monitor: monApp, stats: self.stats, port: self.options.monPort, - path: self.options.monPath} - ); + path: self.options.monPath + }); + monitor.on('listening', function() { misc.ensureDir(process.cwd() + '/pids', true); // Ensure pids dir misc.ensureDir(process.cwd() + '/logs'); // Ensure logs dir diff --git a/package.json b/package.json index 8c6e168..f66d86b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "email": "subbu@ebaysf.com" }], "name": "cluster2", - "version": "0.3.4", + "version": "0.3.5", "repository": { "type": "git", "url": "https://github.com/ql-io/cluster2"