Skip to content

Commit

Permalink
User can pass monitor app (in addition to the actual app) so that he …
Browse files Browse the repository at this point in the history
…can add other interesting monitoring functionalities
  • Loading branch information
prabhakhar committed Aug 24, 2012
1 parent 553be7c commit 5e49e3f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
13 changes: 12 additions & 1 deletion examples/express/express-server.js
Expand Up @@ -22,13 +22,22 @@ 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) {
req.connection.end();
}
});

monApp.get('/monapp', function(req, res) {
res.send('Hello from Monitor app');
if(!serving) {
req.connection.end();
}
});

app.on('close', function() {
serving = false;
})
Expand All @@ -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);
});
10 changes: 5 additions & 5 deletions lib/index.js
Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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];
Expand All @@ -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);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/monitor.js
Expand Up @@ -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');
Expand Down
20 changes: 16 additions & 4 deletions lib/process.js
Expand Up @@ -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();
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
Expand Down

0 comments on commit 5e49e3f

Please sign in to comment.