use of org.jboss.pnc.common.monitor.CancellableCompletableFuture in project pnc by project-ncl.
the class OpenshiftStartedEnvironment method monitorInitialization.
/**
* retries is decremented in retryPod in case of pod failing to start
*
* @param onComplete
* @param onError
* @param retries
*/
private void monitorInitialization(Consumer<RunningEnvironment> onComplete, Consumer<Exception> onError, int retries) {
cancelHook = () -> onComplete.accept(null);
CompletableFuture<Void> podFuture = creatingPod.thenComposeAsync(nul -> {
CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isPodRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
addFuture(monitor);
return monitor;
}, executor);
CompletableFuture<Void> serviceFuture = creatingService.thenComposeAsync(nul -> {
CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isServiceRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
addFuture(monitor);
return monitor;
}, executor);
CompletableFuture<Void> routeFuture;
if (creatingRoute.isPresent()) {
routeFuture = creatingRoute.get().thenComposeAsync(nul -> {
CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isRouteRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
addFuture(monitor);
return monitor;
}, executor);
} else {
routeFuture = CompletableFuture.completedFuture(null);
}
CompletableFuture<Void> openshiftDefinitionsError = new CompletableFuture<>();
openshiftDefinitions.exceptionally(t -> {
openshiftDefinitionsError.completeExceptionally(t);
return null;
});
CancellableCompletableFuture<Void> isBuildAgentUpFuture = pollingMonitor.monitor(this::isInternalServletAvailable, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
addFuture(isBuildAgentUpFuture);
CompletableFuture<RunningEnvironment> runningEnvironmentFuture = CompletableFutureUtils.allOfOrException(podFuture, serviceFuture, routeFuture).thenComposeAsync(nul -> isBuildAgentUpFuture, executor).thenApplyAsync(nul -> RunningEnvironment.createInstance(pod.getMetadata().getName(), Integer.parseInt(environmentConfiguration.getContainerPort()), route.getSpec().getHost(), getPublicEndpointUrl(), getInternalEndpointUrl(), repositorySession, Paths.get(environmentConfiguration.getWorkingDirectory()), this::destroyEnvironment, debugData), executor);
CompletableFuture.anyOf(runningEnvironmentFuture, openshiftDefinitionsError).handleAsync((runningEnvironment, throwable) -> {
if (throwable != null) {
logger.info("Error while trying to create an OpenShift environment... ", throwable);
cancelAndClearMonitors();
// no more retries, execute the onError consumer
if (retries == 0) {
logger.info("No more retries left, giving up!");
onError.accept(new Exception(getPrettierErrorMessageFromThrowable(throwable, true), throwable));
} else {
PodFailedStartException podFailedStartExc = null;
if (throwable instanceof PodFailedStartException) {
podFailedStartExc = (PodFailedStartException) throwable;
} else if (throwable.getCause() instanceof PodFailedStartException) {
podFailedStartExc = (PodFailedStartException) throwable.getCause();
}
if (podFailedStartExc != null && !Arrays.asList(POD_RETRYABLE_STATUSES).contains(podFailedStartExc.getPodStatus())) {
logger.info("The detected pod error status '{}' is not considered among the ones to be retried, giving up!", podFailedStartExc.getPodStatus());
// the status is not to be retried
onError.accept(new Exception(getPrettierErrorMessageFromThrowable(throwable, false), throwable));
} else {
if (!cancelRequested) {
logger.warn("Creating build environment failed with error '{}'! Retrying ({} retries left)...", throwable, retries);
retryEnvironment(onComplete, onError, retries);
} else {
logger.info("Build was cancelled, not retrying environment!");
}
}
}
} else {
logger.info("Environment successfully initialized. Pod [{}]; Service [{}].", pod.getMetadata().getName(), service.getMetadata().getName());
// openshiftDefinitionsError
onComplete.accept((RunningEnvironment) runningEnvironment);
// completes only with error
}
gaugeMetric.ifPresent(g -> g.incrementMetric(METRICS_POD_STARTED_SUCCESS_KEY));
return null;
}, executor);
}
Aggregations