Skip to content

Commit 5d14602

Browse files
stv8sam-github
authored andcommitted
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: #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]>
1 parent 5e781a3 commit 5d14602

5 files changed

+11
-3
lines changed

doc/api/cluster.md

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ It is not emitted in the worker.
257257
added: v0.7.7
258258
-->
259259

260+
* Returns: {Worker} A reference to `worker`.
261+
260262
In a worker, this function will close all servers, wait for the `'close'` event on
261263
those servers, and then disconnect the IPC channel.
262264

lib/cluster.js

+2
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ function masterInit() {
430430
send(this, { act: 'disconnect' });
431431
removeHandlesForWorker(this);
432432
removeWorker(this);
433+
return this;
433434
};
434435

435436
Worker.prototype.destroy = function(signo) {
@@ -687,6 +688,7 @@ function workerInit() {
687688

688689
Worker.prototype.disconnect = function() {
689690
_disconnect.call(this);
691+
return this;
690692
};
691693

692694
Worker.prototype.destroy = function() {

test/parallel/test-cluster-worker-destroy.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
const common = require('../common');
11+
const assert = require('assert');
1112
var cluster = require('cluster');
1213
var worker1, worker2;
1314

@@ -26,7 +27,8 @@ if (cluster.isMaster) {
2627
cluster.worker.destroy();
2728
});
2829

29-
cluster.worker.disconnect();
30+
const w = cluster.worker.disconnect();
31+
assert.strictEqual(w, cluster.worker, 'did not return a reference');
3032
} else {
3133
// Call destroy when worker is not disconnected yet
3234
cluster.worker.destroy();

test/parallel/test-cluster-worker-disconnect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ if (cluster.isWorker) {
4040

4141
// Disconnect worker when it is ready
4242
worker.once('listening', common.mustCall(() => {
43-
worker.disconnect();
43+
const w = worker.disconnect();
44+
assert.strictEqual(worker, w, 'did not return a reference');
4445
}));
4546

4647
// Check cluster events

test/parallel/test-cluster-worker-init.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ if (cluster.isMaster) {
1313

1414
worker.on('message', common.mustCall((message) => {
1515
assert.strictEqual(message, true, 'did not receive expected message');
16-
worker.disconnect();
16+
const w = worker.disconnect();
17+
assert.strictEqual(worker, w, 'did not return a reference');
1718
}));
1819

1920
worker.on('online', () => {

0 commit comments

Comments
 (0)