|
|
@ -51,7 +51,7 @@ var ids = 0; |
|
|
|
var serverHandlers = {}; |
|
|
|
|
|
|
|
// Used in the worker:
|
|
|
|
var serverLisenters = {}; |
|
|
|
var serverListeners = {}; |
|
|
|
var queryIds = 0; |
|
|
|
var queryCallbacks = {}; |
|
|
|
|
|
|
@ -153,7 +153,7 @@ function handleResponse(outMessage, outHandle, inMessage, inHandle, worker) { |
|
|
|
} |
|
|
|
|
|
|
|
// Handle messages from both master and workers
|
|
|
|
var messageHandingObject = {}; |
|
|
|
var messageHandler = {}; |
|
|
|
function handleMessage(worker, inMessage, inHandle) { |
|
|
|
|
|
|
|
//Remove internal prefix
|
|
|
@ -161,14 +161,14 @@ function handleMessage(worker, inMessage, inHandle) { |
|
|
|
message.cmd = inMessage.cmd.substr(INTERNAL_PREFIX.length); |
|
|
|
|
|
|
|
var respondUsed = false; |
|
|
|
var respond = function(outMessage, outHandler) { |
|
|
|
function respond(outMessage, outHandler) { |
|
|
|
respondUsed = true; |
|
|
|
handleResponse(outMessage, outHandler, inMessage, inHandle, worker); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// Run handler if it exist
|
|
|
|
if (messageHandingObject[message.cmd]) { |
|
|
|
messageHandingObject[message.cmd](message, worker, respond); |
|
|
|
if (messageHandler[message.cmd]) { |
|
|
|
messageHandler[message.cmd](message, worker, respond); |
|
|
|
} |
|
|
|
|
|
|
|
// Send respond if it wasn't done
|
|
|
@ -181,7 +181,7 @@ function handleMessage(worker, inMessage, inHandle) { |
|
|
|
if (cluster.isMaster) { |
|
|
|
|
|
|
|
// Handle online messages from workers
|
|
|
|
messageHandingObject.online = function(message, worker) { |
|
|
|
messageHandler.online = function(message, worker) { |
|
|
|
worker.state = 'online'; |
|
|
|
debug('Worker ' + worker.process.pid + ' online'); |
|
|
|
worker.emit('online', worker); |
|
|
@ -189,7 +189,7 @@ if (cluster.isMaster) { |
|
|
|
}; |
|
|
|
|
|
|
|
// Handle queryServer messages form workers
|
|
|
|
messageHandingObject.queryServer = function(message, worker, send) { |
|
|
|
messageHandler.queryServer = function(message, worker, send) { |
|
|
|
|
|
|
|
// This sequence of infomation is unique to the connection but not
|
|
|
|
// to the worker
|
|
|
@ -208,7 +208,7 @@ if (cluster.isMaster) { |
|
|
|
}; |
|
|
|
|
|
|
|
// Handle listening messages from workers
|
|
|
|
messageHandingObject.listening = function(message, worker) { |
|
|
|
messageHandler.listening = function(message, worker) { |
|
|
|
|
|
|
|
worker.state = 'listening'; |
|
|
|
|
|
|
@ -226,7 +226,7 @@ if (cluster.isMaster) { |
|
|
|
}; |
|
|
|
|
|
|
|
// Handle suicide messages from workers
|
|
|
|
messageHandingObject.suicide = function(message, worker) { |
|
|
|
messageHandler.suicide = function(message, worker) { |
|
|
|
worker.suicide = true; |
|
|
|
}; |
|
|
|
|
|
|
@ -236,7 +236,7 @@ if (cluster.isMaster) { |
|
|
|
else if (cluster.isWorker) { |
|
|
|
|
|
|
|
// Handle worker.disconnect from master
|
|
|
|
messageHandingObject.disconnect = function(message, worker) { |
|
|
|
messageHandler.disconnect = function(message, worker) { |
|
|
|
worker.disconnect(); |
|
|
|
}; |
|
|
|
} |
|
|
@ -412,12 +412,12 @@ if (cluster.isMaster) { |
|
|
|
this.suicide = true; |
|
|
|
|
|
|
|
// keep track of open servers
|
|
|
|
var servers = Object.keys(serverLisenters).length; |
|
|
|
var servers = Object.keys(serverListeners).length; |
|
|
|
var progress = new ProgressTracker(servers, function() { |
|
|
|
// there are no more servers open so we will close the IPC channel.
|
|
|
|
// Closeing the IPC channel will emit emit a disconnect event
|
|
|
|
// Closing the IPC channel will emit a disconnect event
|
|
|
|
// in both master and worker on the process object.
|
|
|
|
// This event will be handled by prepearDeath.
|
|
|
|
// This event will be handled by prepareDeath.
|
|
|
|
self.process.disconnect(); |
|
|
|
}); |
|
|
|
|
|
|
@ -428,10 +428,10 @@ if (cluster.isMaster) { |
|
|
|
// in case there are no servers
|
|
|
|
progress.check(); |
|
|
|
|
|
|
|
// closeing all servers graceful
|
|
|
|
// closing all servers gracefully
|
|
|
|
var server; |
|
|
|
for (var key in serverLisenters) { |
|
|
|
server = serverLisenters[key]; |
|
|
|
for (var key in serverListeners) { |
|
|
|
server = serverListeners[key]; |
|
|
|
|
|
|
|
// in case the server is closed we wont close it again
|
|
|
|
if (server._handle === null) { |
|
|
@ -491,7 +491,7 @@ cluster._setupWorker = function() { |
|
|
|
// Get worker class
|
|
|
|
var worker = cluster.worker = new Worker(); |
|
|
|
|
|
|
|
// when the worker is disconnected from parent accidently
|
|
|
|
// when the worker is disconnected from parent accidentally
|
|
|
|
// we will terminate the worker
|
|
|
|
process.once('disconnect', function() { |
|
|
|
if (worker.suicide !== true) { |
|
|
@ -511,7 +511,7 @@ cluster._getServer = function(tcpSelf, address, port, addressType, cb) { |
|
|
|
|
|
|
|
// Store tcp instance for later use
|
|
|
|
var key = [address, port, addressType].join(':'); |
|
|
|
serverLisenters[key] = tcpSelf; |
|
|
|
serverListeners[key] = tcpSelf; |
|
|
|
|
|
|
|
// Send a listening message to the master
|
|
|
|
tcpSelf.once('listening', function() { |
|
|
|