Add timeout option to API requests and update monitor configurations

This commit is contained in:
Simon Larsen 2024-09-18 13:13:24 +01:00
parent e2ef8ae2ce
commit cc8c3f7d9e
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
4 changed files with 29 additions and 5 deletions

View File

@ -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) {

View File

@ -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
},
);

View File

@ -36,6 +36,7 @@ export default class ApiMonitor {
currentRetryCount?: number | undefined;
monitorId?: ObjectID | undefined;
isOnlineCheckRequest?: boolean | undefined;
timeout?: PositiveNumber; // timeout in milliseconds
},
): Promise<APIResponse | null> {
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,
},
);
}

View File

@ -33,6 +33,7 @@ export default class WebsiteMonitor {
currentRetryCount?: number | undefined;
monitorId?: ObjectID | undefined;
isOnlineCheckRequest?: boolean | undefined;
timeout?: PositiveNumber; // timeout in milliseconds
},
): Promise<ProbeWebsiteResponse | null> {
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,
});
}