ffffng/server/resources/taskResource.js

127 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
2018-12-17 22:49:54 +01:00
const _ = require('lodash')
const Constraints = require('../validation/constraints')
2018-12-17 22:49:54 +01:00
const ErrorTypes = require('../utils/errorTypes')
const Resources = require('../utils/resources')
const Scheduler = require('../jobs/scheduler')
const Strings = require('../utils/strings')
const Validator = require('../validation/validator')
2018-12-17 22:49:54 +01:00
const isValidId = Validator.forConstraint(Constraints.id);
2018-12-17 22:49:54 +01:00
function toExternalTask(task) {
return {
id: task.id,
name: task.name,
description: task.description,
schedule: task.schedule,
runningSince: task.runningSince && task.runningSince.unix(),
lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix(),
lastRunDuration: task.lastRunDuration || undefined,
state: task.state,
enabled: task.enabled
};
}
function withValidTaskId(req, res, callback) {
const id = Strings.normalizeString(Resources.getData(req).id);
if (!isValidId(id)) {
return callback({data: 'Invalid task id.', type: ErrorTypes.badRequest});
}
2018-12-17 22:49:54 +01:00
callback(null, id);
}
2018-12-17 22:49:54 +01:00
function getTask(id, callback) {
const tasks = Scheduler.getTasks();
const task = tasks[id];
2018-12-17 22:49:54 +01:00
if (!task) {
return callback({data: 'Task not found.', type: ErrorTypes.notFound});
}
2018-12-17 22:49:54 +01:00
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);
}
2018-12-17 22:49:54 +01:00
callback(null, task);
});
});
}
function setTaskEnabled(req, res, enable) {
withTask(req, res, function (err, task) {
if (err) {
return Resources.error(res, err);
}
task.enabled = !!enable; // ensure boolean
2018-12-17 22:49:54 +01:00
return Resources.success(res, toExternalTask(task));
});
}
module.exports = {
getAll (req, res) {
Resources.getValidRestParams('list', null, req, function (err, restParams) {
if (err) {
return Resources.error(res, err);
}
const tasks = Resources.sort(
_.values(Scheduler.getTasks()),
['id', 'name', 'schedule', 'state', 'runningSince', 'lastRunStarted'],
restParams
);
const filteredTasks = Resources.filter(
tasks,
['id', 'name', 'schedule', 'state'],
restParams
);
const total = filteredTasks.length;
const pageTasks = Resources.getPageEntities(filteredTasks, restParams);
res.set('X-Total-Count', total);
return Resources.success(res, _.map(pageTasks, toExternalTask));
});
2018-12-17 22:49:54 +01:00
},
2018-12-17 22:49:54 +01:00
run (req, res) {
2016-06-07 11:01:35 +02:00
withTask(req, res, function (err, task) {
if (err) {
return Resources.error(res, err);
}
2018-12-17 22:49:54 +01:00
if (task.runningSince) {
return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict});
}
task.run();
2016-06-07 11:01:35 +02:00
return Resources.success(res, toExternalTask(task));
});
2018-12-17 22:49:54 +01:00
},
2018-12-17 22:49:54 +01:00
enable (req, res) {
setTaskEnabled(req, res, true);
},
disable (req, res) {
setTaskEnabled(req, res, false);
}
}