From 894eafec435afa38581c0809a7fb265418770b61 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 7 Aug 2023 08:57:36 +0100 Subject: [PATCH] add logs to probe --- Common/Types/WebsiteRequest.ts | 3 +- Common/Utils/API.ts | 6 +-- Probe/Index.ts | 12 ++++-- Probe/Jobs/Monitor/FetchList.ts | 15 ++++++++ Probe/Tests/Utils/PingMonitor.test.ts | 53 +++++++++++++++++---------- 5 files changed, 62 insertions(+), 27 deletions(-) diff --git a/Common/Types/WebsiteRequest.ts b/Common/Types/WebsiteRequest.ts index 4862c75e16..f861cd19ff 100644 --- a/Common/Types/WebsiteRequest.ts +++ b/Common/Types/WebsiteRequest.ts @@ -3,6 +3,7 @@ import Headers from './API/Headers'; import URL from './API/URL'; import HTML from './Html'; import HTTPMethod from './API/HTTPMethod'; +import Dictionary from './Dictionary'; export interface WebsiteResponse { url: URL; @@ -45,7 +46,7 @@ export default class WebsiteRequest { return { url: url, requestHeaders: options.headers || {}, - responseHeaders: response.headers, + responseHeaders: response.headers as Dictionary, responseStatusCode: response.status, responseBody: new HTML(response.data), isOnline: true, diff --git a/Common/Utils/API.ts b/Common/Utils/API.ts index 7a8a49ba2c..284035de99 100644 --- a/Common/Utils/API.ts +++ b/Common/Utils/API.ts @@ -257,7 +257,7 @@ export default class API { const response: HTTPResponse = new HTTPResponse( result.status, result.data, - result.headers + result.headers as Dictionary ); return response; @@ -280,8 +280,8 @@ export default class API { if (error.response) { return new HTTPErrorResponse( error.response.status, - error.response.data, - error.response.headers + error.response.data as JSONObject | JSONArray, + error.response.headers as Dictionary ); } diff --git a/Probe/Index.ts b/Probe/Index.ts index 613075a908..10a0000863 100644 --- a/Probe/Index.ts +++ b/Probe/Index.ts @@ -20,11 +20,15 @@ const init: Function = async (): Promise => { let workers: number = 0; while (workers < PROBE_MONITORING_WORKERS) { - new FetchListAndProbe().run().catch((err: any) => { - logger.error('FetchListAndProbe Failed:'); - logger.error(err); - }); + logger.info(`Starting worker ${workers}`); workers++; + + new FetchListAndProbe('Worker ' + workers) + .run() + .catch((err: any) => { + logger.error('FetchListAndProbe Failed:'); + logger.error(err); + }); } } catch (err) { logger.error('App Init Failed:'); diff --git a/Probe/Jobs/Monitor/FetchList.ts b/Probe/Jobs/Monitor/FetchList.ts index 7a122dbdcc..742547484c 100644 --- a/Probe/Jobs/Monitor/FetchList.ts +++ b/Probe/Jobs/Monitor/FetchList.ts @@ -14,13 +14,25 @@ import OneUptimeDate from 'Common/Types/Date'; import Sleep from 'Common/Types/Sleep'; export default class FetchListAndProbe { + private workerName: string = ''; + + public constructor(workerName: string) { + this.workerName = workerName; + } + public async run(): Promise { + logger.info(`Running worker ${this.workerName}`); + const runTIme: Date = OneUptimeDate.getCurrentDate(); // eslint-disable-next-line no-constant-condition while (true) { + logger.info(`Probing monitors ${this.workerName}`); + await this.fetchListAndProbe(); + logger.info(`Probing monitors ${this.workerName} complete`); + // if rumTime + 5 seconds is in the future, then this fetchLst either errored out or had no monitors in the list. Either way, wait for 5 seconds and proceed. const fiveSecondsAdded: Date = OneUptimeDate.addRemoveSeconds( @@ -29,6 +41,9 @@ export default class FetchListAndProbe { ); if (OneUptimeDate.isInTheFuture(fiveSecondsAdded)) { + logger.info( + `Worker ${this.workerName} is waiting for 5 seconds` + ); await Sleep.sleep(5000); } } diff --git a/Probe/Tests/Utils/PingMonitor.test.ts b/Probe/Tests/Utils/PingMonitor.test.ts index 0ef9747227..3f39edeff6 100644 --- a/Probe/Tests/Utils/PingMonitor.test.ts +++ b/Probe/Tests/Utils/PingMonitor.test.ts @@ -9,42 +9,57 @@ import '@types/jest'; describe('Ping', () => { jest.setTimeout(10000); test('Ping.ping should return appropriate object if the valid hostname is given', async () => { - let result: PingResponse = await Ping.ping( + let result: PingResponse | null = await Ping.ping( new Hostname('google.com', 80) ); - expect(result.responseTimeInMS?.toNumber()).toBeGreaterThan(0); - expect(result.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); - expect(result.isOnline).toBe(true); + + expect(result).not.toBeNull(); + expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); + expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); + expect(result!.isOnline).toBe(true); result = await Ping.ping(new Hostname('www.google.com', 80), { timeout: new PositiveNumber(5000), }); - expect(result.isOnline).toBe(true); - expect(result.responseTimeInMS?.toNumber()).toBeGreaterThan(0); - expect(result.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); + + expect(result).not.toBeNull(); + expect(result!.isOnline).toBe(true); + expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); + expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); + result = await Ping.ping(new Hostname('www.google.com', 65000), { timeout: new PositiveNumber(5000), }); - expect(result.isOnline).toBe(false); - expect(result.responseTimeInMS).toBeUndefined(); + expect(result).not.toBeNull(); + expect(result!.isOnline).toBe(false); + expect(result!.responseTimeInMS).toBeUndefined(); + result = await Ping.ping(new Hostname('www.a.com', 65000), { timeout: new PositiveNumber(5000), }); - expect(result.isOnline).toBe(false); - expect(result.responseTimeInMS).toBeUndefined(); + expect(result).not.toBeNull(); + expect(result!.isOnline).toBe(false); + expect(result!.isOnline).toBe(false); + expect(result!.responseTimeInMS).toBeUndefined(); }); test('Ping.ping should return appropriate object if the valid IPV4 or IPV6 is given', async () => { - let result: PingResponse; + let result: PingResponse | null = null; + result = await Ping.ping(new IPv4('172.217.170.206'), { timeout: new PositiveNumber(5000), }); // One of the google ip - expect(result.isOnline).toBe(true); - expect(result.responseTimeInMS?.toNumber()).toBeGreaterThan(0); - expect(result.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); + expect(result).not.toBeNull(); + expect(result!.isOnline).toBe(true); + expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); + expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); + result = await Ping.ping(new IPv4('192.0.2.200')); // - expect(result.isOnline).toBe(false); - expect(result.responseTimeInMS).toBeUndefined(); + expect(result).not.toBeNull(); + expect(result!.isOnline).toBe(false); + expect(result!.responseTimeInMS).toBeUndefined(); + result = await Ping.ping(new IPv4('0.42.52.42')); // ip can't start 0 - expect(result.responseTimeInMS).toBeUndefined(); - expect(result.isOnline).toBe(false); + expect(result).not.toBeNull(); + expect(result!.responseTimeInMS).toBeUndefined(); + expect(result!.isOnline).toBe(false); }); });