Skip to content

Commit ae587f3

Browse 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 <[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 da70161 commit ae587f3

File tree

5 files changed

+11
-3
lines changed

5 files changed

+11
-3
lines changed

‎doc/api/cluster.md‎

Lines changed: 2 additions & 0 deletions
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

‎lib/cluster.js‎

Lines 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+
returnthis;
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+
returnthis;
689691
};
690692

691693
Worker.prototype.destroy=function(){

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
constcommon=require('../common');
11+
constassert=require('assert');
1112
constcluster=require('cluster');
1213
letworker1,worker2;
1314

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

29-
cluster.worker.disconnect();
30+
constw=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‎

Lines 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+
constw=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‎

Lines 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+
constw=worker.disconnect();
17+
assert.strictEqual(worker,w,'did not return a reference');
1718
}));
1819

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

0 commit comments

Comments
(0)