Added API call to run task immediately.

This commit is contained in:
baldo 2016-06-04 11:41:57 +02:00
parent 638cc94db3
commit bee528f1b8
4 changed files with 48 additions and 10 deletions

View file

@ -38,7 +38,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
lastRunStarted: false lastRunStarted: false
}; };
cron.schedule(expr, function () { task.run = function () {
if (task.runningSince) { if (task.runningSince) {
// job is still running, skip execution // job is still running, skip execution
return; return;
@ -50,7 +50,9 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
job.run(function () { job.run(function () {
task.runningSince = false; task.runningSince = false;
}); });
}); };
cron.schedule(expr, task.run);
tasks['' + id] = task; tasks['' + id] = task;
} }

View file

@ -1,21 +1,52 @@
'use strict'; 'use strict';
angular.module('ffffng').factory('TaskResource', function ( angular.module('ffffng').factory('TaskResource', function (
Constraints,
Validator,
_, _,
Strings,
Resources, Resources,
ErrorTypes,
Scheduler Scheduler
) { ) {
var isValidId = Validator.forConstraint(Constraints.id);
function toExternalTask(task) {
return {
name: task.name,
schedule: task.schedule,
runningSince: task.runningSince && task.runningSince.unix(),
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix()
};
}
return { return {
getAll: function (req, res) { getAll: function (req, res) {
var tasks = Scheduler.getTasks(); var tasks = Scheduler.getTasks();
return Resources.success(res, _.map(tasks, function (task) { return Resources.success(res, _.mapValues(tasks, toExternalTask));
return { },
name: task.name,
schedule: task.schedule, run: function (req, res) {
runningSince: task.runningSince && task.runningSince.unix(), var id = Strings.normalizeString(Resources.getData(req).id);
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix()
}; if (!isValidId(id)) {
})); return Resources.error(res, {data: 'Invalid task id.', type: ErrorTypes.badRequest});
}
var tasks = Scheduler.getTasks();
var task = tasks[id];
if (!task) {
return Resources.error(res, {data: 'Task not found.', type: ErrorTypes.notFound});
}
if (task.runningSince) {
return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict});
}
task.run();
return Resources.success(res, toExternalTask(task));
} }
}; };
}); });

View file

@ -17,6 +17,7 @@ angular.module('ffffng').factory('Router', function (
app.put('/api/monitoring/disable/:token', MonitoringResource.disable); app.put('/api/monitoring/disable/:token', MonitoringResource.disable);
app.get('/internal/api/task/all', TaskResource.getAll); app.get('/internal/api/task/all', TaskResource.getAll);
app.put('/internal/api/task/run/:id', TaskResource.run);
} }
}; };
}); });

View file

@ -2,6 +2,10 @@
angular.module('ffffng').factory('Constraints', function () { angular.module('ffffng').factory('Constraints', function () {
return { return {
id:{
regex: /^[1-9][0-9]*/,
optional: false
},
token:{ token:{
regex: /^[0-9a-f]{16}$/i, regex: /^[0-9a-f]{16}$/i,
optional: false optional: false