@@ -248,79 +248,115 @@ var rpcPublic = {
248248 }
249249} ;
250250
251- amqper . connect ( config . rabbit , function ( ) {
252- amqper . asRpcClient ( function ( rpcClnt ) {
253- rpcClient = rpcClnt ;
254- log . info ( 'portal initializing as rpc client ok' ) ;
255- joinCluster ( function ( id ) {
256- log . info ( 'portal join cluster ok, with rpcID:' , id ) ;
257- amqper . asRpcServer ( id , rpcPublic , function ( rpcSvr ) {
258- log . info ( 'portal initializing as rpc server ok' ) ;
259- amqper . asMonitor ( function ( data ) {
260- if ( data . reason === 'abnormal' || data . reason === 'error' || data . reason === 'quit' ) {
261- if ( portal !== undefined ) {
262- if ( data . message . purpose === 'conference' ) {
263- return portal . getParticipantsByController ( data . message . type , data . message . id )
264- . then ( function ( impactedParticipants ) {
265- impactedParticipants . forEach ( function ( participantId ) {
266- log . error ( 'Fault on conference controller(type:' , data . message . type , 'id:' , data . message . id , ') of participant' , participantId , 'was detected, drop it.' ) ;
267- socketio_server && socketio_server . drop ( participantId ) ;
251+ // Mutiple process setup
252+ var cluster = require ( "cluster" ) ;
253+ var numCPUs = 4 ;
254+ var rpcID ;
255+
256+ if ( cluster . isMaster ) {
257+ // Master Process
258+ amqper . connect ( config . rabbit , function ( ) {
259+ amqper . asRpcClient ( function ( rpcClnt ) {
260+ rpcClient = rpcClnt ;
261+ log . info ( 'portal initializing as rpc client ok' ) ;
262+ joinCluster ( function ( id ) {
263+ rpcID = id ;
264+ log . info ( 'portal join cluster ok, with rpcID:' , id ) ;
265+ amqper . asRpcServer ( id , rpcPublic , function ( rpcSvr ) {
266+ log . info ( 'portal initializing as rpc server ok' ) ;
267+ amqper . asMonitor ( function ( data ) {
268+ if ( data . reason === 'abnormal' || data . reason === 'error' || data . reason === 'quit' ) {
269+ if ( portal !== undefined ) {
270+ if ( data . message . purpose === 'conference' ) {
271+ return portal . getParticipantsByController ( data . message . type , data . message . id )
272+ . then ( function ( impactedParticipants ) {
273+ impactedParticipants . forEach ( function ( participantId ) {
274+ log . error ( 'Fault on conference controller(type:' , data . message . type , 'id:' , data . message . id , ') of participant' , participantId , 'was detected, drop it.' ) ;
275+ socketio_server && socketio_server . drop ( participantId ) ;
276+ } ) ;
268277 } ) ;
269- } ) ;
278+ }
270279 }
271280 }
272- }
273- } , function ( monitor ) {
274- log . info ( id + ' as monitor ready' ) ;
275- getTokenKey ( id , function ( tokenKey ) {
276- startServers ( id , tokenKey ) ;
277- } , function ( ) {
278- log . error ( 'portal getting token failed.' ) ;
281+ } , function ( monitor ) {
282+ log . info ( id + ' as monitor ready' ) ;
283+ for ( var i = 0 ; i < numCPUs ; i ++ ) {
284+ cluster . fork ( ) ;
285+ }
286+
287+ cluster . on ( 'exit' , function ( worker , code , signal ) {
288+ log . info ( `Worker ${ worker . process . pid } died` ) ;
289+ } ) ;
290+ } , function ( reason ) {
291+ log . error ( 'portal initializing as monitor failed, reason:' , reason ) ;
279292 stopServers ( ) ;
280293 process . exit ( ) ;
281294 } ) ;
282- } , function ( reason ) {
283- log . error ( 'portal initializing as rpc client failed, reason:' , reason ) ;
284- stopServers ( ) ;
285- process . exit ( ) ;
286- } ) ;
287- } , function ( reason ) {
288- log . error ( 'portal initializing as rpc client failed, reason:' , reason ) ;
289- stopServers ( ) ;
290- process . exit ( ) ;
295+ } , function ( reason ) {
296+ log . error ( 'portal initializing as rpc server failed, reason:' , reason ) ;
297+ stopServers ( ) ;
298+ process . exit ( ) ;
299+ } ) ;
291300 } ) ;
301+ } , function ( reason ) {
302+ log . error ( 'portal initializing as rpc client failed, reason:' , reason ) ;
303+ stopServers ( ) ;
304+ process . exit ( ) ;
292305 } ) ;
293306 } , function ( reason ) {
294- log . error ( 'portal initializing as rpc client failed, reason:' , reason ) ;
295- stopServers ( ) ;
296- process . exit ( ) ;
307+ log . error ( 'portal connect to rabbitMQ server failed, reason:' , reason ) ;
308+ process . exit ( ) ;
297309 } ) ;
298- } , function ( reason ) {
299- log . error ( 'portal connect to rabbitMQ server failed, reason:' , reason ) ;
300- process . exit ( ) ;
301- } ) ;
302310
303- [ 'SIGINT' , 'SIGTERM' ] . map ( function ( sig ) {
304- process . on ( sig , async function ( ) {
305- log . warn ( 'Exiting on' , sig ) ;
311+ [ 'SIGINT' , 'SIGTERM' ] . map ( function ( sig ) {
312+ process . on ( sig , async function ( ) {
313+ log . warn ( 'Master exiting on' , sig ) ;
314+ stopServers ( ) ;
315+ amqper . disconnect ( ) ;
316+ process . exit ( ) ;
317+ } ) ;
318+ } ) ;
319+
320+ process . on ( 'SIGPIPE' , function ( ) {
321+ log . warn ( 'SIGPIPE!!' ) ;
322+ } ) ;
323+
324+ process . on ( 'exit' , function ( ) {
325+ log . info ( 'Master Process exit' ) ;
326+ } ) ;
327+
328+ process . on ( 'unhandledRejection' , ( reason ) => {
329+ log . info ( 'Reason: ' + reason ) ;
330+ } ) ;
331+
332+ process . on ( 'SIGUSR2' , function ( ) {
333+ logger . reconfigure ( ) ;
334+ } ) ;
335+ } else {
336+ // Worker Process
337+ getTokenKey ( rpcID , function ( tokenKey ) {
338+ startServers ( rpcID , tokenKey ) ;
339+ } , function ( ) {
340+ log . error ( 'portal getting token failed.' ) ;
306341 stopServers ( ) ;
307- amqper . disconnect ( ) ;
308342 process . exit ( ) ;
309343 } ) ;
310- } ) ;
311344
312- process . on ( 'SIGPIPE' , function ( ) {
313- log . warn ( 'SIGPIPE!!' ) ;
314- } ) ;
345+ log . info ( `Worker ${ process . pid } started` ) ;
315346
316- process . on ( 'exit' , function ( ) {
317- log . info ( 'Process exit' ) ;
318- } ) ;
347+ [ 'SIGINT' , 'SIGTERM' ] . map ( function ( sig ) {
348+ process . on ( sig , async function ( ) {
349+ log . warn ( 'Worker exiting on' , sig ) ;
350+ stopServers ( ) ;
351+ process . exit ( ) ;
352+ } ) ;
353+ } ) ;
319354
320- process . on ( 'unhandledRejection ' , ( reason ) => {
321- log . info ( 'Reason: ' + reason ) ;
322- } ) ;
355+ process . on ( 'exit ' , function ( ) {
356+ log . info ( 'Process exit' ) ;
357+ } ) ;
323358
324- process . on ( 'SIGUSR2' , function ( ) {
325- logger . reconfigure ( ) ;
326- } ) ;
359+ process . on ( 'SIGUSR2' , function ( ) {
360+ logger . reconfigure ( ) ;
361+ } ) ;
362+ }
0 commit comments