Skip to content

Commit

Permalink
Gracefully wait containers to stop
Browse files Browse the repository at this point in the history
Allows waiting for missing containers without failing.
  • Loading branch information
gesellix committed Feb 1, 2022
1 parent 633a551 commit 31eb506
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.gesellix.gradle.docker.tasks;

import de.gesellix.docker.engine.EngineResponse;
import de.gesellix.docker.remote.api.ContainerWaitResponse;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
Expand All @@ -18,10 +20,18 @@ public Property<String> getContainerId() {
return containerId;
}

private EngineResponse result;
private final Property<Boolean> ignoreError;

@Input
@Optional
public Property<Boolean> getIgnoreError() {
return ignoreError;
}

private EngineResponse<ContainerWaitResponse> result;

@Internal
public EngineResponse getResult() {
public EngineResponse<ContainerWaitResponse> getResult() {
return result;
}

Expand All @@ -31,12 +41,30 @@ public DockerWaitTask(ObjectFactory objectFactory) {
setDescription("Block until a container stops, then print its exit code.");

containerId = objectFactory.property(String.class);
ignoreError = objectFactory.property(Boolean.class);
ignoreError.convention(false);
}

@TaskAction
public EngineResponse awaitStop() {
public EngineResponse<ContainerWaitResponse> awaitStop() {
getLogger().info("docker wait");
result = getDockerClient().wait(getContainerId().get());

try {
result = getDockerClient().wait(getContainerId().get());
}
catch (Exception e) {
if (!ignoreError.get()) {
throw new RuntimeException(e);
}
else {
if (getLogger().isInfoEnabled()) {
getLogger().warn("docker container wait " + getContainerId().get() + " failed", e);
}
else {
getLogger().warn("docker container wait " + getContainerId().get() + " failed");
}
}
}
return result;
}
}

0 comments on commit 31eb506

Please sign in to comment.