var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
// This class checks the status of all the datasources.
import Sleep from "../../Types/Sleep";
import logger from "../Utils/Logger";
import { ClickhouseAppInstance } from "./ClickhouseDatabase";
import PostgresAppInstance from "./PostgresDatabase";
import Redis from "./Redis";
import DatabaseNotConnectedException from "../../Types/Exception/DatabaseNotConnectedException";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export default class InfrastructureStatus {
    static async checkStatus(data) {
        logger.info("Checking infrastructure status");
        if (data.checkRedisStatus) {
            logger.info("Checking Redis status");
            if (!(await Redis.checkConnnectionStatus())) {
                logger.info("Redis is not connected");
                throw new DatabaseNotConnectedException("Redis is not connected");
            }
            logger.info("Redis is connected");
        }
        if (data.checkPostgresStatus) {
            logger.info("Checking Postgres status");
            if (!(await PostgresAppInstance.checkConnnectionStatus())) {
                logger.info("Postgres is not connected");
                throw new DatabaseNotConnectedException("Postgres is not connected");
            }
            logger.info("Postgres is connected");
        }
        if (data.checkClickhouseStatus) {
            logger.info("Checking Clickhouse status");
            if (!(await ClickhouseAppInstance.checkConnnectionStatus())) {
                logger.info("Clickhouse is not connected");
                throw new DatabaseNotConnectedException("Clickhouse is not connected");
            }
            logger.info("Clickhouse is connected");
        }
    }
    static async checkStatusWithRetry(data) {
        let retry = 0;
        while (retry < data.retryCount) {
            try {
                await this.checkStatus({
                    checkRedisStatus: data.checkRedisStatus,
                    checkPostgresStatus: data.checkPostgresStatus,
                    checkClickhouseStatus: data.checkClickhouseStatus,
                });
                break;
            }
            catch (err) {
                logger.error("Error checking infrastructure status");
                logger.error(err);
                retry++;
                await Sleep.sleep(1000);
            }
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], InfrastructureStatus, "checkStatus", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], InfrastructureStatus, "checkStatusWithRetry", null);
//# sourceMappingURL=Status.js.map