2020-04-09 20:18:13 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
|
|
|
|
import CONSTRAINTS from "../validation/constraints";
|
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import * as Resources from "../utils/resources";
|
2020-06-30 17:08:24 +02:00
|
|
|
import {Entity} from "../utils/resources";
|
|
|
|
import {getTasks, Task, TaskState} from "../jobs/scheduler";
|
2020-04-09 20:18:13 +02:00
|
|
|
import {normalizeString} from "../utils/strings";
|
|
|
|
import {forConstraint} from "../validation/validator";
|
|
|
|
import {Request, Response} from "express";
|
2022-06-23 14:26:15 +02:00
|
|
|
import {isTaskSortField} from "../types";
|
2020-04-09 20:18:13 +02:00
|
|
|
|
|
|
|
const isValidId = forConstraint(CONSTRAINTS.id, false);
|
|
|
|
|
|
|
|
interface ExternalTask {
|
|
|
|
id: number,
|
|
|
|
name: string,
|
|
|
|
description: string,
|
|
|
|
schedule: string,
|
|
|
|
runningSince: number | null,
|
|
|
|
lastRunStarted: number | null,
|
|
|
|
lastRunDuration: number | null,
|
|
|
|
state: string,
|
2020-06-30 17:08:24 +02:00
|
|
|
result: string | null,
|
|
|
|
message: string | null,
|
2020-04-09 20:18:13 +02:00
|
|
|
enabled: boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
function toExternalTask(task: Task): ExternalTask {
|
|
|
|
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 || null,
|
|
|
|
state: task.state,
|
2020-06-30 17:08:24 +02:00
|
|
|
result: task.state !== TaskState.RUNNING && task.result ? task.result.state : null,
|
|
|
|
message:task.state !== TaskState.RUNNING && task.result ? task.result.message || null : null,
|
2020-04-09 20:18:13 +02:00
|
|
|
enabled: task.enabled
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function withValidTaskId(req: Request): Promise<string> {
|
|
|
|
const id = normalizeString(Resources.getData(req).id);
|
|
|
|
|
|
|
|
if (!isValidId(id)) {
|
|
|
|
throw {data: 'Invalid task id.', type: ErrorTypes.badRequest};
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTask(id: string): Promise<Task> {
|
|
|
|
const tasks = getTasks();
|
|
|
|
const task = tasks[id];
|
|
|
|
|
|
|
|
if (!task) {
|
|
|
|
throw {data: 'Task not found.', type: ErrorTypes.notFound};
|
|
|
|
}
|
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function withTask(req: Request): Promise<Task> {
|
|
|
|
const id = await withValidTaskId(req);
|
|
|
|
return await getTask(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTaskEnabled(req: Request, res: Response, enable: boolean) {
|
|
|
|
withTask(req)
|
|
|
|
.then(task => {
|
|
|
|
task.enabled = enable;
|
|
|
|
Resources.success(res, toExternalTask(task))
|
|
|
|
})
|
|
|
|
.catch(err => Resources.error(res, err))
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
async function doGetAll(req: Request): Promise<{total: number, pageTasks: Entity[]}> {
|
|
|
|
const restParams = await Resources.getValidRestParams('list', null, req);
|
|
|
|
|
|
|
|
const tasks = Resources.sort(
|
|
|
|
_.values(getTasks()),
|
2022-06-23 14:26:15 +02:00
|
|
|
isTaskSortField,
|
2020-04-10 00:43:15 +02:00
|
|
|
restParams
|
|
|
|
);
|
|
|
|
const filteredTasks = Resources.filter(
|
|
|
|
tasks,
|
|
|
|
['id', 'name', 'schedule', 'state'],
|
|
|
|
restParams
|
|
|
|
);
|
|
|
|
|
|
|
|
const total = filteredTasks.length;
|
|
|
|
const pageTasks = Resources.getPageEntities(filteredTasks, restParams);
|
|
|
|
|
|
|
|
return {
|
|
|
|
total,
|
|
|
|
pageTasks,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-09 20:18:13 +02:00
|
|
|
export function getAll (req: Request, res: Response): void {
|
2020-04-10 00:43:15 +02:00
|
|
|
doGetAll(req)
|
|
|
|
.then(({total, pageTasks}) => {
|
|
|
|
res.set('X-Total-Count', total.toString(10));
|
|
|
|
Resources.success(res, _.map(pageTasks, toExternalTask));
|
|
|
|
})
|
|
|
|
.catch(err => Resources.error(res, err));
|
2020-04-09 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function run (req: Request, res: Response): void {
|
|
|
|
withTask(req)
|
|
|
|
.then(task => {
|
|
|
|
if (task.runningSince) {
|
|
|
|
return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict});
|
|
|
|
}
|
|
|
|
|
|
|
|
task.run();
|
|
|
|
|
|
|
|
Resources.success(res, toExternalTask(task));
|
|
|
|
})
|
|
|
|
.catch(err => Resources.error(res, err));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function enable (req: Request, res: Response): void {
|
|
|
|
setTaskEnabled(req, res, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function disable (req: Request, res: Response): void {
|
|
|
|
setTaskEnabled(req, res, false);
|
|
|
|
}
|