Wait container command needs possibility to abort operation by marcuslinke · Pull Request #357 · docker-java/docker-java
9 changes: 6 additions & 3 deletions src/main/java/com/github/dockerjava/api/command/WaitContainerCmd.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/WaitResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.github.dockerjava.api.model; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
|
|
||
| /** | ||
| * Represents a wait container command response | ||
| */ | ||
| @JsonIgnoreProperties(ignoreUnknown = false) | ||
| public class WaitResponse { | ||
|
|
||
| @JsonProperty("StatusCode") | ||
| private Integer statusCode; | ||
|
|
||
| public Integer getStatusCode() { | ||
| return statusCode; | ||
| } | ||
| } |
22 changes: 21 additions & 1 deletion src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -8,10 +8,13 @@ | |
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.github.dockerjava.api.async.ResultCallback; | ||
| import com.google.common.base.Throwables; | ||
|
|
||
| /** | ||
| * Abstract template implementation of {@link ResultCallback} | ||
| Expand All | @@ -30,6 +33,8 @@ public abstract class ResultCallbackTemplate<RC_T extends ResultCallback<A_RES_T | |
|
|
||
| private boolean closed = false; | ||
|
|
||
| private Throwable firstError = null; | ||
|
|
||
| @Override | ||
| public void onStart(Closeable stream) { | ||
| this.stream = stream; | ||
| Expand All | @@ -38,12 +43,15 @@ public void onStart(Closeable stream) { | |
|
|
||
| @Override | ||
| public void onError(Throwable throwable) { | ||
|
|
||
| if (closed) | ||
| return; | ||
|
|
||
| if (this.firstError == null) | ||
| this.firstError = throwable; | ||
|
|
||
| try { | ||
| LOGGER.error("Error during callback", throwable); | ||
| throw new RuntimeException(throwable); | ||
| } finally { | ||
| try { | ||
| close(); | ||
| Expand Down Expand Up | @@ -76,6 +84,8 @@ public void close() throws IOException { | |
| @SuppressWarnings("unchecked") | ||
| public RC_T awaitCompletion() throws InterruptedException { | ||
| completed.await(); | ||
| // eventually (re)throws RuntimeException | ||
| getFirstError(); | ||
| return (RC_T) this; | ||
| } | ||
|
|
||
| Expand All | @@ -87,4 +97,14 @@ public RC_T awaitCompletion(long timeout, TimeUnit timeUnit) throws InterruptedE | |
| completed.await(timeout, timeUnit); | ||
| return (RC_T) this; | ||
| } | ||
|
|
||
| @CheckForNull | ||
| protected RuntimeException getFirstError() { | ||
Copy linkMember Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually mark |
||
| if (firstError != null) { | ||
| // this call throws a RuntimeException | ||
| return Throwables.propagate(firstError); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
5 changes: 3 additions & 2 deletions src/main/java/com/github/dockerjava/core/command/WaitContainerCmdImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Created on 21.07.2015 | ||
| */ | ||
| package com.github.dockerjava.core.command; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.github.dockerjava.api.DockerClientException; | ||
| import com.github.dockerjava.api.model.WaitResponse; | ||
| import com.github.dockerjava.core.async.ResultCallbackTemplate; | ||
| import com.google.common.base.Throwables; | ||
|
|
||
| /** | ||
| * | ||
| * @author marcus | ||
| * | ||
| */ | ||
| public class WaitContainerResultCallback extends ResultCallbackTemplate<WaitContainerResultCallback, WaitResponse> { | ||
|
|
||
| private final static Logger LOGGER = LoggerFactory.getLogger(WaitContainerResultCallback.class); | ||
|
|
||
| @CheckForNull | ||
| private WaitResponse waitResponse = null; | ||
|
|
||
| @Override | ||
| public void onNext(WaitResponse waitResponse) { | ||
| this.waitResponse = waitResponse; | ||
| LOGGER.debug(waitResponse.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Awaits the status code from the container. | ||
| * | ||
| * @throws DockerClientException | ||
| * if the wait operation fails. | ||
| */ | ||
| public Integer awaitStatusCode() { | ||
| try { | ||
| awaitCompletion(); | ||
| } catch (InterruptedException e) { | ||
| throw new DockerClientException("", e); | ||
| } | ||
|
|
||
| return getStatusCode(); | ||
| } | ||
|
|
||
| /** | ||
| * Awaits the status code from the container. | ||
| * | ||
| * @throws DockerClientException | ||
| * if the wait operation fails. | ||
| */ | ||
| public Integer awaitStatusCode(long timeout, TimeUnit timeUnit) { | ||
| try { | ||
| awaitCompletion(timeout, timeUnit); | ||
| } catch (InterruptedException e) { | ||
| throw new DockerClientException("Awaiting status code interrupted: ", e); | ||
| } | ||
|
|
||
| return getStatusCode(); | ||
| } | ||
|
|
||
| private Integer getStatusCode() { | ||
| if (waitResponse == null) { | ||
| throw new DockerClientException("Error while wait container"); | ||
| } else { | ||
| return waitResponse.getStatusCode(); | ||
| } | ||
| } | ||
| } |
27 changes: 18 additions & 9 deletions src/main/java/com/github/dockerjava/jaxrs/WaitContainerCmdExec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion src/test/java/com/github/dockerjava/client/DockerClientTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion src/test/java/com/github/dockerjava/core/command/FrameReaderITest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters