Skip to content

Commit

Permalink
Enhance Clickhouse connection handling with improved error responses …
Browse files Browse the repository at this point in the history
…and status checks
  • Loading branch information
simlarsen committed Nov 27, 2024
1 parent 7232a31 commit 9e50d06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 30 additions & 3 deletions Common/Server/Infrastructure/ClickhouseDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<JSONObject> =
await API.get(URL.fromString(dbUrl.toString()));

logger.debug("Clickhouse Connection Status Result");
logger.debug(result);

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ProbeIngest/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
try {
const statusCheck: PromiseVoidFunction = async (): Promise<void> => {
return await InfrastructureStatus.checkStatusWithRetry({
checkClickhouseStatus: false,
checkClickhouseStatus: true,
checkPostgresStatus: true,
checkRedisStatus: true,
retryCount: 3,
Expand Down

0 comments on commit 9e50d06

Please sign in to comment.