package cn.com.capcloud.core.noentitydb;

import cn.com.capcloud.core.noentitydb.DatabaseHealthCheckService.HealthCheckResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 启动前保护检查。
 */
public final class StartupGuard {

    private static final Logger logger = LoggerFactory.getLogger(StartupGuard.class);

    private StartupGuard() {
    }

    public static void checkDatabaseOrThrow() {
        DatabaseHealthCheckService healthCheckService = new DatabaseHealthCheckService();
        HealthCheckResult healthCheckResult = healthCheckService.checkBeforeStart();
        if (!healthCheckResult.isSuccess()) {
            /*logger.error("数据库未通过启动前检查，Spring Boot 将停止启动。\n" +
                            "  - {}:{}\n" +
                            "  - detail: {}",
                    healthCheckResult.getDatasourceHost(),
                    healthCheckResult.getDatasourcePort(),
                    healthCheckResult.getMessage());
            throw new IllegalStateException(healthCheckResult.getMessage());*/
            logger.error("数据库未通过启动前检查，Spring Boot 将停止启动。\n");
            throw new IllegalStateException(healthCheckResult.getMessage());
        }
    }
}
