feat: add new cron schedule for FetchMonitorTest to run every ten seconds

This commit is contained in:
Simon Larsen
2025-03-21 19:47:58 +00:00
parent 8637a6a5c7
commit 46f2adb459
3 changed files with 39 additions and 40 deletions

View File

@@ -8,3 +8,4 @@ export const EVERY_FIFTEEN_MINUTE: string = "*/15 * * * *";
export const EVERY_THIRTY_SECONDS: string = "*/30 * * * * *";
export const EVERY_THIRTY_MINUTES: string = "*/30 * * * *";
export const EVERY_THREE_HOURS: string = "0 */3 * * *";
export const EVERY_TEN_SECONDS: string = "*/10 * * * * *";

View File

@@ -5,7 +5,7 @@ import {
} from "./Config";
import "./Jobs/Alive";
import FetchListAndProbe from "./Jobs/Monitor/FetchList";
import FetchMonitorTest from "./Jobs/Monitor/FetchMonitorTest";
import "./Jobs/Monitor/FetchMonitorTest";
import Register from "./Services/Register";
import { PromiseVoidFunction } from "Common/Types/FunctionTypes";
import Sleep from "Common/Types/Sleep";
@@ -54,16 +54,7 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
throw err;
}
// add test job
try {
new FetchMonitorTest("Monitor Test Fetcher").run();
} catch (err) {
logger.error("Monitor Test Fetcher failed");
logger.error(err);
throw err;
}
try {
let workers: number = 0;

View File

@@ -10,41 +10,48 @@ import MonitorTest from "Common/Models/DatabaseModels/MonitorTest";
import APIException from "Common/Types/Exception/ApiException";
import { JSONArray } from "Common/Types/JSON";
import ProbeMonitorResponse from "Common/Types/Probe/ProbeMonitorResponse";
import Sleep from "Common/Types/Sleep";
import API from "Common/Utils/API";
import logger from "Common/Server/Utils/Logger";
import BasicCron from "Common/Server/Utils/BasicCron";
import { EVERY_TEN_SECONDS } from "Common/Utils/CronTime";
export default class FetchMonitorTestAndProbe {
private workerName: string = "";
public constructor(workerName: string) {
this.workerName = workerName;
}
public async run(): Promise<void> {
logger.debug(`Running worker ${this.workerName}`);
// eslint-disable-next-line no-constant-condition
while (true) {
try {
logger.debug(`Probing monitors ${this.workerName}`);
await this.fetchListAndProbe();
logger.debug(`Probing monitors ${this.workerName} complete`);
// sleep for 15 seconds
await Sleep.sleep(15000);
} catch (err) {
logger.error(`Error in worker ${this.workerName}`);
logger.error(err);
await Sleep.sleep(2000);
}
BasicCron({
jobName: "Probe:MonitorTest",
options: {
schedule: EVERY_TEN_SECONDS,
runOnStartup: true,
},
runFunction: async () => {
try {
await FetchMonitorTestAndProbe.run();
} catch (err) {
logger.error("Error in worker");
logger.error(err);
}
}
});
private async fetchListAndProbe(): Promise<void> {
export default class FetchMonitorTestAndProbe {
public static async run(): Promise<void> {
try {
logger.debug(`MONITOR TEST: Probing monitors `);
await this.fetchListAndProbe();
logger.debug(`MONITOR TEST: Probing monitors complete`);
} catch (err) {
logger.error(`Error in worker `);
logger.error(err);
}
}
private static async fetchListAndProbe(): Promise<void> {
try {
logger.debug("Fetching monitor list");