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-07 11:01:35 +02:00
|
|
|
function toExternalTask(task) {
|
2016-06-04 11:41:57 +02:00
|
|
|
return {
|
2016-06-07 11:01:35 +02:00
|
|
|
id: task.id,
|
2016-06-04 11:41:57 +02:00
|
|
|
name: task.name,
|
2016-06-21 14:55:02 +02:00
|
|
|
description: task.description,
|
2016-06-04 11:41:57 +02:00
|
|
|
schedule: task.schedule,
|
|
|
|
runningSince: task.runningSince && task.runningSince.unix(),
|
2016-06-05 12:18:37 +02:00
|
|
|
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix(),
|
2017-05-13 12:08:16 +02:00
|
|
|
lastRunDuration: task.lastRunDuration || undefined,
|
2016-06-05 12:18:37 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:01:35 +02:00
|
|
|
callback(null, task);
|
2016-06-05 12:18:37 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTaskEnabled(req, res, enable) {
|
2016-06-07 11:01:35 +02:00
|
|
|
withTask(req, res, function (err, task) {
|
2016-06-05 12:18:37 +02:00
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
task.enabled = !!enable; // ensure boolean
|
|
|
|
|
2016-06-07 11:01:35 +02:00
|
|
|
return Resources.success(res, toExternalTask(task));
|
2016-06-05 12:18:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:42:17 +02:00
|
|
|
return {
|
|
|
|
getAll: function (req, res) {
|
2016-07-23 11:30:41 +02:00
|
|
|
Resources.getValidRestParams('list', null, req, function (err, restParams) {
|
2016-06-07 11:01:35 +02:00
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:58:29 +02:00
|
|
|
var tasks = Resources.sort(
|
|
|
|
_.values(Scheduler.getTasks()),
|
|
|
|
['id', 'name', 'schedule', 'state', 'runningSince', 'lastRunStarted'],
|
|
|
|
restParams
|
|
|
|
);
|
2016-06-07 12:45:23 +02:00
|
|
|
var filteredTasks = Resources.filter(
|
|
|
|
tasks,
|
|
|
|
['id', 'name', 'schedule', 'state'],
|
|
|
|
restParams
|
|
|
|
);
|
|
|
|
var total = filteredTasks.length;
|
2016-06-07 11:01:35 +02:00
|
|
|
|
2016-06-07 12:45:23 +02:00
|
|
|
var pageTasks = Resources.getPageEntities(filteredTasks, restParams);
|
2016-06-07 11:01:35 +02:00
|
|
|
|
|
|
|
res.set('X-Total-Count', total);
|
|
|
|
return Resources.success(res, _.map(pageTasks, toExternalTask));
|
|
|
|
});
|
2016-06-04 11:41:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
run: function (req, res) {
|
2016-06-07 11:01:35 +02:00
|
|
|
withTask(req, res, function (err, task) {
|
2016-06-05 12:18:37 +02:00
|
|
|
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-07 11:01:35 +02:00
|
|
|
return Resources.success(res, toExternalTask(task));
|
2016-06-05 12:18:37 +02:00
|
|
|
});
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|