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);
|
|
|
|
|
2016-06-04 14:58:11 +02:00
|
|
|
function toExternalTask(task, id) {
|
2016-06-04 11:41:57 +02:00
|
|
|
return {
|
2016-06-04 14:58:11 +02:00
|
|
|
id: id,
|
2016-06-04 11:41:57 +02:00
|
|
|
name: task.name,
|
|
|
|
schedule: task.schedule,
|
|
|
|
runningSince: task.runningSince && task.runningSince.unix(),
|
2016-06-05 12:18:37 +02:00
|
|
|
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix(),
|
|
|
|
state: task.state,
|
|
|
|
enabled: task.enabled
|
2016-06-04 11:41:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-05 12:18:37 +02:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:42:17 +02:00
|
|
|
return {
|
|
|
|
getAll: function (req, res) {
|
|
|
|
var tasks = Scheduler.getTasks();
|
2016-06-04 14:58:11 +02:00
|
|
|
return Resources.success(res, _.map(tasks, toExternalTask));
|
2016-06-04 11:41:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
run: function (req, res) {
|
2016-06-05 12:18:37 +02:00
|
|
|
withTask(req, res, function (err, id, task) {
|
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
2016-06-04 11:41:57 +02:00
|
|
|
|
2016-06-05 12:18:37 +02:00
|
|
|
if (task.runningSince) {
|
|
|
|
return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict});
|
|
|
|
}
|
2016-06-04 11:41:57 +02:00
|
|
|
|
2016-06-05 12:18:37 +02:00
|
|
|
task.run();
|
2016-06-04 11:41:57 +02:00
|
|
|
|
2016-06-05 12:18:37 +02:00
|
|
|
return Resources.success(res, toExternalTask(task, id));
|
|
|
|
});
|
|
|
|
},
|
2016-06-04 11:41:57 +02:00
|
|
|
|
2016-06-05 12:18:37 +02:00
|
|
|
enable: function (req, res) {
|
|
|
|
setTaskEnabled(req, res, true);
|
|
|
|
},
|
2016-06-04 11:41:57 +02:00
|
|
|
|
2016-06-05 12:18:37 +02:00
|
|
|
disable: function (req, res) {
|
|
|
|
setTaskEnabled(req, res, false);
|
2016-06-03 23:42:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|