cluster: return worker reference from disconnect()

Changes disconnect() to return a refererence to the worker.
This will enable method chaining such as

    worker.disconnect().once('disconnect', doThis);

PR-URL: https://github.com/nodejs/node/pull/10019
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
This commit is contained in:
Sean Villars 2016-12-01 11:30:34 -06:00 committed by Sam Roberts
parent 5e781a3883
commit 5d14602181
5 changed files with 11 additions and 3 deletions

View File

@ -257,6 +257,8 @@ It is not emitted in the worker.
added: v0.7.7 added: v0.7.7
--> -->
* Returns: {Worker} A reference to `worker`.
In a worker, this function will close all servers, wait for the `'close'` event on In a worker, this function will close all servers, wait for the `'close'` event on
those servers, and then disconnect the IPC channel. those servers, and then disconnect the IPC channel.

View File

@ -430,6 +430,7 @@ function masterInit() {
send(this, { act: 'disconnect' }); send(this, { act: 'disconnect' });
removeHandlesForWorker(this); removeHandlesForWorker(this);
removeWorker(this); removeWorker(this);
return this;
}; };
Worker.prototype.destroy = function(signo) { Worker.prototype.destroy = function(signo) {
@ -687,6 +688,7 @@ function workerInit() {
Worker.prototype.disconnect = function() { Worker.prototype.disconnect = function() {
_disconnect.call(this); _disconnect.call(this);
return this;
}; };
Worker.prototype.destroy = function() { Worker.prototype.destroy = function() {

View File

@ -8,6 +8,7 @@
*/ */
const common = require('../common'); const common = require('../common');
const assert = require('assert');
var cluster = require('cluster'); var cluster = require('cluster');
var worker1, worker2; var worker1, worker2;
@ -26,7 +27,8 @@ if (cluster.isMaster) {
cluster.worker.destroy(); cluster.worker.destroy();
}); });
cluster.worker.disconnect(); const w = cluster.worker.disconnect();
assert.strictEqual(w, cluster.worker, 'did not return a reference');
} else { } else {
// Call destroy when worker is not disconnected yet // Call destroy when worker is not disconnected yet
cluster.worker.destroy(); cluster.worker.destroy();

View File

@ -40,7 +40,8 @@ if (cluster.isWorker) {
// Disconnect worker when it is ready // Disconnect worker when it is ready
worker.once('listening', common.mustCall(() => { worker.once('listening', common.mustCall(() => {
worker.disconnect(); const w = worker.disconnect();
assert.strictEqual(worker, w, 'did not return a reference');
})); }));
// Check cluster events // Check cluster events

View File

@ -13,7 +13,8 @@ if (cluster.isMaster) {
worker.on('message', common.mustCall((message) => { worker.on('message', common.mustCall((message) => {
assert.strictEqual(message, true, 'did not receive expected message'); assert.strictEqual(message, true, 'did not receive expected message');
worker.disconnect(); const w = worker.disconnect();
assert.strictEqual(worker, w, 'did not return a reference');
})); }));
worker.on('online', () => { worker.on('online', () => {