package com.banesco.infrastructure.healthcheck; import com.banesco.commons.config.AppConf; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import lombok.extern.slf4j.Slf4j; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.Readiness; import java.util.concurrent.Semaphore; @SuppressWarnings("unused") @Readiness @Slf4j @ApplicationScoped public class ConfigHealthCheck implements HealthCheck { // Semáforo para evitar múltiples health checks simultáneos private final Semaphore healthCheckSemaphore = new Semaphore(1); @Inject public ConfigHealthCheck() { log.info("ConfigHealthCheck inicializado"); } @Override public HealthCheckResponse call() { if (!healthCheckSemaphore.tryAcquire()) { log.debug("Health check en progreso, saltando esta invocación"); return HealthCheckResponse.builder() .name("ApiConfig") .withData("AppConf", "progressing") .withData("timestamp", System.currentTimeMillis()) .build(); } try { AppConf.getInstance(); return buildResponse(null); } catch (Exception e) { return buildResponse(e.getMessage()); } finally { healthCheckSemaphore.release(); } } private HealthCheckResponse buildResponse(String errorMessage) { var responseBuilder = HealthCheckResponse.named("ApiConfig"); if (errorMessage == null) { return responseBuilder.up() .withData("AppConf", "configured") .withData("timestamp", System.currentTimeMillis()) .build(); } return responseBuilder.down() .withData("AppConf", "wrong") .withData("timestamp", System.currentTimeMillis()) .withData("errorMessage", errorMessage) .build(); } }