From 5284176ff8d8296866f94687542e9893ee7a6926 Mon Sep 17 00:00:00 2001 From: Dmitry Gusev Date: Tue, 18 Apr 2017 18:11:34 +0300 Subject: [PATCH 1/2] Proposed fix for #830 --- .../command/BuildImageResultCallback.java | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java b/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java index 1ca276434..76018cada 100644 --- a/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java +++ b/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java @@ -5,14 +5,13 @@ import java.util.concurrent.TimeUnit; -import javax.annotation.CheckForNull; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.github.dockerjava.api.exception.DockerClientException; import com.github.dockerjava.api.model.BuildResponseItem; import com.github.dockerjava.core.async.ResultCallbackTemplate; +import com.google.common.collect.EvictingQueue; /** * @@ -23,12 +22,12 @@ public class BuildImageResultCallback extends ResultCallbackTemplate latestItems = EvictingQueue.create(2); @Override public void onNext(BuildResponseItem item) { - this.latestItem = item; + latestItems.add(item); LOGGER.debug(item.toString()); } @@ -65,13 +64,37 @@ public String awaitImageId(long timeout, TimeUnit timeUnit) { } private String getImageId() { - if (latestItem == null) { - throw new DockerClientException("Could not build image"); - } else if (!latestItem.isBuildSuccessIndicated()) { - throw new DockerClientException("Could not build image: " + latestItem.getError()); - } else { - return latestItem.getImageId(); + BuildResponseItem buildSuccessItem = findBuildSuccessItem(); + + if (buildSuccessItem == null) { + BuildResponseItem errorItem = findErrorItem(); + if (errorItem == null) { + throw new DockerClientException("Could not build image"); + } else { + throw new DockerClientException("Could not build image: " + errorItem.getError()); + } + } + + return buildSuccessItem.getImageId(); + } + + private BuildResponseItem findBuildSuccessItem() { + for (BuildResponseItem item : latestItems) { + if (item.isBuildSuccessIndicated()) { + return item; + } } + + return null; } + private BuildResponseItem findErrorItem() { + for (BuildResponseItem item : latestItems) { + if (item.isErrorIndicated()) { + return item; + } + } + + return null; + } } From 6559f35a12ba0290f8e4fbac4eaf281ee817aa3e Mon Sep 17 00:00:00 2001 From: Dmitry Gusev Date: Tue, 18 Apr 2017 23:28:08 +0300 Subject: [PATCH 2/2] Simplify the fix for #830 --- .../command/BuildImageResultCallback.java | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java b/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java index 76018cada..e622c4725 100644 --- a/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java +++ b/src/main/java/com/github/dockerjava/core/command/BuildImageResultCallback.java @@ -11,7 +11,6 @@ import com.github.dockerjava.api.exception.DockerClientException; import com.github.dockerjava.api.model.BuildResponseItem; import com.github.dockerjava.core.async.ResultCallbackTemplate; -import com.google.common.collect.EvictingQueue; /** * @@ -22,12 +21,17 @@ public class BuildImageResultCallback extends ResultCallbackTemplate latestItems = EvictingQueue.create(2); + private String imageId; + + private String error; @Override public void onNext(BuildResponseItem item) { - latestItems.add(item); + if (item.isBuildSuccessIndicated()) { + this.imageId = item.getImageId(); + } else if (item.isErrorIndicated()) { + this.error = item.getError(); + } LOGGER.debug(item.toString()); } @@ -64,37 +68,14 @@ public String awaitImageId(long timeout, TimeUnit timeUnit) { } private String getImageId() { - BuildResponseItem buildSuccessItem = findBuildSuccessItem(); - - if (buildSuccessItem == null) { - BuildResponseItem errorItem = findErrorItem(); - if (errorItem == null) { - throw new DockerClientException("Could not build image"); - } else { - throw new DockerClientException("Could not build image: " + errorItem.getError()); - } + if (imageId != null) { + return imageId; } - return buildSuccessItem.getImageId(); - } - - private BuildResponseItem findBuildSuccessItem() { - for (BuildResponseItem item : latestItems) { - if (item.isBuildSuccessIndicated()) { - return item; - } - } - - return null; - } - - private BuildResponseItem findErrorItem() { - for (BuildResponseItem item : latestItems) { - if (item.isErrorIndicated()) { - return item; - } + if (error == null) { + throw new DockerClientException("Could not build image"); } - return null; + throw new DockerClientException("Could not build image: " + error); } }