-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatchdog.js
62 lines (44 loc) · 1.6 KB
/
watchdog.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
const fs = require("fs");
const { exec } = require('child_process');
let interval = 2 //minutes
let filename = 'counter.txt';
let command = 'pm2 reload crawler1';
let counter_value, current_value;
let last_value = 0;
fs.readFile(filename, function(err, buf) {
counter_value = parseInt(buf.toString());
console.log('got initial counter value of '+counter_value);
setInterval(function(){
console.log('checking if counter value increased...');
fs.readFile(filename, function(err_a, buf_a) {
current_value = parseInt(buf_a.toString());
if(isNaN(current_value))
fs.writeFile(filename, last_value, (err)=>{
process.exit(1);
});
else
last_value = current_value;
console.log('got current_value value of '+current_value+' from '+filename);
if(counter_value <= current_value){
console.log('same value. watchdog sets value +1 and restarts crawler');
counter_value++;
fs.writeFile(filename, counter_value, (err) => {
if (err) console.log(err);
console.log("counter updated, restarting proccess");
exec(command, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
});
});
}else{
console.log('values do not match... do nothing', current_value, counter_value);
}
});
},interval*60000);
});