Skip to content

Commit

Permalink
Ws2020 docker (dirkriehle#6)
Browse files Browse the repository at this point in the history
* Add Dockerfile
* Extend DatabaseConnection with method to wait for db
* Adjust AbstractMain to check DB connection
* Add Wahlzeit app to docker-compose
* Adjust Dockefile
* Refactor AbstractMain to throw runtime error if db is not abailable.
* Extend JavaDoc of DatabaseConnection
  • Loading branch information
andreas-bauer committed Sep 11, 2020
1 parent eccde5a commit 71145ae
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 26 deletions.
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#########################################################
# First stage: image to build the application #
#########################################################
FROM adoptopenjdk/openjdk11-openj9:alpine-slim as builder

WORKDIR /builder

# Copy Gradle file
COPY *.gradle /builder/

# Copy sources
COPY src /builder/src

# Copy Gradle resources
COPY gradle /builder/gradle
COPY gradlew /builder/gradlew

# Test project
RUN ./gradlew test

# Build project
RUN ./gradlew assemble

#########################################################
# Second stage: image to run the application #
#########################################################
FROM jetty:9.4-jre11-slim


# Pull the built files from the builder container
COPY --from=builder /builder/build/libs/*.war /var/lib/jetty/webapps/wahlzeit.war

# Expose port
EXPOSE 8080
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
version: '3.7'

services:
app:
image: wahlzeit
build:
context: .
environment:
WAHLZEIT_DB_HOST: "db"
ports:
- 8080:8080
db:
image: postgres:12-alpine
environment:
Expand Down
31 changes: 10 additions & 21 deletions src/main/java/org/wahlzeit/main/AbstractMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

package org.wahlzeit.main;

import java.util.Optional;

import org.wahlzeit.services.*;

/**
* A superclass for a Main class for system startup and shutdown.
*/
public abstract class AbstractMain {

private static final String DB_HOST = Optional.ofNullable(System.getenv("WAHLZEIT_DB_HOST")).orElse("localhost");

/**
*
*/
Expand All @@ -43,32 +47,17 @@ protected AbstractMain() {
*
*/
protected void startUp(String rootDir) throws Exception {
SysConfig.setInstance(createSysConfig(rootDir));
SysConfig.setInstance(new SysConfig(rootDir, DB_HOST));

boolean dbAvailable = DatabaseConnection.waitForDatabaseIsReady(30, 1000);
if (!dbAvailable) {
throw new RuntimeException("Unable to proceed with wahlzeit app. DB connection could not be established.");
}

mainSession = new SysSession("system");
SessionManager.setThreadLocalSession(mainSession);
}

/**
*
*/
protected SysConfig createSysConfig(String rootDir) {
return createDevSysConfig(rootDir);
}
/**
*
*/
protected SysConfig createProdSysConfig(String rootDir) {
return new SysConfig(rootDir);
}

/**
*
*/
protected SysConfig createDevSysConfig(String rootDir) {
return new SysConfig(rootDir);
}

/**
*
*/
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/wahlzeit/services/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ public static synchronized DatabaseConnection ensureDatabaseConnection() throws

return result;
}

/**
* Wait until a database connection can be established or the max retry counter is reached.
*
* @param retries amount of retries until fail.
* @param sleepTimeBetweenRetries time to wait until next retry in milliseconds.
* @return true if connection could be established, otherwise return false.
*/
public static synchronized boolean waitForDatabaseIsReady(final int retries, final int sleepTimeBetweenRetries) {
int retryCounter = retries;
String dbUrl = SysConfig.getDbConnectionAsString();
do {
try {
DatabaseConnection.ensureDatabaseConnection();
SysLog.logSysInfo("[success] Service check for URL " + dbUrl);
return true;
} catch (final SQLException e) {
final String msg = String.format("[%d/%d] Retry service check for URL %s", retryCounter, retries, dbUrl);
SysLog.logSysInfo(msg);
doSleep(sleepTimeBetweenRetries);
}

retryCounter--;
} while (retryCounter > 0);

SysLog.logSysError("[failure] Service check for URL " + dbUrl);
return false;
}

private static void doSleep(final int sleepTime) {
try {
Thread.sleep(sleepTime);
} catch (final InterruptedException e) {
}
}

/**
*
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/wahlzeit/services/SysConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SysConfig extends AbstractConfig {
public static SysConfig getInstance() {
if (instance == null) {
SysLog.logSysInfo("creating generic SysConfig");
setInstance(new SysConfig(""));
setInstance(new SysConfig());
}
return instance;
}
Expand Down Expand Up @@ -104,13 +104,13 @@ public static synchronized void dropInstance() {
*
*/
public SysConfig() {
this("");
this("", "localhost");
}

/**
*
*/
public SysConfig(String myRootDir) {
public SysConfig(String myRootDir, String dbHostName) {
// Root directory
rootDir = myRootDir;

Expand All @@ -126,7 +126,7 @@ public SysConfig(String myRootDir) {

// Database connection
doSetValue(SysConfig.DB_DRIVER, "org.postgresql.Driver");
doSetValue(SysConfig.DB_CONNECTION, "jdbc:postgresql://localhost:5432/wahlzeit");
doSetValue(SysConfig.DB_CONNECTION, "jdbc:postgresql://" + dbHostName + ":5432/wahlzeit");
doSetValue(SysConfig.DB_USER, "wahlzeit");
doSetValue(SysConfig.DB_PASSWORD, "wahlzeit");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wahlzeit.testEnvironmentProvider;

import java.util.Optional;

import org.junit.rules.ExternalResource;
import org.wahlzeit.services.SysConfig;
import org.wahlzeit.webparts.WebPartTemplateService;
Expand All @@ -10,11 +12,12 @@
* @review
*/
public class SysConfigProvider extends ExternalResource {
private static final String DB_HOST = Optional.ofNullable(System.getenv("WAHLZEIT_DB_HOST")).orElse("localhost");

@Override
protected void before() throws Throwable {
SysConfig.dropInstance();
SysConfig sysConfig = new SysConfig("src/main/webapp");
SysConfig sysConfig = new SysConfig("src/main/webapp", DB_HOST);
SysConfig.setInstance(sysConfig);
WebPartTemplateService.getInstance().setTemplatesDir(SysConfig.getTemplatesDir());
}
Expand Down

0 comments on commit 71145ae

Please sign in to comment.