Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae587f3

Browse filesBrowse files
stv8MylesBorins
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 <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>
1 parent da70161 commit ae587f3
Copy full SHA for ae587f3

File tree

Expand file treeCollapse file tree

5 files changed

+11
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+11
-3
lines changed
Open diff view settings
Collapse file

‎doc/api/cluster.md‎

Copy file name to clipboardExpand all lines: doc/api/cluster.md
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ It is not emitted in the worker.
262262
added: v0.7.7
263263
-->
264264

265+
* Returns: {Worker} A reference to `worker`.
266+
265267
In a worker, this function will close all servers, wait for the `'close'` event on
266268
those servers, and then disconnect the IPC channel.
267269

Collapse file

‎lib/cluster.js‎

Copy file name to clipboardExpand all lines: lib/cluster.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ function masterInit() {
429429
send(this, { act: 'disconnect' });
430430
removeHandlesForWorker(this);
431431
removeWorker(this);
432+
return this;
432433
};
433434

434435
Worker.prototype.destroy = function(signo) {
@@ -686,6 +687,7 @@ function workerInit() {
686687

687688
Worker.prototype.disconnect = function() {
688689
_disconnect.call(this);
690+
return this;
689691
};
690692

691693
Worker.prototype.destroy = function() {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-cluster-worker-destroy.js
+3-1Lines changed: 3 additions & 1 deletion
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
const cluster = require('cluster');
1213
let 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();
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-cluster-worker-disconnect.js
+2-1Lines changed: 2 additions & 1 deletion
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
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-cluster-worker-init.js
+2-1Lines changed: 2 additions & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.