From 46f2adb459d0dc490eef35c41b095ec125b9207a Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 21 Mar 2025 19:47:58 +0000 Subject: [PATCH] feat: add new cron schedule for FetchMonitorTest to run every ten seconds --- Common/Utils/CronTime.ts | 1 + Probe/Index.ts | 13 +----- Probe/Jobs/Monitor/FetchMonitorTest.ts | 65 ++++++++++++++------------ 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/Common/Utils/CronTime.ts b/Common/Utils/CronTime.ts index 149f2b1e8b..d01c658bba 100644 --- a/Common/Utils/CronTime.ts +++ b/Common/Utils/CronTime.ts @@ -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 * * * * *"; diff --git a/Probe/Index.ts b/Probe/Index.ts index dcb08c8bc3..13c47a8030 100644 --- a/Probe/Index.ts +++ b/Probe/Index.ts @@ -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 => { 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; diff --git a/Probe/Jobs/Monitor/FetchMonitorTest.ts b/Probe/Jobs/Monitor/FetchMonitorTest.ts index 4fd4184fec..468220e530 100644 --- a/Probe/Jobs/Monitor/FetchMonitorTest.ts +++ b/Probe/Jobs/Monitor/FetchMonitorTest.ts @@ -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 { - 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 { +export default class FetchMonitorTestAndProbe { + + + public static async run(): Promise { + + 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 { try { logger.debug("Fetching monitor list");