Browse Source
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 <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>
v6.x
Sean Villars
8 years ago
committed by
Myles Borins
No known key found for this signature in database
GPG Key ID: 933B01F40B5CA946
5 changed files with
11 additions and
3 deletions
doc/api/cluster.md
lib/cluster.js
test/parallel/test-cluster-worker-destroy.js
test/parallel/test-cluster-worker-disconnect.js
test/parallel/test-cluster-worker-init.js
@ -262,6 +262,8 @@ It is not emitted in the worker.
added: v0.7.7
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
In a worker, this function will close all servers, wait for the `'close'` event on
those servers, and then disconnect the IPC channel.
those servers, and then disconnect the IPC channel.
@ -429,6 +429,7 @@ function masterInit() {
send ( this , { act : 'disconnect' } ) ;
send ( this , { act : 'disconnect' } ) ;
removeHandlesForWorker ( this ) ;
removeHandlesForWorker ( this ) ;
removeWorker ( this ) ;
removeWorker ( this ) ;
return this ;
} ;
} ;
Worker . prototype . destroy = function ( signo ) {
Worker . prototype . destroy = function ( signo ) {
@ -686,6 +687,7 @@ function workerInit() {
Worker . prototype . disconnect = function ( ) {
Worker . prototype . disconnect = function ( ) {
_ disconnect . call ( this ) ;
_ disconnect . call ( this ) ;
return this ;
} ;
} ;
Worker . prototype . destroy = function ( ) {
Worker . prototype . destroy = function ( ) {
@ -8,6 +8,7 @@
* /
* /
const common = require ( '../common' ) ;
const common = require ( '../common' ) ;
const assert = require ( 'assert' ) ;
const cluster = require ( 'cluster' ) ;
const cluster = require ( 'cluster' ) ;
let worker1 , worker2 ;
let worker1 , worker2 ;
@ -26,7 +27,8 @@ if (cluster.isMaster) {
cluster . worker . destroy ( ) ;
cluster . worker . destroy ( ) ;
} ) ;
} ) ;
cluster . worker . disconnect ( ) ;
const w = cluster . worker . disconnect ( ) ;
assert . strictEqual ( w , cluster . worker , 'did not return a reference' ) ;
} else {
} else {
// Call destroy when worker is not disconnected yet
// Call destroy when worker is not disconnected yet
cluster . worker . destroy ( ) ;
cluster . worker . destroy ( ) ;
@ -40,7 +40,8 @@ if (cluster.isWorker) {
// Disconnect worker when it is ready
// Disconnect worker when it is ready
worker . once ( 'listening' , common . mustCall ( ( ) => {
worker . once ( 'listening' , common . mustCall ( ( ) => {
worker . disconnect ( ) ;
const w = worker . disconnect ( ) ;
assert . strictEqual ( worker , w , 'did not return a reference' ) ;
} ) ) ;
} ) ) ;
// Check cluster events
// Check cluster events
@ -13,7 +13,8 @@ if (cluster.isMaster) {
worker . on ( 'message' , common . mustCall ( ( message ) => {
worker . on ( 'message' , common . mustCall ( ( message ) => {
assert . strictEqual ( message , true , 'did not receive expected 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' , ( ) => {
worker . on ( 'online' , ( ) => {