2016-05-21 17:06:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var glob = require('glob');
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
var jobFiles = glob.sync(__dirname + '/*Job.js');
|
|
|
|
_.each(jobFiles, function (jobFile) {
|
|
|
|
require(jobFile);
|
|
|
|
});
|
|
|
|
|
2016-05-24 19:14:09 +02:00
|
|
|
angular.module('ffffng').factory('Scheduler', function ($injector, Logger, config) {
|
2016-05-21 17:06:24 +02:00
|
|
|
var cron = require('node-cron');
|
|
|
|
|
|
|
|
function schedule(expr, jobName) {
|
2016-05-24 16:40:57 +02:00
|
|
|
Logger.tag('jobs').info('Scheduling job: ' + expr + ' ' + jobName);
|
|
|
|
|
2016-05-21 17:06:24 +02:00
|
|
|
var job = $injector.get(jobName);
|
|
|
|
|
|
|
|
if (!_.isFunction(job.run)) {
|
|
|
|
throw new Error('The job ' + jobName + ' does not provide a "run" function.');
|
|
|
|
}
|
|
|
|
|
|
|
|
cron.schedule(expr, job.run);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: function () {
|
2016-05-24 16:40:57 +02:00
|
|
|
Logger.tag('jobs').info('Scheduling background jobs...');
|
|
|
|
|
|
|
|
try {
|
|
|
|
schedule('0 */1 * * * *', 'MailQueueJob');
|
2016-05-24 19:14:09 +02:00
|
|
|
|
|
|
|
if (config.client.monitoring.enabled) {
|
2016-05-24 20:29:28 +02:00
|
|
|
schedule('30 */5 * * * *', 'NodeInformationRetrievalJob');
|
2016-05-24 20:39:51 +02:00
|
|
|
schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00
|
2016-05-24 19:14:09 +02:00
|
|
|
}
|
2016-05-24 16:40:57 +02:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('jobs').error('Error during scheduling of background jobs:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.tag('jobs').info('Scheduling of background jobs done.');
|
2016-05-21 17:06:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|