Skip to content

Commit

Permalink
cluster: return worker reference from disconnect()
Browse files Browse the repository at this point in the history
Changes disconnect() to return a refererence to the worker.
This will enable method chaining such as

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

PR-URL: #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>
  • Loading branch information
stv8 authored and cjihrig committed Dec 20, 2016
1 parent ace5cad commit 663c44e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ It is not emitted in the worker.
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
those servers, and then disconnect the IPC channel.

Expand Down
2 changes: 2 additions & 0 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ function masterInit() {
send(this, { act: 'disconnect' });
removeHandlesForWorker(this);
removeWorker(this);
return this;
};

Worker.prototype.destroy = function(signo) {
Expand Down Expand Up @@ -687,6 +688,7 @@ function workerInit() {

Worker.prototype.disconnect = function() {
_disconnect.call(this);
return this;
};

Worker.prototype.destroy = function() {
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-cluster-worker-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

const common = require('../common');
const assert = require('assert');
var cluster = require('cluster');
var worker1, worker2;

Expand All @@ -26,7 +27,8 @@ if (cluster.isMaster) {
cluster.worker.destroy();
});

cluster.worker.disconnect();
const w = cluster.worker.disconnect();
assert.strictEqual(w, cluster.worker, 'did not return a reference');
} else {
// Call destroy when worker is not disconnected yet
cluster.worker.destroy();
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-cluster-worker-disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ if (cluster.isWorker) {

// Disconnect worker when it is ready
worker.once('listening', common.mustCall(() => {
worker.disconnect();
const w = worker.disconnect();
assert.strictEqual(worker, w, 'did not return a reference');
}));

// Check cluster events
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-cluster-worker-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ if (cluster.isMaster) {

worker.on('message', common.mustCall((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', () => {
Expand Down

0 comments on commit 663c44e

Please sign in to comment.