Comparing version 0.4.0 to 0.4.1
@@ -103,59 +103,28 @@ /* | ||
app.get('/admin/v3console/ValidateInternals', function(req, res){ | ||
//this will render the validate internal homepage | ||
//including links to logs & cals | ||
//and also link to component status | ||
}); | ||
app.get('/ComponentStatus(/[^/]+)?', function(req, res){ | ||
app.get('/admin/v3console/ComponentStatus(/[^/]+)?', function(req, res){ | ||
//when component = empty, this will list all the component ids and links associated with them. | ||
//otherwise, we'll collect the component status from all workers | ||
var component = res.query["component"], //should come from param | ||
view = res.query["view"] || "html",//should come from param | ||
workers = self.stats.workers || [];//should come from master | ||
var component = req.query.component, //should come from param | ||
view = req.query.view || 'html',//should come from param | ||
worker = req.query.worker, | ||
componentStatus = require("./component-status.js").componentStatus; | ||
var promises = _.map(workers, function(worker){ | ||
var defer = Q.defer(); | ||
worker.send("component", { | ||
component: component, | ||
view: view, | ||
query: res.query | ||
}); | ||
var timeOut = setTimeout(function(){ | ||
defer.reject(new Error("time out")); | ||
}); | ||
var handler = function(){ | ||
if(_.isEqual(message.type, "component-response") && _.isEqual(message.component, component)){ | ||
clearTimeout(timeOut); | ||
worker.removeListener("message", handler); | ||
defer.resolve(message.view); | ||
if(component){ | ||
componentStatus.getStatus(component, { | ||
'params': req.query, | ||
'worker': worker, | ||
'done': function(result){ | ||
res.send(JSON.stringify(result), 200); | ||
} | ||
}; | ||
worker.on("message", handler); | ||
return defer.promise; | ||
}); | ||
Q.allResolved(promises, function(resolves){ | ||
//monitor app's logic is to merge the view | ||
res.writeHead(200, { | ||
'Content-Type': _.isEqual(view, "json") ? 'application/json' : "text/html" | ||
}); | ||
res.send("view"); | ||
res.end(); | ||
}); | ||
} | ||
else{ | ||
res.send(JSON.stringify(componentStatus.getComponents()), 200); | ||
} | ||
}); | ||
return app; | ||
} | ||
}; | ||
@@ -162,0 +131,0 @@ function getStats(master, socket) { |
@@ -32,3 +32,3 @@ /* | ||
if(debug) { | ||
console.log.apply(null, (arguments || []).join('')); | ||
console.log(JSON.stringify(arguments)); | ||
} | ||
@@ -216,3 +216,3 @@ } | ||
_.each(_.compact(targets), function(target){ | ||
var targetWorker = workers[target]; | ||
var targetWorker = self.workers[target]; | ||
if(targetWorker){ | ||
@@ -228,2 +228,3 @@ targetWorker.send(message); | ||
timeOut = setTimeout(function(){ | ||
log('[cluster] reject timeout:' + JSON.stringify(message)); | ||
deferred.reject(new Error("timeout")); | ||
@@ -240,2 +241,3 @@ }, message.timeOut || 10000); | ||
.fail(function(error){ | ||
log('[cluster] fail error:' + error); | ||
message.error = error; | ||
@@ -276,4 +278,9 @@ send(message); | ||
this.notifyWorkers = function(message) { | ||
_.each(self.workers, function(worker) { | ||
worker.send(message) | ||
_.each(self.workers, function(worker, pid) { | ||
try{ | ||
worker.send(message); | ||
} | ||
catch(error){ | ||
log('[cluster2] cannot send message to worker:' + pid); | ||
} | ||
}); | ||
@@ -284,2 +291,14 @@ } | ||
Process.prototype.listen = function() { | ||
process.on('uncaughtException', function(err) { | ||
// handle the error safely | ||
log('[fatal] ' + err); | ||
}); | ||
var exit = process.exit; | ||
process.exit = function(){ | ||
log('[cluster2] exit unexpectedly' + new Error().stack); | ||
exit.apply(process, arguments); | ||
}; | ||
var self = this, apps, monApp, cb; | ||
@@ -303,2 +322,13 @@ | ||
//before monitor app starts | ||
process.cluster = { | ||
clustered: true, | ||
emitter: self.emitter, | ||
workers: self.workers | ||
}; | ||
//register the master worker itself | ||
require('./component-status.js').componentStatus.register('worker', function(){ | ||
return 'm' + process.pid; | ||
}, 'array'); | ||
// Monitor to serve log files and other stats - typically on an internal port | ||
@@ -313,3 +343,3 @@ var monitor = new Monitor({ | ||
monitor.on('listening', function() { | ||
monitor.once('listening', function() { | ||
misc.ensureDir(process.cwd() + '/pids', true); // Ensure pids dir | ||
@@ -328,2 +358,4 @@ misc.ensureDir(process.cwd() + '/logs'); // Ensure logs dir | ||
var deathWatcher = function (worker, code, signal) { | ||
worker = worker.process; | ||
log('[cluster2] death watch activated, worker:' + worker.pid + '\tcode:' + code + '\tsignal:' + signal + '\texit:' + worker.exitCode); | ||
if(code === 0) { | ||
@@ -333,2 +365,3 @@ self.stats.noWorkers--; | ||
} | ||
self.emitter.emit('died', worker.pid); | ||
@@ -342,2 +375,4 @@ self.stats.workersKilled++; | ||
delete self.stats.workers[worker.pid]; | ||
log('[cluster2] updated worker list:' + _.keys(self.workers)); | ||
}; | ||
@@ -367,3 +402,3 @@ cluster.on('exit', deathWatcher); | ||
app.app.on('connection', function(conn) { | ||
console.log('master conn listener'); | ||
log('master conn listener'); | ||
}); | ||
@@ -382,3 +417,6 @@ }); | ||
var monHost = this.options.monHost || '0.0.0.0'; | ||
monitor.listen(this.options.monPort, monHost); | ||
monitor.listen(this.options.monPort, monHost).once("listening", function(){ | ||
//redundant for express2, absolutely needed for express3 and above | ||
monitor.emit("listening"); | ||
}); | ||
} | ||
@@ -438,3 +476,3 @@ else { | ||
_.each(apps, function(app){ | ||
app.app.on('listening', function() { | ||
app.app.once('listening', function() { | ||
app.listening = true; | ||
@@ -446,2 +484,6 @@ process.send({ | ||
}); | ||
require('./component-status.js').componentStatus.register('worker', function(){ | ||
return process.pid; | ||
}, 'array'); | ||
}); | ||
@@ -452,5 +494,9 @@ | ||
var host = self.options.host ? self.options.host : '0.0.0.0'; | ||
_.each(ports, function(port) { | ||
app.app.listen(port, host, function() { | ||
log('Worker ', process.pid, ' listening on ', port); | ||
var servers = _.map(ports, function(port) { | ||
log('Worker ', process.pid, ' listening to ', port); | ||
return app.app.listen(port, host); | ||
}); | ||
_.each(servers, function(server){ | ||
server.once("listening", function() { | ||
if(self.options.ecv) { | ||
@@ -462,6 +508,7 @@ ecv.enable(apps, self.options, self.emitter, function(data) { | ||
cb(); | ||
//redundant for express2, absolutely needed for express3 and above | ||
app.app.emit('listening'); | ||
}); | ||
}) | ||
app.app.on('connection', setTimeout); | ||
server.on('connection', setTimeout); | ||
}); | ||
}); | ||
@@ -473,2 +520,3 @@ | ||
if(totalConns > threshold) { | ||
log('[cluster2] exit because of connection threshold:' + threshold + ':' + totalConns); | ||
clearInterval(recycle); | ||
@@ -475,0 +523,0 @@ _.each(apps, function(app){ |
@@ -8,3 +8,3 @@ { | ||
"name": "cluster2", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"repository": { | ||
@@ -29,3 +29,5 @@ "type": "git", | ||
"nodeunit": "", | ||
"request": "" | ||
"request": "", | ||
"mocha": "", | ||
"should": "" | ||
}, | ||
@@ -32,0 +34,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97682
1580
6