-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.js
45 lines (37 loc) · 1.34 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
const later = require('later'), async = require('async');
var backup = require('./worker');
let {databases, jobs} = require('./data');
let updateJobs = () => io.emit('updateJobs', jobs);
async.forever(
function(next) {
// next is suitable for passing to things that need a callback(err [, whatever]);
// it will result in this function being called again.
var now = Date.now();
async.each(jobs.value(), (job, done) => {
let {database, cron, id, next} = job;
if (next) {
if (next < now && (now - next) > 1000) {
console.log('run');
var s = later.parse.cron(cron, false);
jobs.find({id}).assign({next:later.schedule(s).next().getTime()}).value();
updateJobs();
backup.push(database, done);
} else {
done()
}
} else {
console.log('init');
updateJobs();
var s = later.parse.cron(cron, true);
jobs.find({id}).assign({next:later.schedule(s).next().getTime()}).value()
done()
}
}, err => {
setTimeout(next, 1000);
})
},
function(err) {
// if next is called with a value in its first parameter, it will appear
// in here as 'err', and execution will stop.
}
);