use of io.fabric8.kubernetes.api.builds.BuildWatcher in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method waitForOpenShiftBuildToComplete.
private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws MojoExecutionException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch logTerminateLatch = new CountDownLatch(1);
final String buildName = KubernetesHelper.getName(build);
final AtomicReference<Build> buildHolder = new AtomicReference<>();
// Don't query for logs directly, Watch over the build pod:
waitUntilPodIsReady(buildName + "-build", 20, log);
log.info("Waiting for build " + buildName + " to complete...");
try (LogWatch logWatch = client.pods().withName(buildName + "-build").watchLog()) {
KubernetesClientUtil.printLogsAsync(logWatch, "Failed to tail build log", logTerminateLatch, log);
Watcher<Build> buildWatcher = getBuildWatcher(latch, buildName, buildHolder);
try (Watch watcher = client.builds().withName(buildName).watch(buildWatcher)) {
// Check if the build is already finished to avoid waiting indefinitely
Build lastBuild = client.builds().withName(buildName).get();
String lastStatus = KubernetesResourceUtil.getBuildStatusPhase(lastBuild);
if (Builds.isFinished(lastStatus)) {
log.debug("Build %s is already finished", buildName);
buildHolder.set(lastBuild);
latch.countDown();
}
waitUntilBuildFinished(latch);
logTerminateLatch.countDown();
build = buildHolder.get();
String status = KubernetesResourceUtil.getBuildStatusPhase(build);
if (Builds.isFailed(status) || Builds.isCancelled(status)) {
throw new MojoExecutionException("OpenShift Build " + buildName + ": " + KubernetesResourceUtil.getBuildStatusReason(build));
}
log.info("Build " + buildName + " " + status);
}
}
}
use of io.fabric8.kubernetes.api.builds.BuildWatcher in project fabric8 by fabric8io.
the class WatchBuilds method main.
public static void main(String... args) {
String namespace = null;
if (args.length > 0) {
namespace = args[0];
}
String consoleLink = Links.getFabric8ConsoleLink();
OpenShiftClient client = new DefaultOpenShiftClient();
BuildListener buildListener = new BuildListener() {
@Override
public void onBuildFinished(BuildFinishedEvent event) {
System.out.println("Build: " + event.getUid() + " for config: " + event.getConfigName() + " finished. Status: " + event.getStatus() + " link: " + event.getBuildLink());
}
};
BuildWatcher watcher = new BuildWatcher(client, buildListener, namespace, consoleLink);
long pollTime = 3000;
watcher.schedule(pollTime);
watcher.join();
}
Aggregations