From 2371a8a87050df3b79a9ff30a542bb6c4f000b4a Mon Sep 17 00:00:00 2001 From: RickeyWard Date: Tue, 25 Jan 2022 14:23:28 -0500 Subject: [PATCH] Added skip fail job on stop error flag Allow passing of jobs that fail to stop/rm in time. Some large builds can succeed and fail on stop due to timeouts (increasing the timeout is not always an adequate solution) more info JENKINS-42322 --- .../docker/workflow/client/DockerClient.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java index 336efa0bb..1aed9d252 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java @@ -83,6 +83,13 @@ public class DockerClient { @Restricted(NoExternalUse.class) public static boolean SKIP_RM_ON_STOP = Boolean.getBoolean(DockerClient.class.getName() + ".SKIP_RM_ON_STOP"); + /** + * Skip failing a job if docker-stop / rm fails / times out. + */ + @SuppressFBWarnings(value="MS_SHOULD_BE_FINAL", justification="mutable for scripts") + @Restricted(NoExternalUse.class) + public static boolean SKIP_FAILJOB_ON_STOP_ERROR = Boolean.getBoolean(DockerClient.class.getName() + ".SKIP_FAILJOB_ON_STOP_ERROR"); + // e.g. 2015-04-09T13:40:21.981801679Z public static final String DOCKER_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; @@ -181,7 +188,13 @@ public List listProcess(@Nonnull EnvVars launchEnv, @Nonnull String cont public void stop(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws IOException, InterruptedException { LaunchResult result = launch(launchEnv, false, "stop", "--time=1", containerId); if (result.getStatus() != 0) { - throw new IOException(String.format("Failed to kill container '%s'.", containerId)); + if(SKIP_FAILJOB_ON_STOP_ERROR) { + if (LOGGER.isLoggable(Level.WARNING)) { + LOGGER.log(Level.WARNING, "Failed to kill container {0}, SKIP_FAILJOB_ON_STOP_ERROR is enabled.", containerId); + } + } else { + throw new IOException(String.format("Failed to kill container '%s'.", containerId)); + } } if (!SKIP_RM_ON_STOP) { rm(launchEnv, containerId); @@ -198,7 +211,13 @@ public void rm(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws I LaunchResult result; result = launch(launchEnv, false, "rm", "-f", containerId); if (result.getStatus() != 0) { - throw new IOException(String.format("Failed to rm container '%s'.", containerId)); + if(SKIP_FAILJOB_ON_STOP_ERROR) { + if (LOGGER.isLoggable(Level.WARNING)) { + LOGGER.log(Level.WARNING, "Failed to rm container {0}, SKIP_FAILJOB_ON_STOP_ERROR is enabled.", containerId); + } + } else { + throw new IOException(String.format("Failed to rm container '%s'.", containerId)); + } } }