2016-06-03 23:42:17 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('ffffng').factory('TaskResource', function (
|
2016-06-04 11:41:57 +02:00
|
|
|
Constraints,
|
|
|
|
Validator,
|
2016-06-03 23:42:17 +02:00
|
|
|
_,
|
2016-06-04 11:41:57 +02:00
|
|
|
Strings,
|
2016-06-03 23:42:17 +02:00
|
|
|
Resources,
|
2016-06-04 11:41:57 +02:00
|
|
|
ErrorTypes,
|
2016-06-03 23:42:17 +02:00
|
|
|
Scheduler
|
|
|
|
) {
|
2016-06-04 11:41:57 +02:00
|
|
|
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()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:42:17 +02:00
|
|
|
return {
|
|
|
|
getAll: function (req, res) {
|
|
|
|
var tasks = Scheduler.getTasks();
|
2016-06-04 11:41:57 +02:00
|
|
|
return Resources.success(res, _.mapValues(tasks, toExternalTask));
|
|
|
|
},
|
|
|
|
|
|
|
|
run: function (req, res) {
|
|
|
|
var id = Strings.normalizeString(Resources.getData(req).id);
|
|
|
|
|
|
|
|
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));
|
2016-06-03 23:42:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|