-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·41 lines (32 loc) · 1.1 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
const cron = require('node-cron');
module.exports = function (sails) {
return {
jobs: {},
defaults: { cron: {} },
initialize: function (cb) {
const config = sails.config.cron;
const jobs = Object.keys(config);
sails.on('ready', () => {
jobs.forEach(job => {
if (!cron.validate(config[job].schedule)) {
sails.log.error(`Cron (${job})'s schedule (${config[job].schedule}) is invalid.`);
} else if (!config[job].onTick) {
sails.log.error(`Cron (${job}) 'onTick' function is required for cron task.`);
} else {
let options = {};
if (config[job].timezone) { options.timezone = config[job].timezone; }
if (config[job].start === false) { options.scheduled = false; }
this.jobs[job] = cron.schedule(
config[job].schedule,
config[job].onTick,
options);
if (config[job].runOnInit === true) {
config[job].onTick();
}
}
});
});
cb();
}
};
};