org.zodiac.actuate.health.checker.AbstractHealthCheckerProcessor Maven / Gradle / Ivy
package org.zodiac.actuate.health.checker;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.zodiac.commons.util.function.BinaryOperators;
public abstract class AbstractHealthCheckerProcessor implements HealthCheckerProcessor {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private HealthCheckerInfo healthCheckerInfo;
public AbstractHealthCheckerProcessor(HealthCheckerInfo healthCheckerInfo) {
this.healthCheckerInfo = healthCheckerInfo;
}
@Override
public final Health healthCheck() {
logger.info("Begin HealthChecker check.");
List checkers = getCheckers();
Map healthCheckerDetails = new LinkedHashMap<>();
boolean checkResult =
checkers.stream().map(c -> doHealthCheck(c, healthCheckerDetails, healthCheckerInfo.isRetry())).reduce(true,
BinaryOperators.andBoolean());;
Health health = afterHealthCheck(healthCheckerDetails, checkResult);
logger.info("End HealthChecker check.");
return health;
}
protected final boolean doHealthCheck(HealthChecker healthChecker, Map healthMap, boolean retry) {
Health health;
boolean result;
int retryCount = 0;
logger.info("HealthChecker[{}] check start.", healthChecker.getName());
int timeout = healthChecker.getTimeout();
if (timeout <= 0) {
timeout = healthCheckerInfo.getDefaultTimeout();
}
do {
Future future = HealthCheckerExecutor.submitTask(healthChecker::isHealthy);
try {
health = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
logger.error("Timeout occurred while doing HealthChecker[{}] check, the timeout value is: {}ms.",
healthChecker.getName(), timeout);
health = new Health.Builder().withException(e).status(Status.UNKNOWN).build();
} catch (Throwable e) {
logger.error(String.format("Exception occurred while wait the result of HealthChecker[%s] check.",
healthChecker.getName()), e);
health = new Health.Builder().withException(e).status(Status.DOWN).build();
}
result = health.getStatus().equals(Status.UP);
if (result) {
logger.info("HealthChecker[{}] check success with {} retry.", healthChecker.getName(), retryCount);
break;
} else {
logger.info("HealthChecker[{}] check fail with {} retry.", healthChecker.getName(), retryCount);
}
if (retry && retryCount < healthChecker.getRetryCount()) {
try {
retryCount += 1;
TimeUnit.MILLISECONDS.sleep(healthChecker.getRetryTimeInterval());
} catch (InterruptedException e) {
logger.error(String.format("HealthChecker[%s] sleep error[%s] with %s retry.",
healthChecker.getName(), e.getMessage(), retryCount));
}
}
} while (retry && retryCount < healthChecker.getRetryCount());
healthMap.put(healthChecker.getName(), health);
if (!result) {
logger.error(String.format("HealthCheckers failed with %s retry.", healthChecker.getName(), retryCount));
}
return result;
}
protected abstract Health afterHealthCheck(Map healths, boolean checkResult);
protected abstract List getCheckers();
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy