-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
73 lines (57 loc) · 1.86 KB
/
cli.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
//cheerio or orm2 are causing memory leaks,
// so restart the app when it self-closes with a special exit code
const restoreCursor = require('restore-cursor');
const spawn = require('child_process').spawn
const constants = require('./lib/constants')
const LOG_PREFIX = 'PROCESS MONITOR'
const log = require('./lib/logger').prefix(LOG_PREFIX);
const errlog = require('./lib/logger').error(LOG_PREFIX);
var child;
var timeout;
function spawnProcess(isRespawned) {
//log(process.argv)
//process.exit();
//https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
var args = process.argv.slice(2)
if (isRespawned) {
args.push('--respawned')
}
child = spawn('node', ['--expose-gc', '--optimize_for_size', 'runner.js'].concat(args), {
stdio: [process.stdin, process.stdout, process.stderr]
})
var handled = false;
child.on('error', function (err) {
errlog('Failed to start child process.', err, err.stack);
});
function _onClose(code, signal) {
log('CLI EXITED WITH', code)
//TODO start parsing when code is 200
if (code === 100) {
log('*** RESTART ***')
clearTimeout(timeout)
timeout = setTimeout(function () {
spawnProcess(true)
}, 500)
} else {
child.removeListener('close', _onClose)
restoreCursor();
process.stdin.end()
}
}
child.on('close', _onClose)
//catches ctrl+c event
//child.stdout.on('data', function (data) {
// console.log(data.toString());
//});
//
//child.stderr.on('data', function (data) {
// console.log(data.toString());
//});
}
process.on('SIGINT', function () {
if (child) {
log('*** CLOSE CHILD PROCESS ***')
child.kill('SIGINT')
}
});
spawnProcess()