From cc8c3f7d9e8cc333b874fe48ee23512a2ec2ca6f Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 18 Sep 2024 13:13:24 +0100 Subject: [PATCH] Add timeout option to API requests and update monitor configurations --- Common/Utils/API.ts | 13 ++++++++++--- Probe/Utils/Monitors/Monitor.ts | 7 +++++++ Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts | 9 +++++++++ Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts | 5 +++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Common/Utils/API.ts b/Common/Utils/API.ts index 10e3269e49..25c6f9fffa 100644 --- a/Common/Utils/API.ts +++ b/Common/Utils/API.ts @@ -11,12 +11,13 @@ import URL from "../Types/API/URL"; import Dictionary from "../Types/Dictionary"; import APIException from "../Types/Exception/ApiException"; import { JSONArray, JSONObject } from "../Types/JSON"; -import axios, { AxiosError, AxiosResponse } from "axios"; +import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios"; import Sleep from "../Types/Sleep"; export interface RequestOptions { retries?: number | undefined; exponentialBackoff?: boolean | undefined; + timeout?: number | undefined; } export default class API { @@ -383,12 +384,18 @@ export default class API { while (currentRetry <= maxRetries) { currentRetry++; try { - result = await axios({ + const axiosOptions: AxiosRequestConfig = { method: method, url: url.toString(), headers: finalHeaders, data: finalBody, - }); + }; + + if (options?.timeout) { + axiosOptions.timeout = options.timeout; + } + + result = await axios(axiosOptions); break; } catch (e) { diff --git a/Probe/Utils/Monitors/Monitor.ts b/Probe/Utils/Monitors/Monitor.ts index 45cadaf93e..1b5024af8c 100644 --- a/Probe/Utils/Monitors/Monitor.ts +++ b/Probe/Utils/Monitors/Monitor.ts @@ -29,6 +29,7 @@ import API from "Common/Utils/API"; import LocalCache from "Common/Server/Infrastructure/LocalCache"; import logger from "Common/Server/Utils/Logger"; import Monitor from "Common/Models/DatabaseModels/Monitor"; +import PositiveNumber from "Common/Types/PositiveNumber"; export default class MonitorUtil { public static async probeMonitor( @@ -151,6 +152,7 @@ export default class MonitorUtil { { retry: 5, monitorId: monitor.id!, + timeout: new PositiveNumber(60000), // 60 seconds }, ); @@ -167,6 +169,7 @@ export default class MonitorUtil { { retry: 5, monitorId: monitor.id!, + timeout: new PositiveNumber(60000), // 60 seconds }, ); @@ -203,6 +206,7 @@ export default class MonitorUtil { { retry: 5, monitorId: monitor.id!, + timeout: new PositiveNumber(60000), // 60 seconds }, ); @@ -278,6 +282,7 @@ export default class MonitorUtil { { retry: 5, monitorId: monitor.id!, + timeout: new PositiveNumber(60000), // 60 seconds }, ); @@ -305,6 +310,7 @@ export default class MonitorUtil { isHeadRequest: MonitorUtil.isHeadRequest(monitorStep), monitorId: monitor.id!, retry: 5, + timeout: new PositiveNumber(60000), // 60 seconds }, ); @@ -345,6 +351,7 @@ export default class MonitorUtil { monitorId: monitor.id!, requestType: monitorStep.data?.requestType || HTTPMethod.GET, retry: 5, + timeout: new PositiveNumber(60000), // 60 seconds }, ); diff --git a/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts index 3ade7f6f56..9b5532fb90 100644 --- a/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts @@ -36,6 +36,7 @@ export default class ApiMonitor { currentRetryCount?: number | undefined; monitorId?: ObjectID | undefined; isOnlineCheckRequest?: boolean | undefined; + timeout?: PositiveNumber; // timeout in milliseconds }, ): Promise { if (!options) { @@ -62,6 +63,10 @@ export default class ApiMonitor { url, options.requestBody || undefined, options.requestHeaders || undefined, + undefined, + { + timeout: options.timeout?.toNumber() || 5000, + }, ); if ( @@ -75,6 +80,10 @@ export default class ApiMonitor { url, options.requestBody || undefined, options.requestHeaders || undefined, + undefined, + { + timeout: options.timeout?.toNumber() || 5000, + }, ); } diff --git a/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts index aaa792d8c6..9d287957ba 100644 --- a/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts @@ -33,6 +33,7 @@ export default class WebsiteMonitor { currentRetryCount?: number | undefined; monitorId?: ObjectID | undefined; isOnlineCheckRequest?: boolean | undefined; + timeout?: PositiveNumber; // timeout in milliseconds }, ): Promise { if (!options) { @@ -59,7 +60,7 @@ export default class WebsiteMonitor { let startTime: [number, number] = process.hrtime(); let result: WebsiteResponse = await WebsiteRequest.fetch(url, { isHeadRequest: options.isHeadRequest, - timeout: 30000, + timeout: options.timeout?.toNumber() || 5000, }); if ( @@ -70,7 +71,7 @@ export default class WebsiteMonitor { startTime = process.hrtime(); result = await WebsiteRequest.fetch(url, { isHeadRequest: false, - timeout: 30000, + timeout: options.timeout?.toNumber() || 5000, }); }