2020-04-08 23:41:04 +02:00
|
|
|
import cron from "node-cron";
|
|
|
|
import moment from "moment";
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
import { config } from "../config";
|
2020-04-08 23:41:04 +02:00
|
|
|
import Logger from "../logger";
|
|
|
|
|
|
|
|
import MailQueueJob from "./MailQueueJob";
|
|
|
|
import FixNodeFilenamesJob from "./FixNodeFilenamesJob";
|
|
|
|
import NodeInformationRetrievalJob from "./NodeInformationRetrievalJob";
|
|
|
|
import MonitoringMailsSendingJob from "./MonitoringMailsSendingJob";
|
|
|
|
import OfflineNodesDeletionJob from "./OfflineNodesDeletionJob";
|
2022-09-06 19:09:25 +02:00
|
|
|
import { DurationSeconds, JobResultState, TaskState } from "../shared/types";
|
2020-06-30 17:08:24 +02:00
|
|
|
|
|
|
|
export type JobResult = {
|
2022-08-23 20:08:53 +02:00
|
|
|
state: JobResultState;
|
|
|
|
message?: string;
|
2020-06-30 17:08:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export function jobResultOkay(message?: string): JobResult {
|
|
|
|
return {
|
|
|
|
state: JobResultState.OKAY,
|
2022-08-23 20:08:53 +02:00
|
|
|
message,
|
|
|
|
};
|
2020-06-30 17:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function jobResultWarning(message?: string): JobResult {
|
|
|
|
return {
|
|
|
|
state: JobResultState.WARNING,
|
2022-08-23 20:08:53 +02:00
|
|
|
message,
|
|
|
|
};
|
2020-06-30 17:08:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 23:41:04 +02:00
|
|
|
export interface Job {
|
2022-08-23 20:08:53 +02:00
|
|
|
name: string;
|
|
|
|
description: string;
|
2020-04-08 23:41:04 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
run(): Promise<JobResult>;
|
2020-04-08 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Task {
|
|
|
|
constructor(
|
|
|
|
public id: number,
|
|
|
|
public name: string,
|
|
|
|
public description: string,
|
|
|
|
public schedule: string,
|
|
|
|
public job: Job,
|
|
|
|
public runningSince: moment.Moment | null,
|
|
|
|
public lastRunStarted: moment.Moment | null,
|
2022-09-06 19:09:25 +02:00
|
|
|
public lastRunDuration: DurationSeconds | null,
|
2020-04-08 23:41:04 +02:00
|
|
|
public state: TaskState,
|
2020-06-30 17:08:24 +02:00
|
|
|
public result: JobResult | null,
|
2022-08-23 20:08:53 +02:00
|
|
|
public enabled: boolean
|
2020-04-08 23:41:04 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
run(): void {
|
|
|
|
if (this.runningSince || !this.enabled) {
|
|
|
|
// job is still running, skip execution
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.runningSince = moment();
|
|
|
|
this.lastRunStarted = this.runningSince;
|
|
|
|
this.state = TaskState.RUNNING;
|
|
|
|
|
2020-06-30 17:08:24 +02:00
|
|
|
const done = (state: TaskState, result: JobResult | null): void => {
|
2020-04-08 23:41:04 +02:00
|
|
|
const now = moment();
|
2022-09-06 19:09:25 +02:00
|
|
|
const duration = now.diff(
|
|
|
|
this.runningSince || now
|
|
|
|
) as DurationSeconds;
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("jobs").profile("[%sms]\t%s", duration, this.name);
|
2020-04-08 23:41:04 +02:00
|
|
|
|
|
|
|
this.runningSince = null;
|
|
|
|
this.lastRunDuration = duration;
|
|
|
|
this.state = state;
|
2020-06-30 17:08:24 +02:00
|
|
|
this.result = result;
|
2020-04-08 23:41:04 +02:00
|
|
|
};
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
this.job
|
|
|
|
.run()
|
|
|
|
.then((result) => {
|
|
|
|
done(TaskState.IDLE, result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
Logger.tag("jobs").error("Job %s failed: %s", this.name, err);
|
|
|
|
done(TaskState.FAILED, null);
|
|
|
|
});
|
2020-04-08 23:41:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
type Tasks = { [key: string]: Task };
|
2020-04-08 23:41:04 +02:00
|
|
|
|
|
|
|
const tasks: Tasks = {};
|
|
|
|
|
|
|
|
let taskId = 1;
|
|
|
|
function nextTaskId(): number {
|
|
|
|
const id = taskId;
|
|
|
|
taskId += 1;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function schedule(expr: string, job: Job): void {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("jobs").info("Scheduling job: %s %s", expr, job.name);
|
2020-04-08 23:41:04 +02:00
|
|
|
|
|
|
|
const id = nextTaskId();
|
|
|
|
|
|
|
|
const task = new Task(
|
|
|
|
id,
|
|
|
|
job.name,
|
|
|
|
job.description,
|
|
|
|
expr,
|
|
|
|
job,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
TaskState.IDLE,
|
2020-06-30 17:08:24 +02:00
|
|
|
null,
|
2022-08-23 20:08:53 +02:00
|
|
|
true
|
2020-04-08 23:41:04 +02:00
|
|
|
);
|
|
|
|
|
2021-02-22 21:48:39 +01:00
|
|
|
cron.schedule(expr, () => task.run());
|
2020-04-08 23:41:04 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
tasks["" + id] = task;
|
2020-04-08 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function init() {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("jobs").info("Scheduling background jobs...");
|
2020-04-08 23:41:04 +02:00
|
|
|
|
|
|
|
try {
|
2022-08-23 20:08:53 +02:00
|
|
|
schedule("0 */1 * * * *", MailQueueJob);
|
|
|
|
schedule("15 */1 * * * *", FixNodeFilenamesJob);
|
2020-04-08 23:41:04 +02:00
|
|
|
|
|
|
|
if (config.client.monitoring.enabled) {
|
2022-08-23 20:08:53 +02:00
|
|
|
schedule("30 */15 * * * *", NodeInformationRetrievalJob);
|
|
|
|
schedule("45 */5 * * * *", MonitoringMailsSendingJob);
|
|
|
|
schedule("0 0 3 * * *", OfflineNodesDeletionJob); // every night at 3:00
|
2020-04-08 23:41:04 +02:00
|
|
|
}
|
2022-08-23 20:08:53 +02:00
|
|
|
} catch (error) {
|
|
|
|
Logger.tag("jobs").error(
|
|
|
|
"Error during scheduling of background jobs:",
|
|
|
|
error
|
|
|
|
);
|
2020-04-08 23:41:04 +02:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("jobs").info("Scheduling of background jobs done.");
|
2020-04-08 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getTasks(): Tasks {
|
|
|
|
return tasks;
|
|
|
|
}
|