@ -27,7 +27,20 @@ function Worker(options) {
if ( options === null || typeof options !== 'object' )
options = { } ;
this . suicide = undefined ;
this . exitedAfterDisconnect = undefined ;
Object . defineProperty ( this , 'suicide' , {
get : function ( ) {
// TODO: Print deprecation message.
return this . exitedAfterDisconnect ;
} ,
set : function ( val ) {
// TODO: Print deprecation message.
this . exitedAfterDisconnect = val ;
} ,
enumerable : true
} ) ;
this . state = options . state || 'none' ;
this . id = options . id | 0 ;
@ -355,7 +368,7 @@ function masterInit() {
removeWorker ( worker ) ;
}
worker . suicide = ! ! worker . suicide ;
worker . exitedAfterDisconnect = ! ! worker . exitedAfterDisconnect ;
worker . state = 'dead' ;
worker . emit ( 'exit' , exitCode , signalCode ) ;
cluster . emit ( 'exit' , worker , exitCode , signalCode ) ;
@ -376,7 +389,7 @@ function masterInit() {
* /
if ( worker . isDead ( ) ) removeWorker ( worker ) ;
worker . suicide = ! ! worker . suicide ;
worker . exitedAfterDisconnect = ! ! worker . exitedAfterDisconnect ;
worker . state = 'disconnected' ;
worker . emit ( 'disconnect' ) ;
cluster . emit ( 'disconnect' , worker ) ;
@ -407,7 +420,7 @@ function masterInit() {
} ;
Worker . prototype . disconnect = function ( ) {
this . suicide = true ;
this . exitedAfterDisconnect = true ;
send ( this , { act : 'disconnect' } ) ;
removeHandlesForWorker ( this ) ;
removeWorker ( this ) ;
@ -432,8 +445,8 @@ function masterInit() {
queryServer ( worker , message ) ;
else if ( message . act === 'listening' )
listening ( worker , message ) ;
else if ( message . act === 'suicide ' )
suicide ( worker , message ) ;
else if ( message . act === 'exitedAfterDisconnect ' )
exitedAfterDisconnect ( worker , message ) ;
else if ( message . act === 'close' )
close ( worker , message ) ;
}
@ -444,14 +457,14 @@ function masterInit() {
cluster . emit ( 'online' , worker ) ;
}
function suicide ( worker , message ) {
worker . suicide = true ;
function exitedAfterDisconnect ( worker , message ) {
worker . exitedAfterDisconnect = true ;
send ( worker , { ack : message . seq } ) ;
}
function queryServer ( worker , message ) {
// Stop processing if worker already disconnecting
if ( worker . suicide )
if ( worker . exitedAfterDisconnect )
return ;
var args = [ message . address ,
message . port ,
@ -533,7 +546,7 @@ function workerInit() {
cluster . worker = worker ;
process . once ( 'disconnect' , function ( ) {
worker . emit ( 'disconnect' ) ;
if ( ! worker . suicide ) {
if ( ! worker . exitedAfterDisconnect ) {
// Unexpected disconnect, master exited, or some such nastiness, so
// worker exits immediately.
process . exit ( 0 ) ;
@ -670,10 +683,10 @@ function workerInit() {
} ;
Worker . prototype . destroy = function ( ) {
this . suicide = true ;
this . exitedAfterDisconnect = true ;
if ( ! this . isConnected ( ) ) process . exit ( 0 ) ;
var exit = process . exit . bind ( null , 0 ) ;
send ( { act : 'suicide ' } , ( ) => process . disconnect ( ) ) ;
send ( { act : 'exitedAfterDisconnect ' } , ( ) => process . disconnect ( ) ) ;
process . once ( 'disconnect' , exit ) ;
} ;
@ -682,19 +695,20 @@ function workerInit() {
}
function _ disconnect ( masterInitiated ) {
this . suicide = true ;
this . exitedAfterDisconnect = true ;
let waitingCount = 1 ;
function checkWaitingCount ( ) {
waitingCount -- ;
if ( waitingCount === 0 ) {
// If disconnect is worker initiated, wait for ack to be sure suicide
// is properly set in the master, otherwise, if it's master initiated
// there's no need to send the suicide message
// If disconnect is worker initiated, wait for ack to be sure
// exitedAfterDisconnect is properly set in the master, otherwise, if
// it's master initiated there's no need to send the
// exitedAfterDisconnect message
if ( masterInitiated ) {
process . disconnect ( ) ;
} else {
send ( { act : 'suicide ' } , ( ) => process . disconnect ( ) ) ;
send ( { act : 'exitedAfterDisconnect ' } , ( ) => process . disconnect ( ) ) ;
}
}
}