Skip to content

Commit

Permalink
Added force stop option in builder
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonstur committed Oct 4, 2022
1 parent 766e32d commit b1678e0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
19 changes: 15 additions & 4 deletions src/main/java/redis/embedded/RedisInstance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package redis.embedded;

import redis.embedded.util.CheckedRunnable;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
Expand All @@ -14,14 +16,16 @@ public abstract class RedisInstance implements Redis {
private final Pattern readyPattern;
private final int port;
private final List<String> args;
private final boolean forceStop;

private volatile boolean active = false;
private Process process;

protected RedisInstance(final int port, final List<String> args, final Pattern readyPattern) {
protected RedisInstance(final int port, final List<String> args, final Pattern readyPattern, final boolean forceStop) {
this.port = port;
this.args = args;
this.readyPattern = readyPattern;
this.forceStop = forceStop;
}

public synchronized void start() throws IOException {
Expand All @@ -31,7 +35,10 @@ public synchronized void start() throws IOException {
process = new ProcessBuilder(args)
.directory(new File(args.get(0)).getParentFile())
.start();
addShutdownHook("RedisInstanceCleaner", checkedToRuntime(this::stop));
addShutdownHook("RedisInstanceCleaner", checkedToRuntime(new CheckedRunnable() {
public void run() throws Exception {
}
}));
logStream(process.getErrorStream(), System.out::println);
awaitServerReady();

Expand All @@ -51,8 +58,12 @@ public synchronized void stop() throws IOException {
if (!active) return;

try {
process.destroy();
process.waitFor();
if (forceStop)
process.destroyForcibly();
else {
process.destroy();
process.waitFor();
}
active = false;
} catch (InterruptedException e) {
throw new IOException("Failed to stop redis service", e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/embedded/RedisSentinel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public final class RedisSentinel extends RedisInstance {

public RedisSentinel(final int port, final List<String> args) {
super(port, args, SENTINEL_READY_PATTERN);
public RedisSentinel(final int port, final List<String> args, final boolean forceStop) {
super(port, args, SENTINEL_READY_PATTERN, forceStop);
}

public static RedisSentinelBuilder newRedisSentinel() { return new RedisSentinelBuilder(); }
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/redis/embedded/RedisServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ public RedisServer() throws IOException {
}

public RedisServer(final int port) throws IOException {
this(port, newRedisServer().port(port).buildCommandArgs());
this(port, newRedisServer().port(port).buildCommandArgs(), false);
}

public RedisServer(final int port, final File executable) {
this(port, Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
));
this( port
, Arrays.asList(executable.getAbsolutePath(), "--port", Integer.toString(port))
, false
);
}

public RedisServer(final int port, final ExecutableProvider executableProvider) throws IOException {
this(port, Arrays.asList(
executableProvider.get().getAbsolutePath(),
"--port", Integer.toString(port)
));
this( port
, Arrays.asList(executableProvider.get().getAbsolutePath(), "--port", Integer.toString(port))
, false
);
}

public RedisServer(final int port, final List<String> args) {
super(port, args, SERVER_READY_PATTERN);
public RedisServer(final int port, final List<String> args, final boolean forceStop) {
super(port, args, SERVER_READY_PATTERN, forceStop);
}

public static RedisServerBuilder newRedisServer() {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/redis/embedded/core/RedisSentinelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class RedisSentinelBuilder {
private int parallelSyncs = 1;
private int quorumSize = 1;
private String sentinelConf;
private boolean forceStop = false;

private StringBuilder redisConfigBuilder;

Expand Down Expand Up @@ -107,9 +108,14 @@ public RedisSentinelBuilder setting(final String configLine) {
return this;
}

public RedisSentinelBuilder onShutdownForceStop(final boolean forceStop) {
this.forceStop = forceStop;
return this;
}

public RedisSentinel build() {
tryResolveConfAndExec();
return new RedisSentinel(port, buildCommandArgs());
return new RedisSentinel(port, buildCommandArgs(), forceStop);
}

private void tryResolveConfAndExec() {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/redis/embedded/core/RedisServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class RedisServerBuilder {
private String bindAddress = "127.0.0.1";
private int bindPort = DEFAULT_REDIS_PORT;
private InetSocketAddress slaveOf;
private boolean forceStop = false;

private StringBuilder redisConfigBuilder = new StringBuilder();

Expand Down Expand Up @@ -70,8 +71,13 @@ public RedisServerBuilder setting(final String configLine) {
return this;
}

public RedisServerBuilder onShutdownForceStop(final boolean forceStop) {
this.forceStop = forceStop;
return this;
}

public RedisServer build() throws IOException {
return new RedisServer(bindPort, buildCommandArgs());
return new RedisServer(bindPort, buildCommandArgs(), forceStop);
}

public void reset() {
Expand Down

0 comments on commit b1678e0

Please sign in to comment.