From 9e50d068dbee39a9059f92a11de856def68b9f9f Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 27 Nov 2024 11:24:30 +0000 Subject: [PATCH] Enhance Clickhouse connection handling with improved error responses and status checks --- .../Infrastructure/ClickhouseDatabase.ts | 33 +++++++++++++++++-- ProbeIngest/Index.ts | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Common/Server/Infrastructure/ClickhouseDatabase.ts b/Common/Server/Infrastructure/ClickhouseDatabase.ts index e56e23e311..75f873422e 100644 --- a/Common/Server/Infrastructure/ClickhouseDatabase.ts +++ b/Common/Server/Infrastructure/ClickhouseDatabase.ts @@ -7,6 +7,11 @@ import { import { PingResult, createClient, ClickHouseClient } from "@clickhouse/client"; import DatabaseNotConnectedException from "Common/Types/Exception/DatabaseNotConnectedException"; import Sleep from "Common/Types/Sleep"; +import API from "../../Utils/API"; +import URL from "../../Types/API/URL"; +import HTTPErrorResponse from "../../Types/API/HTTPErrorResponse"; +import HTTPResponse from "../../Types/API/HTTPResponse"; +import { JSONObject } from "../../Types/JSON"; export type ClickhouseClient = ClickHouseClient; @@ -103,7 +108,18 @@ export default class ClickhouseDatabase { logger.debug( "Checking Clickhouse Connection Status - pinging clickhouse", ); - const result: PingResult | undefined = await this.getDataSource()?.ping(); + + const dbUrl: string | undefined = this.getDatasourceOptions().url as + | string + | undefined; + + if (!dbUrl) { + throw new DatabaseNotConnectedException("Clickhouse URL not found"); + } + + const result: HTTPErrorResponse | HTTPResponse = + await API.get(URL.fromString(dbUrl.toString())); + logger.debug("Clickhouse Connection Status Result"); logger.debug(result); @@ -113,13 +129,24 @@ export default class ClickhouseDatabase { ); } - if (result?.success === false) { + if (result instanceof HTTPErrorResponse) { throw new DatabaseNotConnectedException( "Clickhouse Database is not connected", ); } - return true; + if ( + result.data && + ((result.data as JSONObject)["data"] as string) && + ((result.data as JSONObject)["data"] as string).toString().trim() === + "Ok." + ) { + return true; + } + + throw new DatabaseNotConnectedException( + "Clickhouse Database is not connected", + ); } catch (err) { logger.error("Clickhouse Connection Lost"); logger.error(err); diff --git a/ProbeIngest/Index.ts b/ProbeIngest/Index.ts index 17d169c600..af3028f86b 100644 --- a/ProbeIngest/Index.ts +++ b/ProbeIngest/Index.ts @@ -28,7 +28,7 @@ const init: PromiseVoidFunction = async (): Promise => { try { const statusCheck: PromiseVoidFunction = async (): Promise => { return await InfrastructureStatus.checkStatusWithRetry({ - checkClickhouseStatus: false, + checkClickhouseStatus: true, checkPostgresStatus: true, checkRedisStatus: true, retryCount: 3,