forked from RoBorregos/sems-vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
47 lines (41 loc) · 1.12 KB
/
run.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
const RESTART_TIME_HOURS = 24;
const RESTART_TIME_MS = RESTART_TIME_HOURS * 60 * 60 * 1000;
const kill = require('tree-kill');
const cproc = require("child_process");
const ERROR = 'ERROR';
let proc; // child.ChildProcess;
let timeoutRef;
function start() {
console.log(__dirname + "/main.py")
proc = cproc.spawn("python3", [__dirname + "/main.py"]);
proc.stdout?.on('data', function(res) {
if (res.toString().indexOf(ERROR) !== -1) {
if (timeoutRef) {
clearTimeout(timeoutRef)
}
restart()
}
console.log(res.toString());
});
proc.stderr?.on('data', function(res) {
if (res.toString().indexOf(ERROR) !== -1) {
if (timeoutRef) {
clearTimeout(timeoutRef)
}
restart()
}
console.log(res.toString());
});
proc.stderr?.on('exit', function(code) {
if (timeoutRef) {
clearTimeout(timeoutRef)
}
restart()
});
timeoutRef = setTimeout(restart, RESTART_TIME_MS);
}
function restart() {
kill(proc.pid);
start();
}
start();