From ae587f3578c4f5286b7547255ed422dd4c48a3b7 Mon Sep 17 00:00:00 2001 From: Sean Villars Date: Thu, 1 Dec 2016 11:30:34 -0600 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/10019 Reviewed-By: Colin Ihrig Reviewed-By: Santiago Gimeno Reviewed-By: Jeremiah Senkpiel Reviewed-By: Roman Reiss Reviewed-By: Sam Roberts --- doc/api/cluster.md | 2 ++ lib/cluster.js | 2 ++ test/parallel/test-cluster-worker-destroy.js | 4 +++- test/parallel/test-cluster-worker-disconnect.js | 3 ++- test/parallel/test-cluster-worker-init.js | 3 ++- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index dc8b6ec6a2b220..01a2aaf6a8cc86 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -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. diff --git a/lib/cluster.js b/lib/cluster.js index 0e82de4e064ab7..5307e09d6b5566 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -429,6 +429,7 @@ function masterInit() { send(this, { act: 'disconnect' }); removeHandlesForWorker(this); removeWorker(this); + return this; }; Worker.prototype.destroy = function(signo) { @@ -686,6 +687,7 @@ function workerInit() { Worker.prototype.disconnect = function() { _disconnect.call(this); + return this; }; Worker.prototype.destroy = function() { diff --git a/test/parallel/test-cluster-worker-destroy.js b/test/parallel/test-cluster-worker-destroy.js index 91091318afa1d3..62000254247a1f 100644 --- a/test/parallel/test-cluster-worker-destroy.js +++ b/test/parallel/test-cluster-worker-destroy.js @@ -8,6 +8,7 @@ */ const common = require('../common'); +const assert = require('assert'); const cluster = require('cluster'); let worker1, worker2; @@ -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(); diff --git a/test/parallel/test-cluster-worker-disconnect.js b/test/parallel/test-cluster-worker-disconnect.js index 4e17a7d475e3d0..684fd5541eac55 100644 --- a/test/parallel/test-cluster-worker-disconnect.js +++ b/test/parallel/test-cluster-worker-disconnect.js @@ -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 diff --git a/test/parallel/test-cluster-worker-init.js b/test/parallel/test-cluster-worker-init.js index 3b82866d1b14eb..eaa9746c5f99a3 100644 --- a/test/parallel/test-cluster-worker-init.js +++ b/test/parallel/test-cluster-worker-init.js @@ -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', () => {