@Override public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { // record "now" before fetching status; otherwise the time to fetch the status // will contribute to how long the container has been running. Instant now = Instant.now(); InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); if (DockerStatus.isContainerRunning(state, minimumRunningDuration, now)) { return StartupStatus.SUCCESSFUL; } else if (DockerStatus.isContainerStopped(state)) { return StartupStatus.FAILED; } return StartupStatus.NOT_YET_KNOWN; }
@Override public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); if (!DockerStatus.isContainerStopped(state)) { return StartupStatus.NOT_YET_KNOWN; } if (DockerStatus.isContainerStopped(state) && DockerStatus.isContainerExitCodeSuccess(state)) { return StartupStatus.SUCCESSFUL; } else { return StartupStatus.FAILED; } } }
/** * Based on this status, has the container halted? * * @param state the state provided by InspectContainer * @return true if we can conclude that the container has started but is now stopped, false otherwise. */ public static boolean isContainerStopped(InspectContainerResponse.ContainerState state) { // get some preconditions out of the way if (state.getRunning() || state.getPaused()) { return false; } // if the finished timestamp is non-empty, that means the container started and finished. boolean hasStarted = isDockerTimestampNonEmpty(state.getStartedAt()); boolean hasFinished = isDockerTimestampNonEmpty(state.getFinishedAt()); return hasStarted && hasFinished; }
@Test public void testRunning() throws Exception { assertTrue(DockerStatus.isContainerRunning(running, minimumDuration, now)); assertTrue(DockerStatus.isContainerRunning(runningVariant, minimumDuration, now)); assertFalse(DockerStatus.isContainerRunning(shortRunning, minimumDuration, now)); assertFalse(DockerStatus.isContainerRunning(created, minimumDuration, now)); assertFalse(DockerStatus.isContainerRunning(createdVariant, minimumDuration, now)); assertFalse(DockerStatus.isContainerRunning(exited, minimumDuration, now)); assertFalse(DockerStatus.isContainerRunning(paused, minimumDuration, now)); }
@Test public void testStopped() throws Exception { assertFalse(DockerStatus.isContainerStopped(running)); assertFalse(DockerStatus.isContainerStopped(runningVariant)); assertFalse(DockerStatus.isContainerStopped(shortRunning)); assertFalse(DockerStatus.isContainerStopped(created)); assertFalse(DockerStatus.isContainerStopped(createdVariant)); assertTrue(DockerStatus.isContainerStopped(exited)); assertFalse(DockerStatus.isContainerStopped(paused)); }
@Override public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); if (state.getRunning()) { return StartupStatus.SUCCESSFUL; } else if (!DockerStatus.isContainerExitCodeSuccess(state)) { return StartupStatus.FAILED; } else { return StartupStatus.NOT_YET_KNOWN; } } }
@Override public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); if (state.getRunning()) { return StartupStatus.SUCCESSFUL; } else if (!DockerStatus.isContainerExitCodeSuccess(state)) { return StartupStatus.FAILED; } else { return StartupStatus.NOT_YET_KNOWN; } } }
@Override public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { // record "now" before fetching status; otherwise the time to fetch the status // will contribute to how long the container has been running. Instant now = Instant.now(); InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); if (DockerStatus.isContainerRunning(state, minimumRunningDuration, now)) { return StartupStatus.SUCCESSFUL; } else if (DockerStatus.isContainerStopped(state)) { return StartupStatus.FAILED; } return StartupStatus.NOT_YET_KNOWN; }
@Override public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { InspectContainerResponse.ContainerState state = getCurrentState(dockerClient, containerId); if (!DockerStatus.isContainerStopped(state)) { return StartupStatus.NOT_YET_KNOWN; } if (DockerStatus.isContainerStopped(state) && DockerStatus.isContainerExitCodeSuccess(state)) { return StartupStatus.SUCCESSFUL; } else { return StartupStatus.FAILED; } } }
/** * Based on this status, has the container halted? * * @param state the state provided by InspectContainer * @return true if we can conclude that the container has started but is now stopped, false otherwise. */ public static boolean isContainerStopped(InspectContainerResponse.ContainerState state) { // get some preconditions out of the way if (state.getRunning() || state.getPaused()) { return false; } // if the finished timestamp is non-empty, that means the container started and finished. boolean hasStarted = isDockerTimestampNonEmpty(state.getStartedAt()); boolean hasFinished = isDockerTimestampNonEmpty(state.getFinishedAt()); return hasStarted && hasFinished; }