-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler.js
45 lines (42 loc) · 1.02 KB
/
scheduler.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 configList = require("./config");
const helper = require("./helper");
const schedule = require("node-schedule");
const parser = require("cron-parser");
const setRule = (rule, config) => {
const s = schedule.scheduleJob(rule, () => {
helper.backup(
config.db,
config.path,
config.email,
config.password,
config.project
);
});
return s;
};
const runConfig = config => {
config.schedules.forEach(sch => {
let rule = null;
if (typeof sch === "string") {
try {
parser.parseExpression(sch);
} catch (err) {
throw new Error("Not a valid cron expression");
}
rule = sch;
} else if (typeof sch === "object") {
rule = new schedule.RecurrenceRule();
for (const r in sch) {
if (Object.prototype.hasOwnProperty.call(sch, r)) {
rule[r] = sch[r];
}
}
} else {
throw new Error("Not a valid schedule");
}
setRule(rule, config);
});
};
configList.forEach(config => {
runConfig(config);
});