-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.js
executable file
·88 lines (56 loc) · 1.85 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var CHILD_EXIT_LIMIT = 5,
CHILD_EXIT_THRESHOLD = 60000, //1 minute
childProcess = require( 'child_process' ),
pkg = require( './package.json' ),
appFile = '/app/app.js',
child,
childExits = [];
if( !process.env.NODE_ENV || process.env.NODE_ENV === 'development' ){
appFile = ( '/src/' + appFile );
}
function createChildProcess(){
child = childProcess.fork( __dirname + appFile );
console.info( 'Child process created, pid: ' + child.pid );
child.on( 'exit', handleChildExit );
}
function handleChildExit( e ){
var now = Date.now(),
exitsInLastMinute = 0;
console.log( 'Child exits so far: %d', childExits.length );
if( childExits.length >= CHILD_EXIT_LIMIT ){
childExits.slice( -CHILD_EXIT_LIMIT ).forEach( function( item, index, array ){
var delta = now - item;
if( delta < CHILD_EXIT_THRESHOLD ){
exitsInLastMinute++;
}
} );
}
console.log( 'Child exits in last minute: %d', exitsInLastMinute );
if( exitsInLastMinute >= CHILD_EXIT_LIMIT ){
console.log( 'Too many exits within the last minute, not spawning new child' );
} else {
console.error( 'Process terminated, restarting...', e );
childExits.push( Date.now() );
createChildProcess();
}
}
function createErrorHandler( type ){
return function(){
console.log( 'Parent event: %s', type );
if( child ){
child.kill();
child = null;
}
process.exit( 0 );
};
}
process.on( 'unhandledException', createErrorHandler( 'unhandled exception' ) );
process.on( 'SIGTERM', createErrorHandler( 'SIGTERM' ) );
process.on( 'SIGINT', createErrorHandler( 'SIGINT' ) );
process.on( 'SIGQUIT', createErrorHandler( 'SIGQUIT' ) );
process.on( 'exit', createErrorHandler( 'exit' ) );
process.on( 'message', function( msg ){
console.log( 'server got message: ' + msg );
} );
console.log( 'Starting ' + pkg.name + ', version: ' + pkg.version );
createChildProcess();