Skip to content

Commit

Permalink
Make <time> in <wait> a timeout
Browse files Browse the repository at this point in the history
In case there are URL or Log checks, <time> now works as a timeout instead of merely waiting until the end.  Fixes #173
  • Loading branch information
rhuss committed Jun 14, 2015
1 parent 27e2bec commit 31ccaf0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Allow CMD and ENTRYPOINT with shell and exec arguments (#130, #149)
- Unix Socket support (#179)
- Add a new parameter 'skipTags' for avoiding configured tagging of images (#145)
- Break build if log check or URL check runs into a timeout (#173)

Please note that for consistencies sake `<command>` has been renamed to `<cmd>` which contains inner elements
to match better the equivalent Dockerfile argument. The update should be trivial and easy to spot since a build will croak immediately.
Expand Down
8 changes: 4 additions & 4 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ As a convenience, a hostname pointing to the docker host may also
be specified. The container will fail to start if the hostname resolves
to an ip address of something other then the docker host itself.

```
```xml
<ports>
<port>docker.example.com:80:80</port>
</ports>
Expand Down Expand Up @@ -719,7 +719,7 @@ should even work for boot2docker:
<volume>${project.basedir}/data:/data</volume>
</bind>
</volumes>
````xml
````

##### Container restart policy

Expand Down Expand Up @@ -755,8 +755,8 @@ some condition is met. These conditions can be specified within a

As soon as one condition is met the build continues. If you add a
`<time>` constraint this works more or less as a timeout for other
conditions. Please note, that the wait mechanism never aborts a build, but only
waits until one of the conditions occurs.
conditions. The build will abort if you wait on an url or log output and reach the timeout.
If only a `<time>` is specified, the build will wait that amount of milliseconds and then continues.

Example:

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/jolokia/docker/maven/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
*/

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -361,7 +359,7 @@ private void startContainer(DockerAccess docker, String id) throws DockerAccessE
containersStarted.add(id);
}

private void waitIfRequested(DockerAccess docker, RunImageConfiguration runConfig, Properties projectProperties, String containerId) {
private void waitIfRequested(DockerAccess docker, RunImageConfiguration runConfig, Properties projectProperties, String containerId) throws MojoExecutionException {
WaitConfiguration wait = runConfig.getWaitConfiguration();
if (wait != null) {
ArrayList<WaitUtil.WaitChecker> checkers = new ArrayList<>();
Expand All @@ -375,8 +373,13 @@ private void waitIfRequested(DockerAccess docker, RunImageConfiguration runConfi
checkers.add(getLogWaitChecker(wait.getLog(), docker, containerId));
logOut.add("on log out '" + wait.getLog() + "'");
}
long waited = WaitUtil.wait(wait.getTime(), checkers.toArray(new WaitUtil.WaitChecker[0]));
log.info("Waited " + StringUtils.join(logOut.toArray(), " and ") + " " + waited + " ms");
try {
long waited = WaitUtil.wait(wait.getTime(), checkers.toArray(new WaitUtil.WaitChecker[0]));
log.info("Waited " + StringUtils.join(logOut.toArray(), " and ") + " " + waited + " ms");
} catch (TimeoutException exp) {
log.error("Timeout after " + wait.getTime() + " ms while waiting on " + StringUtils.join(logOut.toArray(), " and "));
throw new MojoExecutionException("Timeout received after " + wait.getTime());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public LogRequestor(HttpClient client, UrlBuilder urlBuilder, String containerId
this.containerId = containerId;

this.urlBuilder = urlBuilder;



this.callback = callback;
this.exception = null;
this.setDaemon(true);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/jolokia/docker/maven/util/WaitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeoutException;

/**
* @author roland
Expand All @@ -21,7 +22,7 @@ public class WaitUtil {

private WaitUtil() {}

public static long wait(int maxWait,WaitChecker ... checkers) {
public static long wait(int maxWait, WaitChecker ... checkers) throws TimeoutException {
long max = maxWait > 0 ? maxWait : HARD_MAX_WAIT;
long now = System.currentTimeMillis();
do {
Expand All @@ -33,6 +34,11 @@ public static long wait(int maxWait,WaitChecker ... checkers) {
}
sleep(WAIT_RETRY_WAIT);
} while (delta(now) < max);
if (checkers.length > 0) {
// There has been several checkes, but none has matched. So we ware throwing an exception and break
// the build
throw new TimeoutException("No checker finished successfully");
}
return delta(now);
}

Expand Down Expand Up @@ -74,6 +80,7 @@ public static class HttpPingChecker implements WaitChecker {
* Ping the given URL
*
* @param url URL to check
* @param timeout
*/
public HttpPingChecker(String url) {
this.url = url;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/jolokia/docker/maven/util/WaitUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

import com.sun.net.httpserver.*;
import org.junit.BeforeClass;
Expand All @@ -22,15 +23,14 @@ public class WaitUtilTest {
static String httpPingUrl;


@Test
public void httpFail() {
@Test(expected = TimeoutException.class)
public void httpFail() throws TimeoutException {
WaitUtil.HttpPingChecker checker = new WaitUtil.HttpPingChecker(httpPingUrl);
long waited = WaitUtil.wait(500,checker);
assertTrue("Waited only " + waited + " instead of min. 500ms", waited >= 500);
}

@Test
public void httpSuccess() {
public void httpSuccess() throws TimeoutException {
server.start();
System.out.println("Check URL " + httpPingUrl);
WaitUtil.HttpPingChecker checker = new WaitUtil.HttpPingChecker(httpPingUrl);
Expand Down

0 comments on commit 31ccaf0

Please sign in to comment.