Skip to content

Commit

Permalink
Allow customize working directory (opentable#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebell90 authored Dec 26, 2018
1 parent f0e1cd0 commit 6b22f80
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.12.10
-------
* Add builder option to override working directory (otherwise uses java.io.tmpdir or the ot.epg.working-dir system properties)

0.12.9
------
* PR #83 - Logging of initDB process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -106,18 +107,21 @@ public class EmbeddedPostgres implements Closeable
Map<String, String> postgresConfig, Map<String, String> localeConfig, int port, Map<String, String> connectConfig,
PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector, ProcessBuilder.Redirect outputRedirector) throws IOException
{
this(parentDirectory, dataDirectory, cleanDataDirectory, postgresConfig, localeConfig, port, connectConfig, pgBinaryResolver, errorRedirector, outputRedirector, DEFAULT_PG_STARTUP_WAIT);
this(parentDirectory, dataDirectory, cleanDataDirectory, postgresConfig, localeConfig, port, connectConfig,
pgBinaryResolver, errorRedirector, outputRedirector, DEFAULT_PG_STARTUP_WAIT, Optional.empty());
}

EmbeddedPostgres(File parentDirectory, File dataDirectory, boolean cleanDataDirectory,
Map<String, String> postgresConfig, Map<String, String> localeConfig, int port, Map<String, String> connectConfig,
PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector, ProcessBuilder.Redirect outputRedirector, Duration pgStartupWait) throws IOException
PgBinaryResolver pgBinaryResolver, ProcessBuilder.Redirect errorRedirector,
ProcessBuilder.Redirect outputRedirector, Duration pgStartupWait,
Optional<File> overrideWorkingDirectory) throws IOException
{
this.cleanDataDirectory = cleanDataDirectory;
this.postgresConfig = new HashMap<>(postgresConfig);
this.localeConfig = new HashMap<>(localeConfig);
this.port = port;
this.pgDir = prepareBinaries(pgBinaryResolver);
this.pgDir = prepareBinaries(pgBinaryResolver, overrideWorkingDirectory);
this.errorRedirector = errorRedirector;
this.outputRedirector = outputRedirector;
this.pgStartupWait = pgStartupWait;
Expand Down Expand Up @@ -470,6 +474,7 @@ public static EmbeddedPostgres.Builder builder()
public static class Builder
{
private final File parentDirectory = getWorkingDirectory();
private Optional<File> overrideWorkingDirectory = Optional.empty(); // use tmpdir
private File builderDataDirectory;
private final Map<String, String> config = new HashMap<>();
private final Map<String, String> localeConfig = new HashMap<>();
Expand Down Expand Up @@ -530,6 +535,11 @@ public Builder setConnectConfig(String key, String value) {
return this;
}

public Builder setOverrideWorkingDirectory(File workingDirectory) {
overrideWorkingDirectory = Optional.ofNullable(workingDirectory);
return this;
}

public Builder setPort(int port) {
builderPort = port;
return this;
Expand Down Expand Up @@ -559,7 +569,9 @@ public EmbeddedPostgres start() throws IOException {
if (builderDataDirectory == null) {
builderDataDirectory = Files.createTempDirectory("epg").toFile();
}
return new EmbeddedPostgres(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config, localeConfig, builderPort, connectConfig, pgBinaryResolver, errRedirector, outRedirector, pgStartupWait);
return new EmbeddedPostgres(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config,
localeConfig, builderPort, connectConfig, pgBinaryResolver, errRedirector, outRedirector,
pgStartupWait, overrideWorkingDirectory);
}

@Override
Expand Down Expand Up @@ -699,7 +711,7 @@ private static void extractTxz(String tbzPath, String targetDir) throws IOExcept
}
}

private static File prepareBinaries(PgBinaryResolver pgBinaryResolver)
private static File prepareBinaries(PgBinaryResolver pgBinaryResolver, Optional<File> overrideWorkingDirectory)
{
PREPARE_BINARIES_LOCK.lock();
try {
Expand Down Expand Up @@ -735,7 +747,8 @@ private static File prepareBinaries(PgBinaryResolver pgBinaryResolver)

String pgDigest = Hex.encodeHexString(pgArchiveData.getMessageDigest().digest());

pgDir = new File(getWorkingDirectory(), String.format("PG-%s", pgDigest));
File workingDirectory = overrideWorkingDirectory.isPresent() ? overrideWorkingDirectory.get() : getWorkingDirectory();
pgDir = new File(workingDirectory, String.format("PG-%s", pgDigest));

mkdirs(pgDir);
final File unpackLockFile = new File(pgDir, LOCK_FILE_NAME);
Expand Down

0 comments on commit 6b22f80

Please sign in to comment.