1
1
var socketClusterServer = require ( 'socketcluster-server' ) ;
2
2
var EventEmitter = require ( 'events' ) . EventEmitter ;
3
3
var crypto = require ( 'crypto' ) ;
4
- var domain = require ( 'sc-domain' ) ;
5
4
var uuid = require ( 'uuid' ) ;
6
5
var http = require ( 'http' ) ;
7
6
var https = require ( 'https' ) ;
@@ -31,15 +30,7 @@ var SCWorker = function (options) {
31
30
this . type = 'worker' ;
32
31
self . _pendingResponseHandlers = { } ;
33
32
34
- this . _errorDomain = domain . create ( ) ;
35
- this . _errorDomain . on ( 'error' , function ( ) {
36
- self . errorHandler . apply ( self , arguments ) ;
37
- } ) ;
38
-
39
- this . start = this . _errorDomain . bind ( this . _start ) ;
40
- this . _errorDomain . run ( function ( ) {
41
- self . _init ( options ) ;
42
- } ) ;
33
+ this . _init ( options ) ;
43
34
} ;
44
35
45
36
SCWorker . prototype = Object . create ( EventEmitter . prototype ) ;
@@ -77,9 +68,9 @@ SCWorker.prototype._init = function (options) {
77
68
try {
78
69
process . setuid ( this . options . downgradeToUser ) ;
79
70
} catch ( err ) {
80
- this . _errorDomain . emit ( 'error' , new InvalidActionError ( 'Could not downgrade to user "' + this . options . downgradeToUser +
71
+ throw new InvalidActionError ( 'Could not downgrade to user "' + this . options . downgradeToUser +
81
72
'" - Either this user does not exist or the current process does not have the permission' +
82
- ' to switch to it.' ) ) ;
73
+ ' to switch to it' ) ; // TODO 2 test this
83
74
}
84
75
}
85
76
@@ -106,10 +97,10 @@ SCWorker.prototype._init = function (options) {
106
97
} else {
107
98
error = err ;
108
99
}
109
- self . _errorDomain . emit ( 'error' , error ) ;
100
+ self . emitError ( error ) ;
110
101
} ) ;
111
102
this . brokerEngineClient . on ( 'warning' , function ( warning ) {
112
- self . warningHandler ( warning ) ;
103
+ self . emitWarning ( warning ) ;
113
104
} ) ;
114
105
this . exchange = this . global = this . brokerEngineClient . exchange ( ) ;
115
106
@@ -128,15 +119,14 @@ SCWorker.prototype._init = function (options) {
128
119
129
120
this . httpServer . exchange = this . httpServer . global = this . exchange ;
130
121
131
- var httpServerErrorDomain = domain . create ( ) ;
132
- httpServerErrorDomain . add ( this . httpServer ) ;
133
- httpServerErrorDomain . on ( 'error' , function ( err ) {
122
+ this . httpServer . on ( 'error' , function ( err ) {
123
+ var error ;
134
124
if ( typeof err == 'string' ) {
135
125
error = new HTTPServerError ( err ) ;
136
126
} else {
137
127
error = err ;
138
128
}
139
- self . _errorDomain . emit ( 'error' , error ) ;
129
+ self . emitError ( error ) ;
140
130
} ) ;
141
131
142
132
var secure = this . options . protocol == 'https' ? 1 : 0 ;
@@ -172,6 +162,9 @@ SCWorker.prototype._init = function (options) {
172
162
this . setAuthEngine ( new AuthEngine ( ) ) ;
173
163
this . codec = this . scServer . codec ;
174
164
165
+ this . _socketPath = this . scServer . getPath ( ) ;
166
+ this . _socketPathRegex = new RegExp ( '^' + this . _socketPath ) ;
167
+
175
168
this . scServer . on ( '_connection' , function ( socket ) {
176
169
// The connection event counts as a WS request
177
170
self . _wsRequestCount ++ ;
@@ -181,15 +174,12 @@ SCWorker.prototype._init = function (options) {
181
174
self . emit ( self . EVENT_CONNECTION , socket ) ;
182
175
} ) ;
183
176
184
- this . scServer . on ( 'warning' , function ( ) {
185
- self . warningHandler . apply ( self , arguments ) ;
177
+ this . scServer . on ( 'warning' , function ( warning ) {
178
+ self . emitWarning ( warning ) ;
179
+ } ) ;
180
+ this . scServer . on ( 'error' , function ( error ) {
181
+ self . emitError ( error ) ;
186
182
} ) ;
187
-
188
- this . _socketPath = this . scServer . getPath ( ) ;
189
- this . _socketPathRegex = new RegExp ( '^' + this . _socketPath ) ;
190
-
191
- this . _errorDomain . add ( this . scServer ) ;
192
-
193
183
this . scServer . on ( 'ready' , function ( ) {
194
184
self . emit ( self . EVENT_READY ) ;
195
185
} ) ;
@@ -257,7 +247,7 @@ SCWorker.prototype._startServer = function () {
257
247
}
258
248
} ;
259
249
260
- SCWorker . prototype . _start = function ( ) {
250
+ SCWorker . prototype . start = function ( ) {
261
251
var self = this ;
262
252
263
253
this . _httpRequestCount = 0 ;
@@ -335,7 +325,7 @@ SCWorker.prototype._calculateStatus = function () {
335
325
message += 'usage of ' + memoryUsage . heapUsed + ' exceeded ' ;
336
326
message += 'the killWorkerMemoryThreshold of ' + memThreshold ;
337
327
var warning = new ResourceLimitError ( message ) ;
338
- this . warningHandler ( warning ) ;
328
+ this . emitWarning ( warning ) ;
339
329
process . exit ( ) ;
340
330
}
341
331
}
@@ -415,11 +405,11 @@ SCWorker.prototype.handleMasterMessage = function (message) {
415
405
} ) ;
416
406
} ;
417
407
418
- SCWorker . prototype . errorHandler = function ( err ) {
408
+ SCWorker . prototype . emitError = function ( err ) {
419
409
this . emit ( this . EVENT_ERROR , err ) ;
420
410
} ;
421
411
422
- SCWorker . prototype . warningHandler = function ( warning ) {
412
+ SCWorker . prototype . emitWarning = function ( warning ) {
423
413
this . emit ( this . EVENT_WARNING , warning ) ;
424
414
} ;
425
415
0 commit comments