From 639c4b13aeee9dd2c4abd1445d58839bd73f9dff Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 2 Jun 2023 15:32:11 +0100 Subject: [PATCH] fix issue with sending probe-id --- Docs/Probe/CustomProbe.md | 2 +- Probe/Jobs/Monitor/FetchList.ts | 67 ++++++++++++++++++--------------- Probe/Utils/ProbeAPIRequest.ts | 4 +- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/Docs/Probe/CustomProbe.md b/Docs/Probe/CustomProbe.md index 90bfddff9f1..dc97e9c4054 100644 --- a/Docs/Probe/CustomProbe.md +++ b/Docs/Probe/CustomProbe.md @@ -10,7 +10,7 @@ To begin with you need to create a custom probe in your Project Settings > Probe To run a probe, please make sure you have docker installed. You can run custom probe by: ``` -docker run --name oneuptime-probe -e PROBE_KEY= -e PROBE_ID= -e PROBE_API_URL=https://oneuptime.com/probe-api -d oneuptime/probe +docker run --name oneuptime-probe --network host -e PROBE_KEY= -e PROBE_ID= -e PROBE_API_URL=https://oneuptime.com/probe-api -d oneuptime/probe:release ``` diff --git a/Probe/Jobs/Monitor/FetchList.ts b/Probe/Jobs/Monitor/FetchList.ts index fa0a8f7745b..23b7e0d4ce6 100644 --- a/Probe/Jobs/Monitor/FetchList.ts +++ b/Probe/Jobs/Monitor/FetchList.ts @@ -23,42 +23,49 @@ RunCron( // run a set timeout function randomly between 1 to 5 seconds, so same probes do not hit the server at the same time setTimeout(async () => { - const result: HTTPResponse | HTTPErrorResponse = - await API.fetch( - HTTPMethod.POST, - URL.fromString(PROBE_API_URL.toString()).addRoute( - '/monitor/list' - ), - ProbeAPIRequest.getDefaultRequestBody(), - {}, - {} + try { + const monitorListUrl: URL = URL.fromString( + PROBE_API_URL.toString() + ).addRoute('/monitor/list'); + + const result: HTTPResponse | HTTPErrorResponse = + await API.fetch( + HTTPMethod.POST, + monitorListUrl, + ProbeAPIRequest.getDefaultRequestBody(), + {}, + {} + ); + + const monitors: Array = JSONFunctions.fromJSONArray( + result.data as JSONArray, + Monitor ); - const monitors: Array = JSONFunctions.fromJSONArray( - result.data as JSONArray, - Monitor - ); + const monitoringPromises: Array> = []; - const monitoringPromises: Array> = []; + for (const monitor of monitors) { + const promise: Promise = new Promise( + (resolve: Function, reject: Function): void => { + MonitorUtil.probeMonitor(monitor) + .then(() => { + resolve(); + }) + .catch((err: Error) => { + logger.error(err); + reject(err); + }); + } + ); - for (const monitor of monitors) { - const promise: Promise = new Promise( - (resolve: Function, reject: Function): void => { - MonitorUtil.probeMonitor(monitor) - .then(() => { - resolve(); - }) - .catch((err: Error) => { - logger.error(err); - reject(err); - }); - } - ); + monitoringPromises.push(promise); + } - monitoringPromises.push(promise); + await Promise.allSettled(monitoringPromises); + } catch (err) { + logger.error('Error in fetching monitor list'); + logger.error(err); } - - await Promise.allSettled(monitoringPromises); }, Math.floor(Math.random() * 5000) + 1000); } ); diff --git a/Probe/Utils/ProbeAPIRequest.ts b/Probe/Utils/ProbeAPIRequest.ts index 7d1711fa91f..aa765b632dc 100644 --- a/Probe/Utils/ProbeAPIRequest.ts +++ b/Probe/Utils/ProbeAPIRequest.ts @@ -6,7 +6,9 @@ export default class ProbeAPIRequest { public static getDefaultRequestBody(): JSONObject { return { probeKey: PROBE_KEY, - probeId: LocalCache.getString('PROBE', 'PROBE_ID'), + probeId: + LocalCache.getString('PROBE', 'PROBE_ID') || + process.env['PROBE_ID'], }; } }