Skip to content

Commit

Permalink
1.2.6 Better failure error reporting. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Oct 26, 2023
1 parent f3c29d3 commit 3d66be2
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 62 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/build-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,10 @@ jobs:
with:
java-version: '8'
distribution: 'adopt'
- name: Setup GO
uses: actions/setup-go@v3
with:
go-version: '1.19.9'
- name: Install Nats Server
run: |
cd $GITHUB_WORKSPACE
git clone https://github.com/nats-io/nats-server.git
cd nats-server
go get
go build main.go
mkdir -p ~/.local/bin
cp main ~/.local/bin/nats-server
cd ..
rm -rf nats-server
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@main | PREFIX=. sh
sudo mv nats-server /usr/local/bin
nats-server -v
- name: Check out code
uses: actions/checkout@v3
Expand Down
15 changes: 2 additions & 13 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,10 @@ jobs:
with:
java-version: '8'
distribution: 'adopt'
- name: Setup GO
uses: actions/setup-go@v3
with:
go-version: '1.19.9'
- name: Install Nats Server
run: |
cd $GITHUB_WORKSPACE
git clone https://github.com/nats-io/nats-server.git
cd nats-server
go get
go build main.go
mkdir -p ~/.local/bin
cp main ~/.local/bin/nats-server
cd ..
rm -rf nats-server
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@main | PREFIX=. sh
sudo mv nats-server /usr/local/bin
nats-server -v
- name: Check out code
uses: actions/checkout@v3
Expand Down
15 changes: 2 additions & 13 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,10 @@ jobs:
with:
java-version: '8'
distribution: 'adopt'
- name: Setup GO
uses: actions/setup-go@v3
with:
go-version: '1.19.9'
- name: Install Nats Server
run: |
cd $GITHUB_WORKSPACE
git clone https://github.com/nats-io/nats-server.git
cd nats-server
go get
go build main.go
mkdir -p ~/.local/bin
cp main ~/.local/bin/nats-server
cd ..
rm -rf nats-server
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@main | PREFIX=. sh
sudo mv nats-server /usr/local/bin
nats-server -v
- name: Check out code
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
id 'signing'
}

def jarVersion = "1.2.5"
def jarVersion = "1.2.6"
group = 'io.nats'

