From c973b3b93b8f170c46a6bbaf0f8b888878ab4a45 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 10 May 2023 18:53:36 +0100 Subject: [PATCH] fix probes --- CommonServer/Types/Database/QueryHelper.ts | 13 ++++++++ Probe/Index.ts | 1 + Probe/Jobs/Monitor.ts | 37 ---------------------- Probe/Jobs/Monitor/FetchList.ts | 2 ++ ProbeAPI/API/Monitor.ts | 2 +- ProbeAPI/Index.ts | 2 ++ 6 files changed, 19 insertions(+), 38 deletions(-) delete mode 100644 Probe/Jobs/Monitor.ts diff --git a/CommonServer/Types/Database/QueryHelper.ts b/CommonServer/Types/Database/QueryHelper.ts index c7fa68df23..ca1b0225ad 100644 --- a/CommonServer/Types/Database/QueryHelper.ts +++ b/CommonServer/Types/Database/QueryHelper.ts @@ -125,6 +125,19 @@ export default class QueryHelper { ); } + + public static lessThanEqualToOrNull(value: number | Date): FindOperator { + const rid: string = Text.generateRandomText(10); + return Raw( + (alias: string) => { + return `${alias} <= :${rid} or ${alias} IS NULL`; + }, + { + [rid]: value, + } + ); + } + public static greaterThan(value: number | Date): FindOperator { const rid: string = Text.generateRandomText(10); return Raw( diff --git a/Probe/Index.ts b/Probe/Index.ts index 74fc01051d..a01f2b1faf 100644 --- a/Probe/Index.ts +++ b/Probe/Index.ts @@ -4,6 +4,7 @@ import App from 'CommonServer/Utils/StartServer'; import Register from './Services/Register'; import './Jobs/Alive'; +import './Jobs/Monitor/FetchList'; const APP_NAME: string = 'probe'; diff --git a/Probe/Jobs/Monitor.ts b/Probe/Jobs/Monitor.ts deleted file mode 100644 index 65b3ef1db5..0000000000 --- a/Probe/Jobs/Monitor.ts +++ /dev/null @@ -1,37 +0,0 @@ -import API from 'Common/Utils/API'; -import RunCron from '../Utils/Cron'; -import { EVERY_MINUTE } from 'Common/Utils/CronTime'; -import { PROBE_API_URL, PROBE_KEY } from '../Config'; -import LocalCache from 'CommonServer/Infrastructure/LocalCache'; -import URL from 'Common/Types/API/URL'; -import logger from 'CommonServer/Utils/Logger'; -import Register from '../Services/Register'; - -RunCron( - 'Basic:Monitor', - { - schedule: EVERY_MINUTE, - runOnStartup: false, - }, - async () => { - // get a list of monitors from probe-api - - // for each monitor, ping and then report back to probe-api - - if (!LocalCache.getString('PROBE', 'PROBE_ID')) { - logger.warn( - 'Probe is not registered yet. Skipping alive check. Trying to register probe again...' - ); - await Register.registerProbe(); - return; - } - - await API.post( - URL.fromString(PROBE_API_URL.toString()).addRoute('/alive'), - { - probeKey: PROBE_KEY, - probeId: LocalCache.getString('PROBE', 'PROBE_ID'), - } - ); - } -); diff --git a/Probe/Jobs/Monitor/FetchList.ts b/Probe/Jobs/Monitor/FetchList.ts index 50832b470b..87e1718bf4 100644 --- a/Probe/Jobs/Monitor/FetchList.ts +++ b/Probe/Jobs/Monitor/FetchList.ts @@ -31,6 +31,8 @@ RunCron( const monitors: Array = result.data as Array; + console.log('Monitors: ', monitors); + const monitoringPromises: Array> = []; for (const monitor of monitors) { diff --git a/ProbeAPI/API/Monitor.ts b/ProbeAPI/API/Monitor.ts index 89b866de95..c9498ea20d 100644 --- a/ProbeAPI/API/Monitor.ts +++ b/ProbeAPI/API/Monitor.ts @@ -48,7 +48,7 @@ router.post( query: { probeId: (req as ProbeExpressRequest).probe!.id!, isEnabled: true, - nextPingAt: QueryHelper.lessThanEqualTo( + nextPingAt: QueryHelper.lessThanEqualToOrNull( OneUptimeDate.getCurrentDate() ), }, diff --git a/ProbeAPI/Index.ts b/ProbeAPI/Index.ts index 2e9075ff07..10e38e6bf6 100644 --- a/ProbeAPI/Index.ts +++ b/ProbeAPI/Index.ts @@ -5,6 +5,7 @@ import logger from 'CommonServer/Utils/Logger'; import App from 'CommonServer/Utils/StartServer'; import AliveAPI from './API/Alive'; import RegisterAPI from './API/Register'; +import MonitorAPI from './API/Monitor'; import Redis from 'CommonServer/Infrastructure/Redis'; @@ -14,6 +15,7 @@ const APP_NAME: string = 'probe-api'; app.use([`/${APP_NAME}`, '/'], AliveAPI); app.use([`/${APP_NAME}`, '/'], RegisterAPI); +app.use([`/${APP_NAME}`, '/'], MonitorAPI); const init: Function = async (): Promise => { try {