-
Notifications
You must be signed in to change notification settings - Fork 13
/
leo-cli-cron.js
190 lines (165 loc) · 4.77 KB
/
leo-cli-cron.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
const leo = require("leo-sdk");
const async = require("async");
const fs = require("fs");
const path = require("path");
var program = require('commander');
var dynamodb = leo.aws.dynamodb;
var cp = require("child_process");
program
.version('0.0.1')
.option("--regex [regex]", "Flag indicating the ids is a regex")
.option("--regexFlags [regexFlags]", "Regex flags")
.option("--runner [runner]", "Runner file to call when a bot is invoked")
.option("--poll [poll]", "Poll duration in seconds")
.usage('<id> [options]')
.action(function (data) {
var bots = {};
var timeout = null;
let regexExp = program.regex ? data : `^${data}$`;
let regexFlags = program.regexFlags || "i";
let runner = program.runner || path.resolve(__dirname, "./lib/defaultCronRunner.js");
let regex = new RegExp(regexExp, regexFlags);
console.log(regex, regexExp, regexFlags, runner)
let cache = {
_instances: {},
_db: {},
_remoteCode: true
};
let hooks = {};
if (fs.existsSync(`./cron-hooks.js`)) {
hooks = Object.assign(hooks, require(`./cron-hooks.js`));
}
console.log("Looking for Managed Bots", regex);
console.log("Using Cron Table:", leo.configuration.resources.LeoCron);
function findNewBots(callback) {
dynamodb.scan(leo.configuration.resources.LeoCron, {}, (err, items) => {
async.each(items.filter(c => {
return c.id.match(regex);
}), (cron, done) => {
if (!cron.time && (!cron.triggers || cron.triggers.length == 0)) {
cron.trigger = Date.now();
}
//cache._instances[cron.id] = Object.assign(cache._instances[cron.id] || {}, cron.instances, cache._db[cron.id]);
if (!(cron.id in bots) || !bots[cron.id].running) {
var shouldRun = leo.bot.shouldRun(bots[cron.id] && bots[cron.id].cron, cron, cache, (result) => {
if (result && result.value) {
createBot(cron, done);
} else {
if (result && !result.value) {
console.log(`${cron.id} not triggered to run`)
}
done();
}
});
} else {
let bot = bots[cron.id];
if (cron.paused) {
kill(bot.proc);
done();
} else {
updateBot(cron, done);
}
}
}, (err) => {
if (callback) {
callback();
}
})
});
return this;
}
findNewBots();
var interval = setInterval(findNewBots, 1000 * (program.poll || 10));
//do something when app is closing
process.on('exit', () => {
clearInterval(interval);
});
//catches ctrl+c event
process.on('SIGINT', () => {
clearInterval(interval);
});
//catches uncaught exceptions
process.on('uncaughtException', (err) => {
console.log("Error", err)
clearInterval(interval);
});
function getSettings(cron, callback) {
leo.bot.buildPayloads(cron, {}, {
instances: "0"
}).then(r => {
r[0].__cron.lambdaName = cron.lambdaName;
r[0].__cron.force = true;
callback(null, r[0]);
}).catch(callback);
}
function createBot(cron, callback) {
getSettings(cron, (err, settings) => {
if (err) {
return callback(err);
}
var bot = bots[cron.id];
if (!bot) {
bot = {
cron: cron,
proc: cp.fork(runner, [cron.id]),
};
if (!fs.existsSync(path.resolve(`/tmp/log`))) {
fs.mkdirSync(path.resolve(`/tmp/log`));
}
//let stdout = console.log//fs.createWriteStream(path.resolve(`/tmp/log/${cron.id}.log`));
//let stderr = fs.createWriteStream(path.resolve(`/tmp/log/${cron.id}.log`));
//bot.proc.stdout.on("data", d=>console.log(d));
//bot.proc.stderr.on("data", d=>console.error(d));
bot.proc.on("exit", function () {
delete bots[cron.id];
console.log(cron.id, "exited");
//stdout.end();
});
bot.proc.on("message", (msg) => {
if (msg.action == "complete") {
console.log(cron.id, "exited");
bot.running = false;
}
});
bots[cron.id] = bot;
}
bot.running = true;
bot.proc.send({
action: 'start',
cron: settings
});
callback(null, bot);
})
}
function updateBot(cron, callback) {
var bot = bots[cron.id];
async.map({ last: bot.cron, settings: cron }, getSettings, (err, results) => {
let last = results.last;
let settings = results.settings;
let command = hooks.OnUpdate && hooks.OnUpdate(last, settings, bot.proc);
if (command == "restart") {
kill(bot.proc, () => {
console.log("Should restart the bot", cron.id);
createBot(cron, callback);
});
} else {
bot.proc.send({
action: 'update',
cron: settings
});
callback();
}
});
}
function kill(proc, callback) {
if (callback) {
proc.on("exit", callback)
}
proc.kill('SIGINT');
}
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}