-
Notifications
You must be signed in to change notification settings - Fork 7
/
cron.js
46 lines (36 loc) · 970 Bytes
/
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
// Veeeeeeery simple cron job singleton
// ticks every 1 minute, set a job to go every X ticks.
Cron = function(interval) {
var self = this;
interval = interval || 60 * 1000;
self._jobs = [];
self._schedules = [];
Meteor.setInterval(function() {
self.tick();
}, interval)
}
_.extend(Cron.prototype, {
addJob: function(every_x_ticks, fn) {
this._jobs.push({fn: fn, every: every_x_ticks, count: 0});
},
addScheduleJob: function(unix_time, fn) {
this._schedules.push({fn: fn, unix_time: unix_time});
},
tick: function() {
var self = this;
_.each(self._jobs, function(job) {
job.count += 1;
if (job.count === job.every) {
job.fn();
job.count = 0;
}
});
_.each(self._schedules, function(job, index) {
var ts = Math.round((new Date()).getTime() / 1000);
if (ts >= job.unix_time) {
job.fn();
delete self._schedules[index];
}
});
}
})