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-06-03 23:42:17 +02:00
|
|
|
angular.module('ffffng').factory('Scheduler', function ($injector, Logger, config, moment) {
|
2016-05-21 17:06:24 +02:00
|
|
|
var cron = require('node-cron');
|
|
|
|
|
2016-06-03 23:42:17 +02:00
|
|
|
var tasks = {};
|
|
|
|
|
|
|
|
var taskId = 1;
|
|
|
|
function nextTaskId() {
|
|
|
|
var id = taskId;
|
|
|
|
taskId += 1;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-05-21 17:06:24 +02:00
|
|
|
function schedule(expr, jobName) {
|
2016-05-24 21:08:34 +02:00
|
|
|
Logger.tag('jobs').info('Scheduling job: %s %s', expr, jobName);
|
2016-05-24 16:40:57 +02:00
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:42:17 +02:00
|
|
|
var id = nextTaskId();
|
|
|
|
var task = {
|
2016-06-07 11:01:35 +02:00
|
|
|
id: id,
|
2016-06-03 23:42:17 +02:00
|
|
|
name: jobName,
|
2016-06-21 14:55:02 +02:00
|
|
|
description: job.description,
|
2016-06-03 23:42:17 +02:00
|
|
|
schedule: expr,
|
|
|
|
job: job,
|
|
|
|
runningSince: false,
|
2016-06-05 12:18:37 +02:00
|
|
|
lastRunStarted: false,
|
|
|
|
state: 'idle',
|
|
|
|
enabled: true
|
2016-06-03 23:42:17 +02:00
|
|
|
};
|
|
|
|
|
2016-06-04 11:41:57 +02:00
|
|
|
task.run = function () {
|
2016-06-05 12:18:37 +02:00
|
|
|
if (task.runningSince || !task.enabled) {
|
2016-06-03 23:42:17 +02:00
|
|
|
// job is still running, skip execution
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.runningSince = moment();
|
|
|
|
task.lastRunStarted = task.runningSince;
|
2016-06-05 12:18:37 +02:00
|
|
|
task.state = 'running';
|
2016-06-03 23:42:17 +02:00
|
|
|
|
|
|
|
job.run(function () {
|
2016-07-29 12:53:20 +02:00
|
|
|
var now = moment();
|
|
|
|
Logger.tag('jobs').profile('[%sms]\t%s', now.diff(task.runningSince), task.name);
|
2016-06-03 23:42:17 +02:00
|
|
|
task.runningSince = false;
|
2016-06-05 12:18:37 +02:00
|
|
|
task.state = 'idle';
|
2016-06-03 23:42:17 +02:00
|
|
|
});
|
2016-06-04 11:41:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
cron.schedule(expr, task.run);
|
2016-06-03 23:42:17 +02:00
|
|
|
|
|
|
|
tasks['' + id] = task;
|
2016-05-21 17:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: function () {
|
2016-05-24 16:40:57 +02:00
|
|
|
Logger.tag('jobs').info('Scheduling background jobs...');
|
|
|
|
|
|
|
|
try {
|
|
|
|
schedule('0 */1 * * * *', 'MailQueueJob');
|
2016-06-21 15:21:08 +02:00
|
|
|
schedule('15 */1 * * * *', 'FixNodeFilenamesJob');
|
2016-05-24 19:14:09 +02:00
|
|
|
|
|
|
|
if (config.client.monitoring.enabled) {
|
2016-05-24 23:32:04 +02:00
|
|
|
schedule('30 */15 * * * *', 'NodeInformationRetrievalJob');
|
|
|
|
schedule('45 */5 * * * *', 'MonitoringMailsSendingJob');
|
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-06-03 23:42:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getTasks: function () {
|
|
|
|
return tasks;
|
2016-05-21 17:06:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|