use of io.fabric8.kubernetes.client.dsl.ExecListener in project zalenium by zalando.
the class KubernetesContainerClient method executeCommand.
@Override
public void executeCommand(String containerId, String[] command, boolean waitForExecution) {
final CountDownLatch latch = new CountDownLatch(1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
logger.info(String.format("%s %s", containerId, Arrays.toString(command)));
ExecWatch exec = client.pods().withName(containerId).writingOutput(baos).writingError(baos).usingListener(new ExecListener() {
@Override
public void onOpen(Response response) {
}
@Override
public void onFailure(Throwable t, Response response) {
logger.error(String.format("%s Failed to execute command %s", containerId, Arrays.toString(command)), t);
latch.countDown();
}
@Override
public void onClose(int code, String reason) {
latch.countDown();
}
}).exec(command);
Supplier<Void> waitForResultsAndCleanup = () -> {
try {
latch.await();
} catch (InterruptedException e) {
logger.error(String.format("%s Failed to execute command %s", containerId, Arrays.toString(command)), e);
} finally {
exec.close();
}
logger.info(String.format("%s %s", containerId, baos.toString()));
return null;
};
if (waitForExecution) {
// If we're going to wait, let's use the same thread
waitForResultsAndCleanup.get();
} else {
// Let the common ForkJoinPool handle waiting for the results, since we don't care when it finishes.
CompletableFuture.supplyAsync(waitForResultsAndCleanup);
}
}
Aggregations