This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
/
index.js
83 lines (67 loc) · 2.08 KB
/
index.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
/* eslint promise/prefer-await-to-then: off */
const ns = require('node-schedule');
const debug = require('debug')('TaskList');
const Task = require('./lib/task');
const schedules = {
daily: '30 23 * * *', // 23:30 daily
weekly: '30 23 * * 7', // 23:30 on Sunday
monthly: '30 23 1 * *' // 23:30 on the first day of the month.
};
// Pretty a time stamp for debug logs
const ts = () =>
new Date().toISOString().replace('T', ' ').slice(0, 19);
class TaskList {
constructor(opts = {}) {
this.schedules = opts.schedules || schedules;
// Creates a map of task names to task instances for each schedule
this.tasks = Object.keys(this.schedules)
.map(name => new Task(name, opts.haltTasksOnError))
.reduce((tasks, task) => {
tasks[task.name] = task;
return tasks;
}, {});
}
heartbeat(callback) {
this._heartbeat = callback;
}
plan(scheduleKey, callback) {
this.tasks[scheduleKey].actions(callback);
}
beforeEach(callback) {
Object.keys(this.tasks).forEach(key => {
const task = this.tasks[key];
task.before(callback);
});
}
afterEach(callback) {
Object.keys(this.tasks).forEach(key => {
const task = this.tasks[key];
task.after(callback);
});
}
invoke() {
Object.entries(this.tasks)
.forEach(([name, task]) => {
const crontab = this.schedules[name];
debug(`[${ts()}] Scheduling ${task.name} with crontab ${crontab}.`);
ns.scheduleJob(crontab, () => {
debug(`[${ts()}] Executing ${task.name}.`);
task.run()
.then(() => {
debug(`[${ts()}] Task ${task.name} completed successfully.`);
})
.catch(err => {
debug(`[${ts()}] Task ${task.name} errored with ${err}.`);
});
});
});
// Schedules the heartbeat
if (this._heartbeat) {
ns.scheduleJob('* * * * *', () => this._heartbeat());
}
const size = Object.keys(this.tasks).length;
debug(`[${ts()}] Scheduled ${size} tasks.`);
}
}
TaskList.schedules = schedules;
module.exports = TaskList;