Skip to content

Commit 341825d

Browse files
committed
Added workerCluster events. Expose workerCluster publicly.
1 parent fc98443 commit 341825d

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

index.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ var SocketCluster = function (options) {
3737
self.EVENT_WORKER_EXIT = 'workerExit';
3838
self.EVENT_BROKER_START = 'brokerStart';
3939
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';
4043

4144
self._errorAnnotations = {
4245
'EADDRINUSE': 'Failed to bind to a port because it was already used by another process.'
@@ -116,7 +119,7 @@ SocketCluster.prototype._init = function (options) {
116119
};
117120

118121
self._active = false;
119-
self._workerCluster = null;
122+
self.workerCluster = null;
120123

121124
self._colorCodes = {
122125
red: 31,
@@ -491,6 +494,12 @@ SocketCluster.prototype._workerClusterReadyHandler = function () {
491494
this._active = true;
492495
this._logDeploymentDetails();
493496
}
497+
498+
var workerClusterInfo = {
499+
pid: this.workerCluster.pid,
500+
childProcess: this.workerCluster
501+
};
502+
this.emit(this.EVENT_WORKER_CLUSTER_READY, workerClusterInfo);
494503
};
495504

496505
SocketCluster.prototype._workerExitHandler = function (workerInfo) {
@@ -504,14 +513,22 @@ SocketCluster.prototype._workerExitHandler = function (workerInfo) {
504513
this.emit(this.EVENT_WORKER_EXIT, workerInfo);
505514
};
506515

507-
SocketCluster.prototype._workerStartHandler = function (workerInfo) {
516+
SocketCluster.prototype._workerStartHandler = function (workerInfo, signal) {
508517
if (this._active && this.options.logLevel > 0 && workerInfo.respawn) {
509518
this.log('Worker ' + workerInfo.id + ' was respawned');
510519
}
511520
this.emit(this.EVENT_WORKER_START, workerInfo);
512521
};
513522

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+
515532
var message = 'workerCluster exited with code: ' + errorCode;
516533
if (errorCode == 0) {
517534
this.log(message);
@@ -556,7 +573,7 @@ SocketCluster.prototype._launchWorkerCluster = function () {
556573
execOptions.execArgv.push('--inspect=' + inspectPort);
557574
}
558575

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);
560577

561578
var workerOpts = this._cloneObject(this.options);
562579
workerOpts.paths = this._paths;
@@ -580,12 +597,12 @@ SocketCluster.prototype._launchWorkerCluster = function () {
580597
}
581598
}
582599

583-
this._workerCluster.send({
600+
this.workerCluster.send({
584601
type: 'init',
585602
data: workerOpts
586603
});
587604

588-
this._workerCluster.on('message', function workerHandler(m) {
605+
this.workerCluster.on('message', function workerHandler(m) {
589606
if (m.type == 'error') {
590607
var error = scErrors.hydrateError(m.data.error);
591608
if (m.data.workerPid) {
@@ -607,7 +624,13 @@ SocketCluster.prototype._launchWorkerCluster = function () {
607624
}
608625
});
609626

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));
611634
};
612635

613636
SocketCluster.prototype._logDeploymentDetails = function () {
@@ -697,7 +720,7 @@ SocketCluster.prototype._start = function () {
697720
};
698721

699722
SocketCluster.prototype.sendToWorker = function (workerId, data) {
700-
this._workerCluster.send({
723+
this.workerCluster.send({
701724
type: 'masterMessage',
702725
workerId: workerId,
703726
data: data
@@ -712,8 +735,8 @@ SocketCluster.prototype.sendToBroker = function (brokerId, data) {
712735
// immediate: Shut down the workers immediately without waiting for termination timeout.
713736
// killClusterMaster: Shut down the cluster master (load balancer) as well as all the workers.
714737
SocketCluster.prototype.killWorkers = function (options) {
715-
if (this._workerCluster) {
716-
this._workerCluster.send({
738+
if (this.workerCluster) {
739+
this.workerCluster.send({
717740
type: 'terminate',
718741
data: options || {}
719742
});

lib/workercluster.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var InvalidActionError = scErrors.InvalidActionError;
66
var processTermTimeout = 10000;
77

88
if (cluster.isMaster) {
9-
109
process.on('disconnect', function () {
1110
process.exit();
1211
});
@@ -69,7 +68,9 @@ if (cluster.isMaster) {
6968

7069
if (!isReady && ++readyCount >= workerCount) {
7170
isReady = true;
72-
process.send(m);
71+
process.send({
72+
type: 'ready'
73+
});
7374
}
7475
} else {
7576
process.send(m);

0 commit comments

Comments
 (0)