cluster: Rename destroy() to kill(signal=SIGTERM)

Fix #4133, bringing the cluster worker API more in line with the
child process API.
This commit is contained in:
isaacs 2013-03-03 15:22:12 -08:00
parent 384f1be739
commit e428bb7eae
6 changed files with 25 additions and 15 deletions

View File

@ -178,8 +178,9 @@ on more than one address.
* `worker` {Worker object} * `worker` {Worker object}
When a workers IPC channel has disconnected this event is emitted. This will happen When a workers IPC channel has disconnected this event is emitted.
when the worker dies, usually after calling `.destroy()`. This will happen when the worker dies, usually after calling
`.kill()`.
When calling `.disconnect()`, there may be a delay between the When calling `.disconnect()`, there may be a delay between the
`disconnect` and `exit` events. This event can be used to detect if `disconnect` and `exit` events. This event can be used to detect if
@ -323,8 +324,9 @@ See: [Child Process module](child_process.html)
* {Boolean} * {Boolean}
This property is a boolean. It is set when a worker dies after calling `.destroy()` This property is a boolean. It is set when a worker dies after calling
or immediately after calling the `.disconnect()` method. Until then it is `undefined`. `.kill()` or immediately after calling the `.disconnect()` method.
Until then it is `undefined`.
### worker.send(message, [sendHandle]) ### worker.send(message, [sendHandle])
@ -348,7 +350,10 @@ This example will echo back all messages from the master:
}); });
} }
### worker.destroy() ### worker.kill([signal='SIGTERM'])
* `signal` {String} Name of the kill signal to send to the worker
process.
This function will kill the worker, and inform the master to not spawn a This function will kill the worker, and inform the master to not spawn a
new worker. The boolean `suicide` lets you distinguish between voluntary new worker. The boolean `suicide` lets you distinguish between voluntary
@ -360,9 +365,11 @@ and accidental exit.
} }
}); });
// destroy worker // kill worker
worker.destroy(); worker.kill();
This method is aliased as `worker.destroy()` for backwards
compatibility.
### worker.disconnect() ### worker.disconnect()
@ -375,7 +382,7 @@ the worker finally die.
Because there might be long living connections, it is useful to implement a timeout. Because there might be long living connections, it is useful to implement a timeout.
This example ask the worker to disconnect and after 2 seconds it will destroy the This example ask the worker to disconnect and after 2 seconds it will destroy the
server. An alternative would be to execute `worker.destroy()` after 2 seconds, but server. An alternative would be to execute `worker.kill()` after 2 seconds, but
that would normally not allow the worker to do any cleanup if needed. that would normally not allow the worker to do any cleanup if needed.
if (cluster.isMaster) { if (cluster.isMaster) {

View File

@ -401,7 +401,10 @@ Worker.prototype.send = function() {
}; };
// Kill the worker without restarting // Kill the worker without restarting
Worker.prototype.destroy = function() { Worker.prototype.kill = Worker.prototype.destroy = function(signal) {
if (!signal)
signal = 'SIGTERM';
var self = this; var self = this;
this.suicide = true; this.suicide = true;
@ -411,11 +414,11 @@ Worker.prototype.destroy = function() {
// this way the worker won't need to propagate suicide state to master // this way the worker won't need to propagate suicide state to master
if (self.process.connected) { if (self.process.connected) {
self.process.once('disconnect', function() { self.process.once('disconnect', function() {
self.process.kill(); self.process.kill(signal);
}); });
self.process.disconnect(); self.process.disconnect();
} else { } else {
self.process.kill(); self.process.kill(signal);
} }
} else { } else {

View File

@ -102,7 +102,7 @@ else if (cluster.isMaster) {
//Kill worker when listening //Kill worker when listening
cluster.on('listening', function() { cluster.on('listening', function() {
worker.destroy(); worker.kill();
}); });
//Kill process when worker is killed //Kill process when worker is killed

View File

@ -32,7 +32,7 @@ if (cluster.isMaster) {
assert(port); assert(port);
// ensure that the port is numerical // ensure that the port is numerical
assert.strictEqual(typeof(port), 'number'); assert.strictEqual(typeof(port), 'number');
worker.destroy(); worker.kill();
}); });
process.on('exit', function() { process.on('exit', function() {
// ensure that the 'listening' handler has been called // ensure that the 'listening' handler has been called

View File

@ -123,7 +123,7 @@ else if (cluster.isMaster) {
// When the connection ends kill worker and shutdown process // When the connection ends kill worker and shutdown process
client.on('end', function() { client.on('end', function() {
worker.destroy(); worker.kill();
}); });
worker.on('exit', function() { worker.on('exit', function() {

View File

@ -66,7 +66,7 @@ if (cluster.isWorker) {
if (correctIn === totalWorkers) { if (correctIn === totalWorkers) {
checks.args = true; checks.args = true;
} }
worker.destroy(); worker.kill();
}); });
// All workers are online // All workers are online