Run jobs only once at a time. Start an task api.

This commit is contained in:
baldo 2016-06-03 23:42:17 +02:00
parent 5ff76db5a9
commit 638cc94db3
9 changed files with 99 additions and 7 deletions

View file

@ -2,11 +2,13 @@
angular.module('ffffng').factory('MailQueueJob', function (MailService, Logger) { angular.module('ffffng').factory('MailQueueJob', function (MailService, Logger) {
return { return {
run: function () { run: function (callback) {
MailService.sendPendingMails(function (err) { MailService.sendPendingMails(function (err) {
if (err) { if (err) {
Logger.tag('mail', 'queue').error('Error sending pending mails:', err); Logger.tag('mail', 'queue').error('Error sending pending mails:', err);
} }
callback();
}); });
} }
}; };

View file

@ -2,11 +2,13 @@
angular.module('ffffng').factory('MonitoringMailsSendingJob', function (MonitoringService, Logger) { angular.module('ffffng').factory('MonitoringMailsSendingJob', function (MonitoringService, Logger) {
return { return {
run: function () { run: function (callback) {
MonitoringService.sendMonitoringMails(function (err) { MonitoringService.sendMonitoringMails(function (err) {
if (err) { if (err) {
Logger.tag('monitoring', 'mail-sending').error('Error sending monitoring mails:', err); Logger.tag('monitoring', 'mail-sending').error('Error sending monitoring mails:', err);
} }
callback();
}); });
} }
}; };

View file

@ -2,11 +2,13 @@
angular.module('ffffng').factory('NodeInformationCleanupJob', function (MonitoringService, Logger) { angular.module('ffffng').factory('NodeInformationCleanupJob', function (MonitoringService, Logger) {
return { return {
run: function () { run: function (callback) {
MonitoringService.cleanupNodeInformation(function (err) { MonitoringService.cleanupNodeInformation(function (err) {
if (err) { if (err) {
Logger.tag('monitoring', 'information-cleanup').error('Error cleaning up node data:', err); Logger.tag('monitoring', 'information-cleanup').error('Error cleaning up node data:', err);
} }
callback();
}); });
} }
}; };

View file

@ -2,11 +2,13 @@
angular.module('ffffng').factory('NodeInformationRetrievalJob', function (MonitoringService, Logger) { angular.module('ffffng').factory('NodeInformationRetrievalJob', function (MonitoringService, Logger) {
return { return {
run: function () { run: function (callback) {
MonitoringService.retrieveNodeInformation(function (err) { MonitoringService.retrieveNodeInformation(function (err) {
if (err) { if (err) {
Logger.tag('monitoring', 'information-retrieval').error('Error retrieving node data:', err); Logger.tag('monitoring', 'information-retrieval').error('Error retrieving node data:', err);
} }
callback();
}); });
} }
}; };

View file

@ -8,9 +8,18 @@ _.each(jobFiles, function (jobFile) {
require(jobFile); require(jobFile);
}); });
angular.module('ffffng').factory('Scheduler', function ($injector, Logger, config) { angular.module('ffffng').factory('Scheduler', function ($injector, Logger, config, moment) {
var cron = require('node-cron'); var cron = require('node-cron');
var tasks = {};
var taskId = 1;
function nextTaskId() {
var id = taskId;
taskId += 1;
return id;
}
function schedule(expr, jobName) { function schedule(expr, jobName) {
Logger.tag('jobs').info('Scheduling job: %s %s', expr, jobName); Logger.tag('jobs').info('Scheduling job: %s %s', expr, jobName);
@ -20,7 +29,30 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
throw new Error('The job ' + jobName + ' does not provide a "run" function.'); throw new Error('The job ' + jobName + ' does not provide a "run" function.');
} }
cron.schedule(expr, job.run); var id = nextTaskId();
var task = {
name: jobName,
schedule: expr,
job: job,
runningSince: false,
lastRunStarted: false
};
cron.schedule(expr, function () {
if (task.runningSince) {
// job is still running, skip execution
return;
}
task.runningSince = moment();
task.lastRunStarted = task.runningSince;
job.run(function () {
task.runningSince = false;
});
});
tasks['' + id] = task;
} }
return { return {
@ -31,6 +63,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
schedule('0 */1 * * * *', 'MailQueueJob'); schedule('0 */1 * * * *', 'MailQueueJob');
if (config.client.monitoring.enabled) { if (config.client.monitoring.enabled) {
schedule('* * * * * *', 'TestJob');
schedule('30 */15 * * * *', 'NodeInformationRetrievalJob'); schedule('30 */15 * * * *', 'NodeInformationRetrievalJob');
schedule('45 */5 * * * *', 'MonitoringMailsSendingJob'); schedule('45 */5 * * * *', 'MonitoringMailsSendingJob');
schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00 schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00
@ -42,6 +75,10 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
} }
Logger.tag('jobs').info('Scheduling of background jobs done.'); Logger.tag('jobs').info('Scheduling of background jobs done.');
},
getTasks: function () {
return tasks;
} }
}; };
}); });

18
server/jobs/testJob.js Normal file
View file

@ -0,0 +1,18 @@
'use strict';
angular.module('ffffng').factory('TestJob', function (Logger) {
var i = 1;
return {
run: function (callback) {
var j = i;
i += 1;
Logger.tag('test').info('Start test job... ' + j);
setTimeout(function () {
Logger.tag('test').info('Done test job... ' + j);
callback();
}, 2000);
}
};
});

View file

@ -19,6 +19,7 @@ require('./utils/resources');
require('./utils/strings'); require('./utils/strings');
require('./utils/urlBuilder'); require('./utils/urlBuilder');
require('./resources/taskResource');
require('./resources/nodeResource'); require('./resources/nodeResource');
require('./resources/monitoringResource'); require('./resources/monitoringResource');

View file

@ -0,0 +1,21 @@
'use strict';
angular.module('ffffng').factory('TaskResource', function (
_,
Resources,
Scheduler
) {
return {
getAll: function (req, res) {
var tasks = Scheduler.getTasks();
return Resources.success(res, _.map(tasks, function (task) {
return {
name: task.name,
schedule: task.schedule,
runningSince: task.runningSince && task.runningSince.unix(),
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix()
};
}));
}
};
});

View file

@ -1,6 +1,11 @@
'use strict'; 'use strict';
angular.module('ffffng').factory('Router', function (app, NodeResource, MonitoringResource) { angular.module('ffffng').factory('Router', function (
app,
NodeResource,
MonitoringResource,
TaskResource
) {
return { return {
init: function () { init: function () {
app.post('/api/node', NodeResource.create); app.post('/api/node', NodeResource.create);
@ -10,6 +15,8 @@ angular.module('ffffng').factory('Router', function (app, NodeResource, Monitori
app.put('/api/monitoring/confirm/:token', MonitoringResource.confirm); app.put('/api/monitoring/confirm/:token', MonitoringResource.confirm);
app.put('/api/monitoring/disable/:token', MonitoringResource.disable); app.put('/api/monitoring/disable/:token', MonitoringResource.disable);
app.get('/internal/api/task/all', TaskResource.getAll);
} }
}; };
}); });