Skip to content

Commit

Permalink
#14 The check now takes place on a more reliable text. The old text l…
Browse files Browse the repository at this point in the history
…eft a bit of wiggle room, resulting in a racing condition. A slow MacBook may see Spring Boot try to create a connection before Postgres is ready to accept it (see #8). Two problems:

* the startup verification text is printed twice -- only the second time counts
* Docker through the ProcessRunner did not print all active log to file

Extra field added to check the number of times the startup verification text must be found. The infinite tailer will keep track and return success when the designated number of texts have been spotted.

The second problem is addressed by adding a --tty flag to the docker run command.
  • Loading branch information
robert-bor committed Feb 13, 2017
1 parent 1ef9705 commit ff98003
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-docker-postgres</artifactId>
<version>0.5.0</version>
<version>0.5.1-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>nl.42</groupId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public DockerPostgresContainer execute() throws IOException, InterruptedExceptio
LOGGER.info("| * Port: " + properties.getPort());
LOGGER.info("| * Password: " + properties.getPassword());
LOGGER.info("| * Startup Verification Text: [" + properties.getStartupVerificationText() + "]");
LOGGER.info("| * Times expected verification text: " + properties.getTimesExpectedVerificationText() + "x");
LOGGER.info("| * After verification wait: " + properties.getAfterVerificationWait() + "ms");
LOGGER.info("| * Docker command: [" + properties.getDockerCommand() + "]");
LOGGER.info("| * Custom variables (" + properties.getCustomVariables().size() + ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public class DockerPostgresProperties {

private String containerName = "postgression";

private String startupVerificationText = "PostgreSQL init process complete; ready for start up.";
private String startupVerificationText = "database system is ready to accept connections";

private String dockerCommand = "docker run --rm -e POSTGRES_PASSWORD=${password} -p ${port}:5432 --name ${containerName} ${imageName}:${imageVersion}";
private Integer timesExpectedVerificationText = 2;

private String dockerCommand = "docker run --rm --tty -e POSTGRES_PASSWORD=${password} -p ${port}:5432 --name ${containerName} ${imageName}:${imageVersion}";

private Integer timeout = 300000; // 5 minutes because of time required for downloading?

Expand Down Expand Up @@ -148,6 +150,14 @@ public void setAfterVerificationWait(Integer afterVerificationWait) {
this.afterVerificationWait = afterVerificationWait;
}

public Integer getTimesExpectedVerificationText() {
return timesExpectedVerificationText;
}

public void setTimesExpectedVerificationText(Integer timesExpectedVerificationText) {
this.timesExpectedVerificationText = timesExpectedVerificationText;
}

public Map<String, String> getProperties() {
Map<String,String> properties = new HashMap<>();
properties.put("stdOutFilename", getStdOutFilename());
Expand All @@ -159,6 +169,8 @@ public Map<String, String> getProperties() {
properties.put("imageName", getImageName());
properties.put("imageVersion", getImageVersion());
properties.put("startupVerificationText", getStartupVerificationText());
properties.put("timesExpectedVerificationText", getTimesExpectedVerificationText().toString());
properties.put("afterVerificationWait", getAfterVerificationWait().toString());
properties.put("dockerCommand", getDockerCommand());
properties.put("forceClean", Boolean.toString(isForceClean()));
properties.put("afterVerificationWait", Boolean.toString(isForceClean()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public class DockerInfiniteProcessTailer {
private final String dockerStandardOutFilename;
private final String dockerStandardErrorFilename;
private final String startupVerificationText;
private final Integer timesExpectedVerificationText;
private final Integer timeout;

private Integer sleepTime = 0;
private Integer verificationTextEncountered = 0;

public DockerInfiniteProcessTailer( Thread dockerThread,
DockerPostgresProperties properties,
Expand All @@ -25,6 +27,7 @@ public DockerInfiniteProcessTailer( Thread dockerThread,
this.dockerStandardOutFilename = properties.getStdOutFilename();
this.dockerStandardErrorFilename = properties.getStdErrFilename();
this.startupVerificationText = properties.getStartupVerificationText();
this.timesExpectedVerificationText = properties.getTimesExpectedVerificationText();
this.timeout = imageDownloaded ? properties.getTimeout() : -1;
LOGGER.info("| Applied timeout: (-1 means no timeout): " + this.timeout);
}
Expand All @@ -44,13 +47,17 @@ public boolean verify() throws IOException {
if( reader.available() > 0 ) {
char readChar = (char)reader.read();
if (readChar == '\n') {
LOGGER.info("| > " + line.toString());
if (line.toString().contains(startupVerificationText)) {
LOGGER.info("| > " + line.toString());
LOGGER.info("| = Docker startup verification text found");
logErrorLinesAsWarning();
return true;
verificationTextEncountered++;
LOGGER.info("| = Verification text encountered " + verificationTextEncountered + "x");
if (verificationTextEncountered == timesExpectedVerificationText) {
LOGGER.info("| > " + line.toString());
LOGGER.info("| = Docker startup verification text found");
logErrorLinesAsWarning();
return true;
}
}
LOGGER.info("| > " + line.toString());
line = new StringBuilder();
} else {
line.append(readChar);
Expand Down

0 comments on commit ff98003

Please sign in to comment.