-
Notifications
You must be signed in to change notification settings - Fork 25
/
jobs.js
46 lines (40 loc) · 1.02 KB
/
jobs.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
/* eslint-disable no-console, no-unused-vars */
var config = require('./config.js');
var storage = config.getStorage();
var moment = require('moment');
var jobs = require('require-all')(__dirname + '/jobs');
function update_widget(name, data, next_time) {
console.log("updating widget: " + name);
storage.set(name, JSON.stringify({
payload: data,
next_time: next_time
}), function(err, res) {
if(err) {
console.log(err);
}
});
}
function reschedule(job) {
setTimeout(function() {start_recurring_job(job)}, job.interval)
}
function start_recurring_job(job) {
new Promise(job.promise)
.then(
function(widget_data) {
for (var widget in widget_data) {
update_widget(widget, widget_data[widget], moment().add(job.interval, 'ms'));
}
reschedule(job);
}
)
.catch(
function(error) {
console.log(error);
reschedule(job);
}
);
}
for (var job in jobs) {
console.log("Starting job: " + job)
start_recurring_job(jobs[job]);
}