Skip to content

Commit e4acbc7

Browse files
committed
Fixed bug in redis instance logging
1 parent 391d5ea commit e4acbc7

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/main/java/redis/embedded/RedisInstance.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package redis.embedded;
22

3-
import redis.embedded.util.CheckedRunnable;
4-
53
import java.io.File;
64
import java.io.IOException;
7-
import java.util.Arrays;
85
import java.util.Collections;
96
import java.util.List;
107
import java.util.function.Consumer;
@@ -43,25 +40,23 @@ public synchronized void start() throws IOException {
4340
.directory(new File(args.get(0)).getParentFile())
4441
.start();
4542
addShutdownHook("RedisInstanceCleaner", checkedToRuntime(this::stop));
46-
final String startupLog = awaitServerReady(process, readyPattern);
47-
if (soutListener != null) {
48-
soutListener.accept(startupLog);
49-
newDaemonThread(() -> logStream(process.getInputStream(), soutListener)).start();
50-
}
5143
if (serrListener != null)
5244
newDaemonThread(() -> logStream(process.getErrorStream(), serrListener)).start();
45+
awaitServerReady(process, readyPattern, soutListener);
46+
if (soutListener != null)
47+
newDaemonThread(() -> logStream(process.getInputStream(), soutListener)).start();
5348

5449
active = true;
5550
} catch (final IOException e) {
5651
throw new IOException("Failed to start Redis service", e);
5752
}
5853
}
5954

60-
private static String awaitServerReady(final Process process, final Pattern readyPattern) throws IOException {
55+
private static void awaitServerReady(final Process process, final Pattern readyPattern,
56+
final Consumer<String> soutListener) throws IOException {
6157
final StringBuilder log = new StringBuilder();
62-
if (!findMatchInStream(process.getInputStream(), readyPattern, log))
58+
if (!findMatchInStream(process.getInputStream(), readyPattern, soutListener, log))
6359
throw new IOException("Ready pattern not found in log. Startup log: " + log);
64-
return log.toString();
6560
}
6661

6762
public synchronized void stop() throws IOException {

src/main/java/redis/embedded/util/IO.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public static Thread newDaemonThread(final Runnable run) {
5858
return thread;
5959
}
6060

61-
public static boolean findMatchInStream(final InputStream in, final Pattern pattern,
62-
final StringBuilder processOutput) throws IOException {
61+
public static boolean findMatchInStream(final InputStream in, final Pattern pattern
62+
, final Consumer<String> soutListener, final StringBuilder processOutput) throws IOException {
6363
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
6464
String line; while ((line = reader.readLine()) != null) {
65+
if (soutListener != null) soutListener.accept(line);
6566
processOutput.append('\n').append(line);
6667
if (pattern.matcher(line).matches())
6768
return true;

0 commit comments

Comments
 (0)