Skip to content

Commit

Permalink
v6.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jondubois committed Aug 8, 2017
1 parent bdc8c86 commit d709eb3
Show file tree
Hide file tree
Showing 6 changed files with 1,220 additions and 59 deletions.
34 changes: 16 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var path = require('path');
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var domain = require('sc-domain');
var uuid = require('uuid');
var fork = require('child_process').fork;
var os = require('os');
Expand Down Expand Up @@ -49,18 +48,18 @@ var SocketCluster = function (options) {
'EADDRINUSE': 'Failed to bind to a port because it was already used by another process.'
};

self._errorDomain = domain.create();
self._errorDomain.on('error', function (err) {
self.errorHandler(err, {
self.on('error', function (error) {
self.emitFail(error, {
type: 'Master',
pid: process.pid
});
});

self._errorDomain.add(self);

self._errorDomain.run(function () {
// Capture any errors that are thrown during initialization.
new Promise(function () {
self._init(options);
}).catch(function (error) {
self.emit('error', error);
});
};

Expand Down Expand Up @@ -309,7 +308,7 @@ SocketCluster.prototype._init = function (options) {
}

process.stdin.on('error', function (err) {
self.warningHandler(err, {
self.emitWarning(err, {
type: 'Master',
pid: process.pid
});
Expand Down Expand Up @@ -397,7 +396,6 @@ SocketCluster.prototype._convertValueToUnknownError = function (err, origin) {
if (!(err instanceof Error)) {
if (err && typeof err == 'object') {
if (err.message || err.stack) {
// TODO 2
err = scErrors.hydrateError(err, true);
} else {
// If err has neither a stack nor a message property
Expand Down Expand Up @@ -433,7 +431,7 @@ SocketCluster.prototype._convertValueToUnknownError = function (err, origin) {
return err;
};

SocketCluster.prototype.errorHandler = function (err, origin) {
SocketCluster.prototype.emitFail = function (err, origin) {
err = this._convertValueToUnknownError(err, origin);

var annotation = this._errorAnnotations[err.code];
Expand All @@ -446,7 +444,7 @@ SocketCluster.prototype.errorHandler = function (err, origin) {
this._logObject(err, 'Error');
};

SocketCluster.prototype.warningHandler = function (warning, origin) {
SocketCluster.prototype.emitWarning = function (warning, origin) {
warning = this._convertValueToUnknownError(warning, origin);

this.emit(this.EVENT_WARNING, warning);
Expand All @@ -457,28 +455,28 @@ SocketCluster.prototype.warningHandler = function (warning, origin) {
};

SocketCluster.prototype._workerClusterErrorHandler = function (pid, error) {
this.errorHandler(error, {
this.emitFail(error, {
type: 'WorkerCluster',
pid: pid
});
};

SocketCluster.prototype._workerErrorHandler = function (workerPid, error) {
this.errorHandler(error, {
this.emitFail(error, {
type: 'Worker',
pid: workerPid
});
};

SocketCluster.prototype._brokerEngineErrorHandler = function (pid, error) {
this.errorHandler(error, {
this.emitFail(error, {
type: 'BrokerEngine',
pid: pid
});
};

SocketCluster.prototype._brokerErrorHandler = function (brokerPid, error) {
this.errorHandler(error, {
this.emitFail(error, {
type: 'Broker',
pid: brokerPid
});
Expand All @@ -489,7 +487,7 @@ SocketCluster.prototype._workerWarningHandler = function (workerPid, warning) {
type: 'Worker',
pid: workerPid
};
this.warningHandler(warning, origin);
this.emitWarning(warning, origin);
};

SocketCluster.prototype._workerClusterReadyHandler = function () {
Expand All @@ -509,7 +507,7 @@ SocketCluster.prototype._workerClusterReadyHandler = function () {

var warning = new ProcessExitError(warningMessage);

self.warningHandler(warning, {
self.emitWarning(warning, {
type: 'Master',
pid: process.pid
});
Expand Down Expand Up @@ -571,7 +569,7 @@ SocketCluster.prototype._handleWorkerClusterExit = function (errorCode, signal)
this.log(message);
} else {
var error = new ProcessExitError(message, errorCode);
this.errorHandler(error, {
this.emitFail(error, {
type: 'WorkerCluster',
pid: wcPid
});
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/socketcluster-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
spec:
containers:
- name: socketcluster
image: socketcluster/socketcluster:v6.7.1
image: socketcluster/socketcluster:v6.8.0
ports:
- containerPort: 8000
env:
Expand Down
50 changes: 20 additions & 30 deletions lib/scworker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var socketClusterServer = require('socketcluster-server');
var EventEmitter = require('events').EventEmitter;
var crypto = require('crypto');
var domain = require('sc-domain');
var uuid = require('uuid');
var http = require('http');
var https = require('https');
Expand Down Expand Up @@ -31,15 +30,7 @@ var SCWorker = function (options) {
this.type = 'worker';
self._pendingResponseHandlers = {};

this._errorDomain = domain.create();
this._errorDomain.on('error', function () {
self.errorHandler.apply(self, arguments);
});

this.start = this._errorDomain.bind(this._start);
this._errorDomain.run(function () {
self._init(options);
});
this._init(options);
};

SCWorker.prototype = Object.create(EventEmitter.prototype);
Expand Down Expand Up @@ -77,9 +68,9 @@ SCWorker.prototype._init = function (options) {
try {
process.setuid(this.options.downgradeToUser);
} catch (err) {
this._errorDomain.emit('error', new InvalidActionError('Could not downgrade to user "' + this.options.downgradeToUser +
throw new InvalidActionError('Could not downgrade to user "' + this.options.downgradeToUser +
'" - Either this user does not exist or the current process does not have the permission' +
' to switch to it.'));
' to switch to it'); // TODO 2 test this
}
}

Expand All @@ -106,10 +97,10 @@ SCWorker.prototype._init = function (options) {
} else {
error = err;
}
self._errorDomain.emit('error', error);
self.emitError(error);
});
this.brokerEngineClient.on('warning', function (warning) {
self.warningHandler(warning);
self.emitWarning(warning);
});
this.exchange = this.global = this.brokerEngineClient.exchange();

Expand All @@ -128,15 +119,14 @@ SCWorker.prototype._init = function (options) {

this.httpServer.exchange = this.httpServer.global = this.exchange;

var httpServerErrorDomain = domain.create();
httpServerErrorDomain.add(this.httpServer);
httpServerErrorDomain.on('error', function (err) {
this.httpServer.on('error', function (err) {
var error;
if (typeof err == 'string') {
error = new HTTPServerError(err);
} else {
error = err;
}
self._errorDomain.emit('error', error);
self.emitError(error);
});

var secure = this.options.protocol == 'https' ? 1 : 0;
Expand Down Expand Up @@ -172,6 +162,9 @@ SCWorker.prototype._init = function (options) {
this.setAuthEngine(new AuthEngine());
this.codec = this.scServer.codec;

this._socketPath = this.scServer.getPath();
this._socketPathRegex = new RegExp('^' + this._socketPath);

this.scServer.on('_connection', function (socket) {
// The connection event counts as a WS request
self._wsRequestCount++;
Expand All @@ -181,15 +174,12 @@ SCWorker.prototype._init = function (options) {
self.emit(self.EVENT_CONNECTION, socket);
});

this.scServer.on('warning', function () {
self.warningHandler.apply(self, arguments);
this.scServer.on('warning', function (warning) {
self.emitWarning(warning);
});
this.scServer.on('error', function (error) {
self.emitError(error);
});

this._socketPath = this.scServer.getPath();
this._socketPathRegex = new RegExp('^' + this._socketPath);

this._errorDomain.add(this.scServer);

this.scServer.on('ready', function () {
self.emit(self.EVENT_READY);
});
Expand Down Expand Up @@ -257,7 +247,7 @@ SCWorker.prototype._startServer = function () {
}
};

SCWorker.prototype._start = function () {
SCWorker.prototype.start = function () {
var self = this;

this._httpRequestCount = 0;
Expand Down Expand Up @@ -335,7 +325,7 @@ SCWorker.prototype._calculateStatus = function () {
message += 'usage of ' + memoryUsage.heapUsed + ' exceeded ';
message += 'the killWorkerMemoryThreshold of ' + memThreshold;
var warning = new ResourceLimitError(message);
this.warningHandler(warning);
this.emitWarning(warning);
process.exit();
}
}
Expand Down Expand Up @@ -415,11 +405,11 @@ SCWorker.prototype.handleMasterMessage = function (message) {
});
};

SCWorker.prototype.errorHandler = function (err) {
SCWorker.prototype.emitError = function (err) {
this.emit(this.EVENT_ERROR, err);
};

SCWorker.prototype.warningHandler = function (warning) {
SCWorker.prototype.emitWarning = function (warning) {
this.emit(this.EVENT_WARNING, warning);
};

Expand Down
Loading

0 comments on commit d709eb3

Please sign in to comment.