-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcron.js
55 lines (47 loc) · 1.47 KB
/
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
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const Instance = require('./models/instance');
const PromisePool = require('es6-promise-pool');
const checkInstance = require('./jobs/check_instance');
setTimeout(() => {
console.error('Looks like we stalled!');
process.exit(1);
}, 3600_000);
(async () => {
let aliveThreshold = new Date();
aliveThreshold.setMinutes(aliveThreshold.getMinutes() - 60);
let deadThreshold = new Date();
deadThreshold.setDate(deadThreshold.getDate() - 7);
let instances = await Instance.findAll({
where: {
blacklisted: false,
[Op.or]: [{
latest_check: {
[Op.eq]: null
}
}, {
dead: false,
latest_check: {
[Op.lt]: aliveThreshold
}
}, {
latest_check: {
[Op.lt]: deadThreshold
}
}]
},
order: [['latest_check', 'ASC']],
limit: 10000
});
console.log(`${instances.length} instances to check`);
let start = new Date();
await (new PromisePool(() => {
if(instances.length === 0)
return null;
return checkInstance({
instance: instances.splice(0, 1)[0].id
});
}, 50)).start();
console.log(`Done in ${(new Date().getTime() - start.getTime()) / 1000} s.`);
process.exit(0);
})().catch(console.error);