Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 0044685

Browse files
authored
More verbose logging to debug failures (refs #110) (#111)
1 parent 2f8cc2b commit 0044685

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/main/java/ru/yandex/qatools/embed/postgresql/InitDbProcess.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
22
* Copyright (C) 2011
3-
* Michael Mosmann <[email protected]>
4-
* Martin Jöhren <[email protected]>
5-
*
3+
* Michael Mosmann <[email protected]>
4+
* Martin Jöhren <[email protected]>
5+
* <p>
66
* with contributions from
7-
* konstantin-ba@github, Archimedes Trajano (trajano@github), Christian Bayer ([email protected])
8-
*
7+
* konstantin-ba@github, Archimedes Trajano (trajano@github), Christian Bayer ([email protected])
8+
* <p>
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
13-
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
12+
* <p>
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
* <p>
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,29 +25,32 @@
2525
import de.flapdoodle.embed.process.distribution.Distribution;
2626
import de.flapdoodle.embed.process.distribution.Platform;
2727
import de.flapdoodle.embed.process.extract.IExtractedFileSet;
28-
import de.flapdoodle.embed.process.io.LogWatchStreamProcessor;
2928
import de.flapdoodle.embed.process.io.Processors;
3029
import de.flapdoodle.embed.process.io.StreamToLineProcessor;
3130
import de.flapdoodle.embed.process.io.file.Files;
3231
import de.flapdoodle.embed.process.runtime.ProcessControl;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3334
import ru.yandex.qatools.embed.postgresql.config.PostgresConfig;
35+
import ru.yandex.qatools.embed.postgresql.ext.LogWatchStreamProcessor;
3436
import ru.yandex.qatools.embed.postgresql.ext.SubdirTempDir;
3537

3638
import java.io.File;
3739
import java.io.IOException;
3840
import java.util.ArrayList;
39-
import java.util.HashSet;
4041
import java.util.List;
4142

4243
import static de.flapdoodle.embed.process.io.file.Files.createTempFile;
4344
import static java.util.Arrays.asList;
45+
import static java.util.Collections.singleton;
4446
import static java.util.UUID.randomUUID;
4547

4648
/**
4749
* initdb process
4850
* (helper to initialize the DB)
4951
*/
5052
class InitDbProcess<E extends InitDbExecutable> extends AbstractPGProcess<E, InitDbProcess> {
53+
private static final Logger LOGGER = LoggerFactory.getLogger(InitDbProcess.class);
5154

5255
public InitDbProcess(Distribution distribution, PostgresConfig config, IRuntimeConfig runtimeConfig, E executable) throws IOException {
5356
super(distribution, config, runtimeConfig, executable);
@@ -80,10 +83,10 @@ protected List<String> getCommandLine(Distribution distribution, PostgresConfig
8083

8184
@Override
8285
protected void onAfterProcessStart(ProcessControl process, IRuntimeConfig runtimeConfig) throws IOException {
83-
ProcessOutput outputConfig = runtimeConfig.getProcessOutput();
84-
LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(
85-
"database system is ready to accept connections",
86-
new HashSet<>(asList("[initdb error]")), StreamToLineProcessor.wrap(outputConfig.getOutput()));
86+
final ProcessOutput outputConfig = runtimeConfig.getProcessOutput();
87+
final LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(
88+
"performing post-bootstrap initialization",
89+
singleton("[initdb error]"), StreamToLineProcessor.wrap(outputConfig.getOutput()));
8790
Processors.connect(process.getReader(), logWatch);
8891
Processors.connect(process.getError(), StreamToLineProcessor.wrap(outputConfig.getError()));
8992
logWatch.waitForResult(getConfig().timeout().startupTimeout());

src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ private static String runCmd(boolean silent,
118118
.prepare(postgresConfig);
119119
AbstractPGProcess proc = exec.start();
120120
logWatch.waitForResult(DEFAULT_CMD_TIMEOUT);
121+
if (!logWatch.isInitWithSuccess() && !silent) {
122+
LOGGER.warn("Possibly failed to run {}:\n{}", cmd.commandName(), logWatch.getOutput());
123+
}
121124
proc.waitFor();
122125
return logWatch.getOutput();
123126
} catch (IOException | InterruptedException e) {
@@ -190,8 +193,7 @@ protected void onBeforeProcess(IRuntimeConfig runtimeConfig)
190193
return;
191194
}
192195

193-
runCmd(config, runtimeConfig, InitDb,
194-
"Success. You can now start the database server using", emptySet());
196+
runCmd(config, runtimeConfig, InitDb, "Success", singleton("[initdb error]"));
195197
}
196198

197199
@Override

src/main/java/ru/yandex/qatools/embed/postgresql/ext/LogWatchStreamProcessor.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.Set;
88

9-
import static org.apache.commons.lang3.StringUtils.isEmpty;
9+
import static java.util.Optional.ofNullable;
1010

1111
/**
1212
* @author Ilya Sadykov
@@ -18,18 +18,20 @@ public class LogWatchStreamProcessor extends de.flapdoodle.embed.process.io.LogW
1818
private final String success;
1919
private final Set<String> failures;
2020
private volatile boolean found = false;
21+
private volatile boolean initWithSuccess = false;
2122

2223
public LogWatchStreamProcessor(String success, Set<String> failures, IStreamProcessor destination) {
2324
super(success, failures, destination);
24-
this.success = success;
25+
this.success = ofNullable(success).orElse("");
2526
this.failures = failures;
2627
}
2728

2829
@Override
2930
public void process(String block) {
3031
LOGGER.debug(block);
3132
output.append(block).append("\n");
32-
if (containsSuccess(block) || containsFailure(block)) {
33+
initWithSuccess = containsSuccess(block);
34+
if (initWithSuccess || containsFailure(block)) {
3335
synchronized (mutex) {
3436
found = true;
3537
mutex.notifyAll();
@@ -52,21 +54,27 @@ private boolean containsFailure(String block) {
5254
return false;
5355
}
5456

57+
@Override
5558
public void waitForResult(long timeout) {
5659
synchronized (mutex) {
5760
try {
5861
if (!found) {
5962
mutex.wait(timeout);
6063
}
6164
} catch (InterruptedException e) {
65+
LOGGER.error("Failed to wait for the result: '{}' not found in: \n{}", success, output);
6266
e.printStackTrace();
6367
}
6468
}
6569
}
6670

71+
@Override
72+
public boolean isInitWithSuccess() {
73+
return initWithSuccess || getOutput().contains(success);
74+
}
75+
6776
@Override
6877
public String getOutput() {
69-
String res = output.toString();
70-
return isEmpty(res) ? null : res;
78+
return output.toString();
7179
}
7280
}

0 commit comments

Comments
 (0)