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 <[email protected]>
Reviewed-By: Santiago Gimeno <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Roman Reiss <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
  • Loading branch information
stv8 authored and MylesBorins committed May 18, 2017
1 parent da70161 commit ae587f3
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 @@ -262,6 +262,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 @@ -429,6 +429,7 @@ function masterInit() {
send(this, { act: 'disconnect' });
removeHandlesForWorker(this);
removeWorker(this);
return this;
};

Worker.prototype.destroy = function(signo) {
Expand Down Expand Up @@ -686,6 +687,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');
const cluster = require('cluster');
let 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 ae587f3

Please sign in to comment.