Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds shutdown hook #11

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@
<version>2.5</version>
</dependency>

<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>

<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
Expand Down Expand Up @@ -163,6 +169,14 @@
<tag>${project.version}</tag>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redis.embedded;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.embedded.exceptions.EmbeddedRedisException;

import java.io.*;
Expand All @@ -11,9 +13,12 @@
import java.util.concurrent.Executors;

abstract class AbstractRedisInstance implements Redis {

private static Log log = LogFactory.getLog(AbstractRedisInstance.class);

protected List<String> args = Collections.emptyList();
private volatile boolean active = false;
private Process redisProcess;
private Process redisProcess;
private final int port;

private ExecutorService executor;
Expand All @@ -32,6 +37,7 @@ public synchronized void start() throws EmbeddedRedisException {
}
try {
redisProcess = createRedisProcessBuilder().start();
installExitHook();
logErrors();
awaitRedisServerReady();
active = true;
Expand All @@ -40,6 +46,10 @@ public synchronized void start() throws EmbeddedRedisException {
}
}

private void installExitHook() {
Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "RedisInstanceCleaner"));
}

private void logErrors() {
final InputStream errorStream = redisProcess.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream));
Expand All @@ -57,9 +67,8 @@ private void awaitRedisServerReady() throws IOException {
outputLine = reader.readLine();
if (outputLine == null) {
//Something goes wrong. Stream is ended before server was activated.
throw new RuntimeException("Can't start redis server. Check logs for details. Redis process log: "+outputStringBuffer.toString());
}
else{
throw new RuntimeException("Can't start redis server. Check logs for details. Redis process log: " + outputStringBuffer.toString());
} else {
outputStringBuffer.append("\n");
outputStringBuffer.append(outputLine);
}
Expand All @@ -80,11 +89,14 @@ private ProcessBuilder createRedisProcessBuilder() {

public synchronized void stop() throws EmbeddedRedisException {
if (active) {
log.info("Stopping redis server...");

if (executor != null && !executor.isShutdown()) {
executor.shutdown();
}
redisProcess.destroy();
tryWaitFor();
log.info("Redis exited");
active = false;
}
}
Expand Down