Search in sources :

Example 1 with StopMode

use of io.fabric8.maven.docker.config.StopMode in project docker-maven-plugin by fabric8io.

the class RunService method shutdown.

private void shutdown(ContainerTracker.ContainerShutdownDescriptor descriptor, boolean keepContainer, boolean removeVolumes) throws DockerAccessException, ExecException {
    String containerId = descriptor.getContainerId();
    StopMode stopMode = descriptor.getStopMode();
    if (descriptor.getPreStop() != null) {
        try {
            execInContainer(containerId, descriptor.getPreStop(), descriptor.getImageConfiguration());
        } catch (DockerAccessException e) {
            log.error("%s", e.getMessage());
        } catch (ExecException e) {
            if (descriptor.isBreakOnError()) {
                throw e;
            } else {
                log.warn("Cannot run preStop: %s", e.getMessage());
            }
        }
    }
    if (stopMode.equals(StopMode.graceful)) {
        int killGracePeriod = adjustGracePeriod(descriptor.getKillGracePeriod());
        log.debug("shutdown will wait max of %d seconds before removing container", killGracePeriod);
        long waited;
        if (killGracePeriod == 0) {
            docker.stopContainer(containerId, 0);
            waited = 0;
        } else {
            waited = shutdownAndWait(containerId, killGracePeriod);
        }
        log.info("%s: Stop%s container %s after %s ms", descriptor.getDescription(), (keepContainer ? "" : " and removed"), containerId.substring(0, 12), waited);
    } else if (stopMode.equals(StopMode.kill)) {
        docker.killContainer(containerId);
        log.info("%s: Killed%s container %s.", descriptor.getDescription(), (keepContainer ? "" : " and removed"), containerId.subSequence(0, 12));
    }
    if (!keepContainer) {
        removeContainer(descriptor, removeVolumes, containerId);
    }
}
Also used : DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ExecException(io.fabric8.maven.docker.access.ExecException) StopMode(io.fabric8.maven.docker.config.StopMode)

Aggregations

DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)1 ExecException (io.fabric8.maven.docker.access.ExecException)1 StopMode (io.fabric8.maven.docker.config.StopMode)1