Skip to content

Commit

Permalink
Amend container shutdown to catch and log for all Exception cla… (#1663)
Browse files Browse the repository at this point in the history
* Amend container shutdown to catch and log for all Exception classes
Replacement for #1420 to fix incomplete error handling
Refs #1379

* Log root cause exception message rather than immediate exception's message
  • Loading branch information
rnorth committed Aug 10, 2019
1 parent 28b2032 commit 4d1ba9e
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions core/src/main/java/org/testcontainers/utility/ResourceReaper.java
Expand Up @@ -2,9 +2,6 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.ExposedPort;
Expand All @@ -13,6 +10,7 @@
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.api.model.Volume;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
Expand Down Expand Up @@ -218,12 +216,14 @@ private void stopContainer(String containerId, String imageName) {
boolean running;
try {
InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
running = containerInfo.getState().getRunning();
running = containerInfo.getState() != null && Boolean.TRUE.equals(containerInfo.getState().getRunning());
} catch (NotFoundException e) {
LOGGER.trace("Was going to stop container but it apparently no longer exists: {}", containerId);
return;
} catch (DockerException e) {
LOGGER.trace("Error encountered when checking container for shutdown (ID: {}) - it may not have been stopped, or may already be stopped: {}", containerId, e.getMessage());
} catch (Exception e) {
LOGGER.trace("Error encountered when checking container for shutdown (ID: {}) - it may not have been stopped, or may already be stopped. Root cause: {}",
containerId,
Throwables.getRootCause(e).getMessage());
return;
}

Expand All @@ -232,34 +232,28 @@ private void stopContainer(String containerId, String imageName) {
LOGGER.trace("Stopping container: {}", containerId);
dockerClient.killContainerCmd(containerId).exec();
LOGGER.trace("Stopped container: {}", imageName);
} catch (ConflictException e) {
if ( e.getMessage().startsWith("Cannot kill container: ") && e.getMessage().endsWith(containerId + " is not running")) {
LOGGER.trace("Was going to kill the container but it apparently already dead : {}", e.getMessage());
} else {
throw e;
}
} catch (DockerException e) {
LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped: {}", containerId, e.getMessage());
} catch (Exception e) {
LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped. Root cause: {}",
containerId,
Throwables.getRootCause(e).getMessage());
}
}

try {
dockerClient.inspectContainerCmd(containerId).exec();
} catch (NotFoundException e) {
} catch (Exception e) {
LOGGER.trace("Was going to remove container but it apparently no longer exists: {}", containerId);
return;
}

try {
LOGGER.trace("Removing container: {}", containerId);
try {
dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).withForce(true).exec();
LOGGER.debug("Removed container and associated volume(s): {}", imageName);
} catch (InternalServerErrorException e) {
LOGGER.trace("Exception when removing container with associated volume(s): {} (due to {})", imageName, e.getMessage());
}
} catch (DockerException e) {
LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped: {}", containerId, e.getMessage());
dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).withForce(true).exec();
LOGGER.debug("Removed container and associated volume(s): {}", imageName);
} catch (Exception e) {
LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped. Root cause: {}",
containerId,
Throwables.getRootCause(e).getMessage());
}
}

Expand Down

0 comments on commit 4d1ba9e

Please sign in to comment.