-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
38 lines (31 loc) · 920 Bytes
/
index.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
'use strict';
const fork = require('child_process').fork;
const path = require('path');
const argv = require('process').argv;
const program = path.resolve('server.js');
const options = {
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
};
let child = null;
async function startNewChild() {
child = fork(program, argv, options);
child.on('message', onChildMessage);
child.on('exit', onChildCrash);
}
function onChildMessage(message) {
if (message === 'restart') {
child.removeListener('exit', onChildCrash);
child.kill();
startNewChild();
} else if (message === 'exit') {
child.removeListener('exit', onChildCrash);
child.kill();
}
}
function onChildCrash(exitCode) {
if (exitCode !== 0) {
console.info('server.js has exited unexpectedly, restarting', exitCode);
child = fork(program, argv, options);
}
}
startNewChild();