private static InspectContainerResponse.ContainerState buildState(boolean running, boolean paused, String startedAt, String finishedAt) { InspectContainerResponse.ContainerState state = Mockito.mock(InspectContainerResponse.ContainerState.class); when(state.getRunning()).thenReturn(running); when(state.getPaused()).thenReturn(paused); when(state.getStartedAt()).thenReturn(startedAt); when(state.getFinishedAt()).thenReturn(finishedAt); return state; } }
/** * Based on this status, is this container running, and has it been doing so for the specified amount of time? * * @param state the state provided by InspectContainer * @param minimumRunningDuration minimum duration to consider this as "solidly" running, or null * @param now the time to consider as the current time * @return true if we can conclude that the container is running, false otherwise */ public static boolean isContainerRunning(InspectContainerResponse.ContainerState state, Duration minimumRunningDuration, Instant now) { if (state.getRunning()) { if (minimumRunningDuration == null) { return true; } Instant startedAt = DateTimeFormatter.ISO_INSTANT.parse( state.getStartedAt(), Instant::from); if (startedAt.isBefore(now.minus(minimumRunningDuration))) { return true; } } return false; }
/** * return container last start epoch time * * @param id container id * @return long epoch time */ public long getContainerStartedAt(String id) { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(id).exec(); String startedAt = inspectInfo.getState().getStartedAt(); return startedAt != null ? DateTimeFormatter.ISO_INSTANT.parse(startedAt, Instant::from).toEpochMilli() : Instant.now().toEpochMilli(); }
/** * gets {@link Container} status * * @param containerId - id of {@link Container} * @return {@link MicroserviceStatus} */ public MicroserviceStatus getMicroserviceStatus(String containerId) { InspectContainerResponse inspectInfo = dockerClient.inspectContainerCmd(containerId).exec(); ContainerState containerState = inspectInfo.getState(); MicroserviceStatus result = new MicroserviceStatus(); if (containerState != null) { if (containerState.getStartedAt() != null) { result.setStartTime(getStartedTime(containerState.getStartedAt())); } if (containerState.getStatus() != null) { MicroserviceState microserviceState = MicroserviceState.fromText(containerState.getStatus()); result.setStatus(MicroserviceState.RESTARTING.equals(microserviceState) && RestartStuckChecker.isStuck(containerId) ? MicroserviceState.STUCK_IN_RESTART : microserviceState); } result.setContainerId(containerId); result.setUsage(containerId); } return result; }
/** * Based on this status, is this container running, and has it been doing so for the specified amount of time? * * @param state the state provided by InspectContainer * @param minimumRunningDuration minimum duration to consider this as "solidly" running, or null * @param now the time to consider as the current time * @return true if we can conclude that the container is running, false otherwise */ public static boolean isContainerRunning(InspectContainerResponse.ContainerState state, Duration minimumRunningDuration, Instant now) { if (state.getRunning()) { if (minimumRunningDuration == null) { return true; } Instant startedAt = DateTimeFormatter.ISO_INSTANT.parse( state.getStartedAt(), Instant::from); if (startedAt.isBefore(now.minus(minimumRunningDuration))) { return true; } } return false; }
/** * 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; }
/** * 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; }