Java Code Examples for com.github.dockerjava.api.command.InspectContainerResponse#ContainerState

The following examples show how to use com.github.dockerjava.api.command.InspectContainerResponse#ContainerState . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerializableContainerState.java    From jbpm-work-items with Apache License 2.0 6 votes vote down vote up
public SerializableContainerState(InspectContainerResponse.ContainerState containerState) {
    if(containerState != null) {
        this.status = containerState.getStatus();
        this.startedAt = containerState.getStartedAt();
        this.running = containerState.getRunning();
        this.paused = containerState.getPaused();
        this.restarting = containerState.getRestarting();
        this.oomKilled = containerState.getOOMKilled();
        this.dead = containerState.getDead();
        this.pid = containerState.getPid();
        this.exitCode = containerState.getExitCode();
        this.error = containerState.getError();
        this.startedAt = containerState.getStartedAt();
        this.finishedAt = containerState.getFinishedAt();
    }
}
 
Example 2
Source File: DockerManager.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
/**
 * Wait container to exit or timeout
 *
 * @return true if not running, false for timeout
 */
public boolean waitContainer(String cid, int timeout) {
    Instant expire = Instant.now().plus(timeout, ChronoUnit.SECONDS);

    for (; ; ) {
        InspectContainerResponse inspect = client.inspectContainerCmd(cid).exec();
        InspectContainerResponse.ContainerState state = inspect.getState();

        if (state.getRunning() == null) {
            return false;
        }

        if (!state.getRunning()) {
            return true;
        }

        if (Instant.now().isAfter(expire)) {
            return false;
        }

        ThreadHelper.sleep(1000);
    }
}
 
Example 3
Source File: DockerStatus.java    From testcontainers-java with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: DockerStatus.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: OneShotStartupCheckStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@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;
    }
}
 
Example 6
Source File: IsRunningStartupCheckStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@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;
    }
}
 
Example 7
Source File: MinimumDurationRunningStartupCheckStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@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;
}
 
Example 8
Source File: DockerStatusTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
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;
}
 
Example 9
Source File: DockerStatus.java    From testcontainers-java with MIT License 4 votes vote down vote up
public static boolean isContainerExitCodeSuccess(InspectContainerResponse.ContainerState state) {
    int exitCode = state.getExitCode();
    // 0 is the only exit code we can consider as success
    return exitCode == 0;
}
 
Example 10
Source File: StartupCheckStrategy.java    From testcontainers-java with MIT License 4 votes vote down vote up
protected InspectContainerResponse.ContainerState getCurrentState(DockerClient dockerClient, String containerId) {
    return dockerClient.inspectContainerCmd(containerId).exec().getState();
}