@@ -37,6 +37,9 @@ var SocketCluster = function (options) {
37
37
self . EVENT_WORKER_EXIT = 'workerExit' ;
38
38
self . EVENT_BROKER_START = 'brokerStart' ;
39
39
self . EVENT_BROKER_EXIT = 'brokerExit' ;
40
+ self . EVENT_WORKER_CLUSTER_START = 'workerClusterStart' ;
41
+ self . EVENT_WORKER_CLUSTER_READY = 'workerClusterReady' ;
42
+ self . EVENT_WORKER_CLUSTER_EXIT = 'workerClusterExit' ;
40
43
41
44
self . _errorAnnotations = {
42
45
'EADDRINUSE' : 'Failed to bind to a port because it was already used by another process.'
@@ -116,7 +119,7 @@ SocketCluster.prototype._init = function (options) {
116
119
} ;
117
120
118
121
self . _active = false ;
119
- self . _workerCluster = null ;
122
+ self . workerCluster = null ;
120
123
121
124
self . _colorCodes = {
122
125
red : 31 ,
@@ -491,6 +494,12 @@ SocketCluster.prototype._workerClusterReadyHandler = function () {
491
494
this . _active = true ;
492
495
this . _logDeploymentDetails ( ) ;
493
496
}
497
+
498
+ var workerClusterInfo = {
499
+ pid : this . workerCluster . pid ,
500
+ childProcess : this . workerCluster
501
+ } ;
502
+ this . emit ( this . EVENT_WORKER_CLUSTER_READY , workerClusterInfo ) ;
494
503
} ;
495
504
496
505
SocketCluster . prototype . _workerExitHandler = function ( workerInfo ) {
@@ -504,14 +513,22 @@ SocketCluster.prototype._workerExitHandler = function (workerInfo) {
504
513
this . emit ( this . EVENT_WORKER_EXIT , workerInfo ) ;
505
514
} ;
506
515
507
- SocketCluster . prototype . _workerStartHandler = function ( workerInfo ) {
516
+ SocketCluster . prototype . _workerStartHandler = function ( workerInfo , signal ) {
508
517
if ( this . _active && this . options . logLevel > 0 && workerInfo . respawn ) {
509
518
this . log ( 'Worker ' + workerInfo . id + ' was respawned' ) ;
510
519
}
511
520
this . emit ( this . EVENT_WORKER_START , workerInfo ) ;
512
521
} ;
513
522
514
- SocketCluster . prototype . _handleWorkerClusterExit = function ( errorCode ) {
523
+ SocketCluster . prototype . _handleWorkerClusterExit = function ( errorCode , signal ) {
524
+ var workerClusterInfo = {
525
+ pid : this . workerCluster . pid ,
526
+ code : errorCode ,
527
+ signal : signal ,
528
+ childProcess : this . workerCluster
529
+ } ;
530
+ this . emit ( this . EVENT_WORKER_CLUSTER_EXIT , workerClusterInfo ) ;
531
+
515
532
var message = 'workerCluster exited with code: ' + errorCode ;
516
533
if ( errorCode == 0 ) {
517
534
this . log ( message ) ;
@@ -556,7 +573,7 @@ SocketCluster.prototype._launchWorkerCluster = function () {
556
573
execOptions . execArgv . push ( '--inspect=' + inspectPort ) ;
557
574
}
558
575
559
- this . _workerCluster = fork ( __dirname + '/lib/workercluster.js' , process . argv . slice ( 2 ) , execOptions ) ;
576
+ this . workerCluster = fork ( __dirname + '/lib/workercluster.js' , process . argv . slice ( 2 ) , execOptions ) ;
560
577
561
578
var workerOpts = this . _cloneObject ( this . options ) ;
562
579
workerOpts . paths = this . _paths ;
@@ -580,12 +597,12 @@ SocketCluster.prototype._launchWorkerCluster = function () {
580
597
}
581
598
}
582
599
583
- this . _workerCluster . send ( {
600
+ this . workerCluster . send ( {
584
601
type : 'init' ,
585
602
data : workerOpts
586
603
} ) ;
587
604
588
- this . _workerCluster . on ( 'message' , function workerHandler ( m ) {
605
+ this . workerCluster . on ( 'message' , function workerHandler ( m ) {
589
606
if ( m . type == 'error' ) {
590
607
var error = scErrors . hydrateError ( m . data . error ) ;
591
608
if ( m . data . workerPid ) {
@@ -607,7 +624,13 @@ SocketCluster.prototype._launchWorkerCluster = function () {
607
624
}
608
625
} ) ;
609
626
610
- this . _workerCluster . on ( 'exit' , this . _handleWorkerClusterExit . bind ( this ) ) ;
627
+ var workerClusterInfo = {
628
+ pid : this . workerCluster . pid ,
629
+ childProcess : this . workerCluster
630
+ } ;
631
+ this . emit ( this . EVENT_WORKER_CLUSTER_START , workerClusterInfo ) ;
632
+
633
+ this . workerCluster . on ( 'exit' , this . _handleWorkerClusterExit . bind ( this ) ) ;
611
634
} ;
612
635
613
636
SocketCluster . prototype . _logDeploymentDetails = function ( ) {
@@ -697,7 +720,7 @@ SocketCluster.prototype._start = function () {
697
720
} ;
698
721
699
722
SocketCluster . prototype . sendToWorker = function ( workerId , data ) {
700
- this . _workerCluster . send ( {
723
+ this . workerCluster . send ( {
701
724
type : 'masterMessage' ,
702
725
workerId : workerId ,
703
726
data : data
@@ -712,8 +735,8 @@ SocketCluster.prototype.sendToBroker = function (brokerId, data) {
712
735
// immediate: Shut down the workers immediately without waiting for termination timeout.
713
736
// killClusterMaster: Shut down the cluster master (load balancer) as well as all the workers.
714
737
SocketCluster . prototype . killWorkers = function ( options ) {
715
- if ( this . _workerCluster ) {
716
- this . _workerCluster . send ( {
738
+ if ( this . workerCluster ) {
739
+ this . workerCluster . send ( {
717
740
type : 'terminate' ,
718
741
data : options || { }
719
742
} ) ;
0 commit comments