Skip to content

Commit

Permalink
Intercept and log listener exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and losipiuk committed Sep 17, 2020
1 parent d548911 commit 2b3a1c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.io.MoreFiles.deleteRecursively;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

Expand Down Expand Up @@ -192,7 +191,13 @@ private void copyFileFromContainer(String filename, Path rootHostPath)

log.info("Copying file %s to %s", filename, targetPath);
ensurePathExists(targetPath.getParent());
copyFileFromContainer(filename, targetPath.toString());

try {
copyFileFromContainer(filename, targetPath.toString());
}
catch (Exception e) {
log.warn("Could not copy file from %s to %s", filename, targetPath);
}
}

private Stream<String> listFilesInContainer(String path)
Expand All @@ -206,7 +211,7 @@ private Stream<String> listFilesInContainer(String path)
.splitToStream(result.getStdout());
}

throw new RuntimeException(format("Could not list files in %s: %s", path, result.getStderr()));
log.warn("Could not list files in %s: %s", path, result.getStderr());
}
catch (Exception e) {
log.warn("Could not list files in container '%s': %s", logicalName, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;

import static com.google.common.base.Throwables.getStackTraceAsString;
import static java.util.Objects.requireNonNull;

public interface EnvironmentListener
Expand Down Expand Up @@ -61,56 +63,68 @@ default void containerStopped(DockerContainer container, InspectContainerRespons
{
}

static void tryInvokeListener(Consumer<EnvironmentListener> call, EnvironmentListener... listeners)
{
Arrays.stream(listeners).forEach(listener -> {
try {
call.accept(listener);
}
catch (Exception e) {
log.error("Could not invoke listener %s due to %s", listener.getClass().getSimpleName(), getStackTraceAsString(e));
}
});
}

static EnvironmentListener compose(EnvironmentListener... listeners)
{
return new EnvironmentListener()
{
@Override
public void environmentStarting(Environment environment)
{
Arrays.stream(listeners).forEach(listener -> listener.environmentStarting(environment));
tryInvokeListener(listener -> listener.environmentStarting(environment), listeners);
}

@Override
public void environmentStarted(Environment environment)
{
Arrays.stream(listeners).forEach(listener -> listener.environmentStarted(environment));
tryInvokeListener(listener -> listener.environmentStarted(environment), listeners);
}

@Override
public void environmentStopping(Environment environment)
{
Arrays.stream(listeners).forEach(listener -> listener.environmentStopping(environment));
tryInvokeListener(listener -> listener.environmentStopping(environment), listeners);
}

@Override
public void environmentStopped(Environment environment)
{
Arrays.stream(listeners).forEach(listener -> listener.environmentStopped(environment));
tryInvokeListener(listener -> listener.environmentStopped(environment), listeners);
}

@Override
public void containerStarting(DockerContainer container, InspectContainerResponse response)
{
Arrays.stream(listeners).forEach(listener -> listener.containerStarting(container, response));
tryInvokeListener(listener -> listener.containerStarting(container, response), listeners);
}

@Override
public void containerStarted(DockerContainer container, InspectContainerResponse response)
{
Arrays.stream(listeners).forEach(listener -> listener.containerStarted(container, response));
tryInvokeListener(listener -> listener.containerStarted(container, response), listeners);
}

@Override
public void containerStopping(DockerContainer container, InspectContainerResponse response)
{
Arrays.stream(listeners).forEach(listener -> listener.containerStopping(container, response));
tryInvokeListener(listener -> listener.containerStopping(container, response), listeners);
}

@Override
public void containerStopped(DockerContainer container, InspectContainerResponse response)
{
Arrays.stream(listeners).forEach(listener -> listener.containerStopped(container, response));
tryInvokeListener(listener -> listener.containerStopped(container, response), listeners);
}
};
}
Expand Down

0 comments on commit 2b3a1c3

Please sign in to comment.