2022-08-04 15:31:01 +02:00
|
|
|
import CONSTRAINTS from "../shared/validation/constraints";
|
2020-04-09 20:18:13 +02:00
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import * as Resources from "../utils/resources";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { handleJSONWithData, RequestData } from "../utils/resources";
|
2022-09-06 19:09:25 +02:00
|
|
|
import { getTasks, Task } from "../jobs/scheduler";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { normalizeString } from "../shared/utils/strings";
|
|
|
|
import { forConstraint } from "../shared/validation/validator";
|
2022-09-14 16:33:43 +02:00
|
|
|
import type { Request, Response } from "express";
|
2022-09-06 19:09:25 +02:00
|
|
|
import {
|
|
|
|
isString,
|
|
|
|
isTaskSortField,
|
|
|
|
TaskResponse,
|
|
|
|
TaskSortField,
|
|
|
|
TaskState,
|
|
|
|
UnixTimestampSeconds,
|
|
|
|
} from "../types";
|
2022-09-20 19:09:49 +02:00
|
|
|
import { HttpHeader } from "../shared/utils/http";
|
2020-04-09 20:18:13 +02:00
|
|
|
|
|
|
|
const isValidId = forConstraint(CONSTRAINTS.id, false);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function toTaskResponse(task: Task): TaskResponse {
|
2020-04-09 20:18:13 +02:00
|
|
|
return {
|
|
|
|
id: task.id,
|
|
|
|
name: task.name,
|
|
|
|
description: task.description,
|
|
|
|
schedule: task.schedule,
|
2022-09-06 19:09:25 +02:00
|
|
|
runningSince:
|
|
|
|
task.runningSince &&
|
|
|
|
(task.runningSince.unix() as UnixTimestampSeconds),
|
|
|
|
lastRunStarted:
|
|
|
|
task.lastRunStarted &&
|
|
|
|
(task.lastRunStarted.unix() as UnixTimestampSeconds),
|
2020-04-09 20:18:13 +02:00
|
|
|
lastRunDuration: task.lastRunDuration || null,
|
|
|
|
state: task.state,
|
2022-08-23 20:08:53 +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,
|
|
|
|
enabled: task.enabled,
|
2020-04-09 20:18:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function withValidTaskId(data: RequestData): Promise<string> {
|
|
|
|
if (!isString(data.id)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Missing task id.", type: ErrorTypes.badRequest };
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
|
|
|
const id = normalizeString(data.id);
|
2020-04-09 20:18:13 +02:00
|
|
|
|
|
|
|
if (!isValidId(id)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Invalid task id.", type: ErrorTypes.badRequest };
|
2020-04-09 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTask(id: string): Promise<Task> {
|
|
|
|
const tasks = getTasks();
|
|
|
|
const task = tasks[id];
|
|
|
|
|
|
|
|
if (!task) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Task not found.", type: ErrorTypes.notFound };
|
2020-04-09 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function withTask(data: RequestData): Promise<Task> {
|
|
|
|
const id = await withValidTaskId(data);
|
2020-04-09 20:18:13 +02:00
|
|
|
return await getTask(id);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function setTaskEnabled(
|
|
|
|
data: RequestData,
|
|
|
|
enable: boolean
|
|
|
|
): Promise<TaskResponse> {
|
2022-07-21 18:39:33 +02:00
|
|
|
const task = await withTask(data);
|
|
|
|
task.enabled = enable;
|
|
|
|
return toTaskResponse(task);
|
2020-04-09 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function doGetAll(
|
|
|
|
req: Request
|
|
|
|
): Promise<{ total: number; pageTasks: Task[] }> {
|
|
|
|
const restParams = await Resources.getValidRestParams("list", null, req);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-09-06 19:09:25 +02:00
|
|
|
const tasks = Resources.sort<Task, TaskSortField>(
|
2022-07-28 13:16:13 +02:00
|
|
|
Object.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,
|
2022-08-23 20:08:53 +02:00
|
|
|
["id", "name", "schedule", "state"],
|
2020-04-10 00:43:15 +02:00
|
|
|
restParams
|
|
|
|
);
|
|
|
|
|
|
|
|
const total = filteredTasks.length;
|
|
|
|
const pageTasks = Resources.getPageEntities(filteredTasks, restParams);
|
|
|
|
|
|
|
|
return {
|
|
|
|
total,
|
|
|
|
pageTasks,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export function getAll(req: Request, res: Response): void {
|
2020-04-10 00:43:15 +02:00
|
|
|
doGetAll(req)
|
2022-08-23 20:08:53 +02:00
|
|
|
.then(({ total, pageTasks }) => {
|
2022-09-20 19:09:49 +02:00
|
|
|
res.set(HttpHeader.X_TOTAL_COUNT, total.toString(10));
|
2022-07-28 13:16:13 +02:00
|
|
|
Resources.success(res, pageTasks.map(toTaskResponse));
|
2020-04-10 00:43:15 +02:00
|
|
|
})
|
2022-08-23 20:08:53 +02:00
|
|
|
.catch((err) => Resources.error(res, err));
|
2020-04-09 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const run = handleJSONWithData(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
const task = await withTask(data);
|
2020-04-09 20:18:13 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (task.runningSince) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Task already running.", type: ErrorTypes.conflict };
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
2020-04-09 20:18:13 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
task.run();
|
|
|
|
return toTaskResponse(task);
|
|
|
|
});
|
2020-04-09 20:18:13 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const enable = handleJSONWithData(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
await setTaskEnabled(data, true);
|
|
|
|
});
|
2020-04-09 20:18:13 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const disable = handleJSONWithData(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
await setTaskEnabled(data, false);
|
|
|
|
});
|