-
Notifications
You must be signed in to change notification settings - Fork 6
/
run-bot.js
72 lines (54 loc) · 1.59 KB
/
run-bot.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
var exec = require('child_process').exec;
function run() {
var coffeeProcess = exec('go run main.go', {
cwd: '/Users/maiavinicius/go/src/github.com/MaiaVinicius/wabot'
});
coffeeProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
getKillOrder(function (kill) {
if (kill) {
fs.unlink('storage/logs/kill.json', function () {
console.log("FORCE_KILL");
coffeeProcess.kill(null, 'SIGINT');
});
}
});
});
}
function getKillOrder(cb) {
fs.readFile('storage/logs/kill.json', function (err, data) {
if (!err) {
data = JSON.parse(data);
if (data.kill) {
console.log(data.kill)
cb(true);
}
}
})
}
function saveExecution(pid) {
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
var data = {
datetime: dateTime,
running: true,
pid: pid
};
fs.writeFile('storage/logs/execution.json', JSON.stringify(data), function (err) {
if (err) throw err;
console.log('Execution file saved!');
});
}
var cron = require('node-cron');
const fs = require("fs");
saveExecution()
setInterval(function () {
saveExecution()
}, 25000);
run();
cron.schedule('*/12 * * * *', () => {
run()
// console.log("run")
});