@@ -49,6 +49,8 @@ var Process = module.exports = function Process(options) {
4949 workersKilled : 0
5050 } ;
5151
52+ this . options . maxHeartbeatDelay = options . maxHeartbeatDelay || 3 * 60000 ; //default 3 mins
53+
5254 this . _heartbeats = [ ] ;
5355
5456 this . killall = function ( signal ) {
@@ -150,9 +152,12 @@ var Process = module.exports = function Process(options) {
150152 self . emitter . emit ( 'listening' , message . pid ) ;
151153 }
152154 if ( message . type === 'heartbeat' ) {
153-
154155 if ( message . pid != process . pid ) {
155156 self . _heartbeats . push ( message ) ; //must append to the tail
157+ // update the last heartbeat time for the worker
158+ var workerStats = self . stats . workers [ message . pid ] ;
159+ //console.log('heartbeat ' + process.pid);
160+ workerStats . lastHeartbeatAt = Date . now ( ) ;
156161 }
157162
158163 self . _heartbeatScheduler = self . _heartbeatScheduler || setInterval ( function ( ) {
@@ -194,6 +199,23 @@ var Process = module.exports = function Process(options) {
194199 } ) ;
195200
196201 self . lastTime = Date . now ( ) ;
202+
203+ // Check the last heartbeat time of all the workers
204+ _ . each ( self . stats . workers , function ( workerStats , pid ) {
205+ var now = Date . now ( ) ;
206+ if ( now - workerStats . lastHeartbeatAt > self . options . maxHeartbeatDelay ) {
207+ // this worker hasn't been sending heartbeat for maxHeartbeatDelay
208+ log ( util . format ( '[Cluster2] Detected worker%d is not responsive for %d' , pid , now - workerStats . lastHeartbeatAt ) ) ;
209+ var deathQueue = require ( './misc' ) . deathQueue ;
210+ deathQueue ( self . workers [ pid ] , self . emitter , function ( ) {
211+ // create a successor
212+ var successor = self . createWorker ( ) ;
213+ self . workers [ successor . pid ] = successor ;
214+ log ( util . format ( '[Cluster2] Created a new worker with pid %d' , successor . pid ) ) ;
215+ return successor ;
216+ } ) ;
217+ }
218+ } ) ;
197219
198220 } , self . options . heartbeatInterval || 60000 ) ;
199221 }
@@ -385,17 +407,19 @@ Process.prototype.listen = function() {
385407 self . emitter . emit ( 'died' , worker . pid ) ;
386408 self . stats . workersKilled ++ ;
387409 self . stats . noWorkers -- ;
410+ delete self . workers [ worker . pid + '' ] ;
411+ delete self . stats . workers [ worker . pid ] ;
388412 return ;
389413 }
390414
391415 self . emitter . emit ( 'died' , worker . pid ) ;
392416 self . stats . workersKilled ++ ;
393417 self . stats . noWorkers -- ;
418+ delete self . workers [ worker . pid + '' ] ;
419+ delete self . stats . workers [ worker . pid ] ;
394420 //bugfix by huzhou@ebay .com, worker & replacement name collision
395421 var replacement = self . createWorker ( ) ;
396422 self . workers [ replacement . pid + '' ] = replacement ;
397- delete self . workers [ worker . pid + '' ] ;
398- delete self . stats . workers [ worker . pid ] ;
399423
400424 log ( '[cluster2] updated worker list:' + _ . keys ( self . workers ) ) ;
401425 } ;
@@ -456,6 +480,8 @@ Process.prototype.listen = function() {
456480 }
457481 } ) ;
458482
483+ // put the emitter in the process
484+ process . emitter = self . emitter ;
459485 self . emitter . emit ( signal , {
460486 pid : process . pid ,
461487 type : 'worker'
@@ -551,7 +577,7 @@ Process.prototype.listen = function() {
551577 // we'd like to have the threshold randomized in between [1, 1.5) of the given threshold
552578 // to avoid all workers die around the same time. This is in particular important for boxes of small number of cpu cores
553579 var connThreshold = self . options . connThreshold ,
554- uptimeThreshod = self . options . uptimeThreshod ;
580+ uptimeThreshold = self . options . uptimeThreshold ;
555581
556582 var recycle = setInterval ( function ( ) {
557583
@@ -637,6 +663,8 @@ Process.prototype.listen = function() {
637663 } ) ;
638664
639665 } , self . options . heartbeatInterval || 60000 ) ;
666+ // put the heartbeat interval id in the process context
667+ process . heartbeat = heartbeat ;
640668
641669 _ . each ( apps , function ( app ) {
642670
0 commit comments