Skip to content

Commit

Permalink
Allow changing the "stdout readiness check" in order to handle differ…
Browse files Browse the repository at this point in the history
…ent redis versions
  • Loading branch information
ikempf committed May 23, 2019
1 parent b855803 commit c8ab6a3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ abstract class AbstractRedisInstance implements Redis {
private volatile boolean active = false;
private Process redisProcess;
private final int port;
private String redisReadyPattern;

private ExecutorService executor;

protected AbstractRedisInstance(int port) {
protected AbstractRedisInstance(int port, String redisReadyPattern) {
this.port = port;
this.redisReadyPattern = redisReadyPattern;
}

@Override
Expand Down Expand Up @@ -61,14 +63,12 @@ private void awaitRedisServerReady() throws IOException {
//Something goes wrong. Stream is ended before server was activated.
throw new RuntimeException("Can't start redis server. Check logs for details.");
}
} while (!outputLine.matches(redisReadyPattern()));
} while (!outputLine.matches(redisReadyPattern));
} finally {
IOUtils.closeQuietly(reader);
}
}

protected abstract String redisReadyPattern();

private ProcessBuilder createRedisProcessBuilder() {
File executable = new File(args.get(0));
ProcessBuilder pb = new ProcessBuilder(args);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/redis/embedded/RedisSentinel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import java.util.List;

public class RedisSentinel extends AbstractRedisInstance {
private static final String REDIS_READY_PATTERN = ".*Sentinel runid is.*";
static final String REDIS_SENTINEL_READY_PATTERN = ".*Sentinel runid is.*";

public RedisSentinel(List<String> args, int port) {
super(port);
this(args, port, REDIS_SENTINEL_READY_PATTERN);
}

public RedisSentinel(List<String> args, int port, String readyPattern) {
super(port, readyPattern);
this.args = new ArrayList<String>(args);
}

public static RedisSentinelBuilder builder() { return new RedisSentinelBuilder(); }

@Override
protected String redisReadyPattern() {
return REDIS_READY_PATTERN;
}
}
14 changes: 12 additions & 2 deletions src/main/java/redis/embedded/RedisSentinelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class RedisSentinelBuilder {
private File executable;
private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider();
private Integer port = 26379;
private int masterPort = 6379;
private String readyPattern = RedisSentinel.REDIS_SENTINEL_READY_PATTERN;
private int masterPort = RedisServer.DEFAULT_REDIS_PORT;
private String masterName = "mymaster";
private long downAfterMilliseconds = 60000L;
private long failoverTimeout = 180000L;
Expand All @@ -42,6 +43,11 @@ public RedisSentinelBuilder port(Integer port) {
return this;
}

public RedisSentinelBuilder readyPattern(String readyPattern) {
this.readyPattern = readyPattern;
return this;
}

public RedisSentinelBuilder masterPort(Integer masterPort) {
this.masterPort = masterPort;
return this;
Expand Down Expand Up @@ -97,7 +103,11 @@ public RedisSentinelBuilder setting(String configLine) {
public RedisSentinel build() {
tryResolveConfAndExec();
List<String> args = buildCommandArgs();
return new RedisSentinel(args, port);
if (this.readyPattern == null) {
return new RedisSentinel(args, port);
} else {
return new RedisSentinel(args, port, readyPattern);
}
}

private void tryResolveConfAndExec() {
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 @@ -7,15 +7,15 @@
import java.util.List;

public class RedisServer extends AbstractRedisInstance {
private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*";
private static final int DEFAULT_REDIS_PORT = 6379;
static final String REDIS_STANDALONE_READY_PATTERN = ".*The server is now ready to accept connections on port.*";
static final int DEFAULT_REDIS_PORT = 6379;

public RedisServer() throws IOException {
this(DEFAULT_REDIS_PORT);
}

public RedisServer(int port) throws IOException {
super(port);
super(port, REDIS_STANDALONE_READY_PATTERN);
File executable = RedisExecProvider.defaultProvider().get();
this.args = Arrays.asList(
executable.getAbsolutePath(),
Expand All @@ -24,32 +24,32 @@ public RedisServer(int port) throws IOException {
}

public RedisServer(File executable, int port) {
super(port);
super(port, REDIS_STANDALONE_READY_PATTERN);
this.args = Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
);
}

public RedisServer(RedisExecProvider redisExecProvider, int port) throws IOException {
super(port);
super(port, REDIS_STANDALONE_READY_PATTERN);
this.args = Arrays.asList(
redisExecProvider.get().getAbsolutePath(),
"--port", Integer.toString(port)
);
}

RedisServer(List<String> args, int port) {
super(port);
public RedisServer(List<String> args, int port) {
this(args, port, REDIS_STANDALONE_READY_PATTERN);
}

public RedisServer(List<String> args, int port, String readyPattern) {
super(port, readyPattern);
this.args = new ArrayList<String>(args);
}

public static RedisServerBuilder builder() {
return new RedisServerBuilder();
}

@Override
protected String redisReadyPattern() {
return REDIS_READY_PATTERN;
}
}
10 changes: 8 additions & 2 deletions src/main/java/redis/embedded/RedisServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class RedisServerBuilder {

private File executable;
private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider();
private int port = 6379;
private int port = RedisServer.DEFAULT_REDIS_PORT;
private String readyPattern = RedisServer.REDIS_STANDALONE_READY_PATTERN;
private InetSocketAddress slaveOf;
private String redisConf;

Expand All @@ -33,6 +34,11 @@ public RedisServerBuilder port(int port) {
return this;
}

public RedisServerBuilder readyPattern(String readyPattern) {
this.readyPattern = readyPattern;
return this;
}

public RedisServerBuilder slaveOf(String hostname, int port) {
this.slaveOf = new InetSocketAddress(hostname, port);
return this;
Expand Down Expand Up @@ -68,7 +74,7 @@ public RedisServerBuilder setting(String configLine) {
public RedisServer build() {
tryResolveConfAndExec();
List<String> args = buildCommandArgs();
return new RedisServer(args, port);
return new RedisServer(args, port, readyPattern);
}

public void reset() {
Expand Down

0 comments on commit c8ab6a3

Please sign in to comment.