def isMerge = System.getenv("BUILD_EVENT") == "push"
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/nats/io/ConsoleOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void info(Supplier<String> msgSupplier) {

@Override
public void info(String msg) {
if (shouldShow(INFO)) {
if (shouldShow(Level.INFO)) {
System.out.println(format(INFO, msg));
}
}
Expand All @@ -83,7 +83,7 @@ public Logger getLogger() {
return null;
}

private boolean shouldShow(Level testLevel) {
return level.intValue() <= testLevel.intValue();
protected boolean shouldShow(Level testLevel) {
return level.intValue() <= testLevel.intValue() && testLevel != OFF;
}
}
27 changes: 23 additions & 4 deletions src/main/java/nats/io/NatsOutputLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
Expand All @@ -35,21 +37,36 @@
final class NatsOutputLogger implements Runnable {
private final Output output;
private final BufferedReader reader;
private final List<String> startupLines;
private boolean inStartupPhase;

private NatsOutputLogger(Output output, Process process) {
this.output = output;
this.reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
startupLines = new ArrayList<>();
inStartupPhase = true;
}

private void logLine(String line) {
public void endStartupPhase() {
inStartupPhase = false;
}

public List<String> getStartupLines() {
return startupLines;
}

public void logInfo(String line) {
output.info(() -> line);
if (inStartupPhase) {
startupLines.add(line);
}
}

@Override
public void run() {
try {
try {
reader.lines().forEach(this::logLine);
reader.lines().forEach(this::logInfo);
} catch (final UncheckedIOException e) {
output.warning(() -> "while reading output " + e);
}
Expand All @@ -62,12 +79,14 @@ public void run() {
}
}

static void logOutput(final Output output, final Process process, final String processName) {
static NatsOutputLogger logOutput(final Output output, final Process process, final String processName) {
final String threadName = (isBlank(processName) ? "unknown" : processName) + ":" + processId(process);
final Thread t = new Thread(new NatsOutputLogger(output, process));
NatsOutputLogger nol = new NatsOutputLogger(output, process);
final Thread t = new Thread(nol);
t.setName(threadName);
t.setDaemon(true);
t.start();
return nol;
}

private static String processId(Process process) {
Expand Down
71 changes: 62 additions & 9 deletions src/main/java/nats/io/NatsServerRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -35,6 +36,8 @@
* Server Runner
*/
public class NatsServerRunner implements AutoCloseable {
public static final String ERROR_NOTE_PART_1 = "Make sure that the nats-server is installed and in your PATH.";
public static final String ERROR_NOTE_PART_2 = "See https://github.com/nats-io/nats-server for information on installation";
public static long DEFAULT_PROCESS_CHECK_WAIT = 100;
public static int DEFAULT_PROCESS_CHECK_TRIES = 10;
public static long DEFAULT_RUN_CHECK_WAIT = 100;
Expand Down Expand Up @@ -310,8 +313,16 @@ protected NatsServerRunner(Builder b) throws IOException {
_executablePath = b.executablePath == null ? getResolvedServerPath() : b.executablePath.toString();
_port = b.port == null || b.port <= 0 ? nextPort() : b.port;

_displayOut = b.output == null ? DefaultOutputSupplier.get() : b.output;
_displayOut.setLevel(b.outputLevel == null ? DefaultOutputLevel : b.outputLevel);
if (b.output == null) {
_displayOut = DefaultOutputSupplier.get();
_displayOut.setLevel(DefaultOutputLevel);
}
else {
_displayOut = b.output;
if (b.outputLevel != null) {
_displayOut.setLevel(b.outputLevel);
}
}

long procCheckWait = b.processCheckWait == null ? DEFAULT_PROCESS_CHECK_WAIT : b.processCheckWait;
int procCheckTries = b.processCheckTries == null ? DEFAULT_PROCESS_CHECK_TRIES : b.processCheckTries;
Expand Down Expand Up @@ -361,6 +372,7 @@ protected NatsServerRunner(Builder b) throws IOException {

_cmdLine = String.join(" ", cmd);

NatsOutputLogger nol = null;
try {
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Expand All @@ -369,8 +381,7 @@ protected NatsServerRunner(Builder b) throws IOException {
_displayOut.info("%%% Starting [" + _cmdLine + "] with redirected IO");

process = pb.start();

NatsOutputLogger.logOutput(_displayOut, process, DEFAULT_NATS_SERVER);
nol = NatsOutputLogger.logOutput(_displayOut, process, DEFAULT_NATS_SERVER);

int tries = procCheckTries;
do {
Expand Down Expand Up @@ -401,15 +412,51 @@ protected NatsServerRunner(Builder b) throws IOException {
}

_displayOut.info("%%% Started [" + _cmdLine + "]");
nol.endStartupPhase();
}
catch (IOException ex) {
_displayOut.info("%%% Failed to start [" + _cmdLine + "] with message:");
_displayOut.info("\t" + ex.getMessage());
_displayOut.info("%%% Make sure that the nats-server is installed and in your PATH.");
_displayOut.info("%%% See https://github.com/nats-io/nats-server for information on installation");
_displayOut.error("%%% Failed to run [" + _cmdLine + "]");
String exMsg = ex.getMessage();
if (exMsg != null) {
_displayOut.error(" " + ex.getMessage());
}
_displayOut.error("%%% " + ERROR_NOTE_PART_1);
_displayOut.error("%%% " + ERROR_NOTE_PART_2);
StringBuilder exMessage = new StringBuilder("Failed to run [").append(_cmdLine).append("]");
if (b.fullErrorReportOnStartup) {
if (nol != null) {
for (String line : nol.getStartupLines()) {
exMessage.append(System.lineSeparator()).append(line);
}
}
if (_cmdLine.contains(CONFIG_FILE_OPTION_NAME)) {
String configPath = _configFile.getAbsolutePath();
String configSep = getConfigSep(configPath);
exMessage.append(System.lineSeparator()).append(configSep);
exMessage.append(System.lineSeparator()).append(configPath);
exMessage.append(System.lineSeparator()).append(configSep);
try {
List<String> lines = Files.readAllLines(_configFile.toPath());
for (String line : lines) {
exMessage.append(System.lineSeparator()).append(line);
}
}
catch (Exception ignore) {
}
exMessage.append(System.lineSeparator()).append(configSep);
}
}
throw new IllegalStateException(exMessage.toString());
}
}

throw new IllegalStateException("Failed to run [" + _cmdLine + "]");
private String getConfigSep(String configPath) {
StringBuilder sep = new StringBuilder("------------------------------");
int len = configPath.length();
while (sep.length() < len) {
sep.append(sep);
}
return sep.substring(0, len);
}

// ----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -552,6 +599,7 @@ public static class Builder {
Integer processCheckTries;
Long connectCheckWait;
Integer connectCheckTries;
boolean fullErrorReportOnStartup = true;

public Builder port(Integer port) {
this.port = port;
Expand Down Expand Up @@ -653,6 +701,11 @@ public Builder connectCheckTries(Integer connectCheckTries) {
return this;
}

public Builder fullErrorReportOnStartup(boolean expandedError) {
this.fullErrorReportOnStartup = expandedError;
return this;
}

public Builder runnerOptions(NatsServerRunnerOptions nsro) {
port(nsro.port())
.debugLevel(nsro.debugLevel())
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/nats/io/NatsServerRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,18 @@ public void testShutdownCoverage() throws Exception {
Thread.sleep(1000);
runner.shutdown();
}

@Test
public void testBadConfig() throws Exception {
try (NatsServerRunner runner = NatsServerRunner.builder()
.configFilePath(SOURCE_CONFIG_FILE_PATH + "bad.conf")
.output(new ConsoleOutput())
.build())
{
}
catch (Exception e) {
assertTrue(e.getMessage().contains("nats-server: Parse error on line 2"));
System.out.println(e);
}
}
}
Loading

0 comments on commit 3d66be2

Please sign in to comment.