Added possibility to enable / disable tasks.

This commit is contained in:
baldo 2016-06-05 12:18:37 +02:00
parent 8f8d78d1df
commit 53a0ecd366
7 changed files with 109 additions and 46 deletions

View file

@ -35,20 +35,24 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
schedule: expr,
job: job,
runningSince: false,
lastRunStarted: false
lastRunStarted: false,
state: 'idle',
enabled: true
};
task.run = function () {
if (task.runningSince) {
if (task.runningSince || !task.enabled) {
// job is still running, skip execution
return;
}
task.runningSince = moment();
task.lastRunStarted = task.runningSince;
task.state = 'running';
job.run(function () {
task.runningSince = false;
task.state = 'idle';
});
};
@ -65,7 +69,6 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
schedule('0 */1 * * * *', 'MailQueueJob');
if (config.client.monitoring.enabled) {
schedule('* * * * * *', 'TestJob');
schedule('30 */15 * * * *', 'NodeInformationRetrievalJob');
schedule('45 */5 * * * *', 'MonitoringMailsSendingJob');
schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00

View file

@ -1,18 +0,0 @@
'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

@ -17,10 +17,61 @@ angular.module('ffffng').factory('TaskResource', function (
name: task.name,
schedule: task.schedule,
runningSince: task.runningSince && task.runningSince.unix(),
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix()
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix(),
state: task.state,
enabled: task.enabled
};
}
function withValidTaskId(req, res, callback) {
var id = Strings.normalizeString(Resources.getData(req).id);
if (!isValidId(id)) {
return callback({data: 'Invalid task id.', type: ErrorTypes.badRequest});
}
callback(null, id);
}
function getTask(id, callback) {
var tasks = Scheduler.getTasks();
var task = tasks[id];
if (!task) {
return callback({data: 'Task not found.', type: ErrorTypes.notFound});
}
callback(null, task);
}
function withTask(req, res, callback) {
withValidTaskId(req, res, function (err, id) {
if (err) {
return callback(err);
}
getTask(id, function (err, task) {
if (err) {
return callback(err);
}
callback(null, id, task);
});
});
}
function setTaskEnabled(req, res, enable) {
withTask(req, res, function (err, id, task) {
if (err) {
return Resources.error(res, err);
}
task.enabled = !!enable; // ensure boolean
return Resources.success(res, toExternalTask(task, id));
});
}
return {
getAll: function (req, res) {
var tasks = Scheduler.getTasks();
@ -28,26 +79,27 @@ angular.module('ffffng').factory('TaskResource', function (
},
run: function (req, res) {
var id = Strings.normalizeString(Resources.getData(req).id);
withTask(req, res, function (err, id, task) {
if (err) {
return Resources.error(res, err);
}
if (!isValidId(id)) {
return Resources.error(res, {data: 'Invalid task id.', type: ErrorTypes.badRequest});
}
if (task.runningSince) {
return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict});
}
var tasks = Scheduler.getTasks();
var task = tasks[id];
task.run();
if (!task) {
return Resources.error(res, {data: 'Task not found.', type: ErrorTypes.notFound});
}
return Resources.success(res, toExternalTask(task, id));
});
},
if (task.runningSince) {
return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict});
}
enable: function (req, res) {
setTaskEnabled(req, res, true);
},
task.run();
return Resources.success(res, toExternalTask(task, id));
disable: function (req, res) {
setTaskEnabled(req, res, false);
}
};
});

View file

@ -18,6 +18,8 @@ angular.module('ffffng').factory('Router', function (
app.get('/internal/api/tasks', TaskResource.getAll);
app.put('/internal/api/tasks/run/:id', TaskResource.run);
app.put('/internal/api/tasks/enable/:id', TaskResource.enable);
app.put('/internal/api/tasks/disable/:id', TaskResource.disable);
}
};
});