|
@ -81,11 +81,11 @@ function startMaster() { |
|
|
// Quickly try to kill all the workers.
|
|
|
// Quickly try to kill all the workers.
|
|
|
// TODO: be session leader - will cause auto SIGHUP to the children.
|
|
|
// TODO: be session leader - will cause auto SIGHUP to the children.
|
|
|
eachWorker(function(worker) { |
|
|
eachWorker(function(worker) { |
|
|
debug("kill worker " + worker.pid); |
|
|
debug('kill worker ' + worker.pid); |
|
|
worker.kill(); |
|
|
worker.kill(); |
|
|
}) |
|
|
}); |
|
|
|
|
|
|
|
|
console.error("Exception in cluster master process: " + |
|
|
console.error('Exception in cluster master process: ' + |
|
|
e.message + '\n' + e.stack); |
|
|
e.message + '\n' + e.stack); |
|
|
process.exit(1); |
|
|
process.exit(1); |
|
|
}); |
|
|
}); |
|
@ -96,21 +96,21 @@ function handleWorkerMessage(worker, message) { |
|
|
// This can only be called from the master.
|
|
|
// This can only be called from the master.
|
|
|
assert(cluster.isMaster); |
|
|
assert(cluster.isMaster); |
|
|
|
|
|
|
|
|
debug("recv " + JSON.stringify(message)); |
|
|
debug('recv ' + JSON.stringify(message)); |
|
|
|
|
|
|
|
|
switch (message.cmd) { |
|
|
switch (message.cmd) { |
|
|
case 'online': |
|
|
case 'online': |
|
|
debug("Worker " + worker.pid + " online"); |
|
|
debug('Worker ' + worker.pid + ' online'); |
|
|
worker.online = true; |
|
|
worker.online = true; |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 'queryServer': |
|
|
case 'queryServer': |
|
|
var key = message.address + ":" + |
|
|
var key = message.address + ':' + |
|
|
message.port + ":" + |
|
|
message.port + ':' + |
|
|
message.addressType; |
|
|
message.addressType; |
|
|
var response = { _queryId: message._queryId }; |
|
|
var response = { _queryId: message._queryId }; |
|
|
|
|
|
|
|
|
if (key in servers == false) { |
|
|
if (!(key in servers)) { |
|
|
// Create a new server.
|
|
|
// Create a new server.
|
|
|
debug('create new server ' + key); |
|
|
debug('create new server ' + key); |
|
|
servers[key] = net._createServerHandle(message.address, |
|
|
servers[key] = net._createServerHandle(message.address, |
|
@ -136,7 +136,7 @@ function eachWorker(cb) { |
|
|
cb(workers[id]); |
|
|
cb(workers[id]); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cluster.fork = function() { |
|
|
cluster.fork = function() { |
|
@ -176,19 +176,19 @@ cluster.fork = function() { |
|
|
// Internal function. Called from src/node.js when worker process starts.
|
|
|
// Internal function. Called from src/node.js when worker process starts.
|
|
|
cluster._startWorker = function() { |
|
|
cluster._startWorker = function() { |
|
|
assert(cluster.isWorker); |
|
|
assert(cluster.isWorker); |
|
|
workerId = parseInt(process.env.NODE_WORKER_ID); |
|
|
workerId = parseInt(process.env.NODE_WORKER_ID, 10); |
|
|
|
|
|
|
|
|
queryMaster({ cmd: 'online' }); |
|
|
queryMaster({ cmd: 'online' }); |
|
|
|
|
|
|
|
|
// Make callbacks from queryMaster()
|
|
|
// Make callbacks from queryMaster()
|
|
|
process.on('message', function(msg, handle) { |
|
|
process.on('message', function(msg, handle) { |
|
|
debug("recv " + JSON.stringify(msg)); |
|
|
debug('recv ' + JSON.stringify(msg)); |
|
|
if (msg._queryId && msg._queryId in queryCallbacks) { |
|
|
if (msg._queryId && msg._queryId in queryCallbacks) { |
|
|
var cb = queryCallbacks[msg._queryId]; |
|
|
var cb = queryCallbacks[msg._queryId]; |
|
|
if (typeof cb == 'function') { |
|
|
if (typeof cb == 'function') { |
|
|
cb(msg, handle); |
|
|
cb(msg, handle); |
|
|
} |
|
|
} |
|
|
delete queryCallbacks[msg._queryId] |
|
|
delete queryCallbacks[msg._queryId]; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
@ -219,7 +219,7 @@ cluster._getServer = function(address, port, addressType, cb) { |
|
|
assert(cluster.isWorker); |
|
|
assert(cluster.isWorker); |
|
|
|
|
|
|
|
|
queryMaster({ |
|
|
queryMaster({ |
|
|
cmd: "queryServer", |
|
|
cmd: 'queryServer', |
|
|
address: address, |
|
|
address: address, |
|
|
port: port, |
|
|
port: port, |
|
|
addressType: addressType |
|
|
addressType: addressType |
|
|