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