From 657d6d48511706d57c644d947116d127fb57e0e5 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Thu, 4 Dec 2014 20:29:14 +0100 Subject: [PATCH] #190: Graceful deleting a pod --- .../io/fabric8/jube/local/NodeHelper.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/node/src/main/java/io/fabric8/jube/local/NodeHelper.java b/node/src/main/java/io/fabric8/jube/local/NodeHelper.java index efd8019..9a4952e 100644 --- a/node/src/main/java/io/fabric8/jube/local/NodeHelper.java +++ b/node/src/main/java/io/fabric8/jube/local/NodeHelper.java @@ -66,6 +66,8 @@ public final class NodeHelper { public static final String KIND_REPLICATION_CONTROLLER = "ReplicationController"; public static final String KIND_SERVICE = "SERVICE"; + private static final int TIMEOUT = 30; + private static final transient Logger LOG = LoggerFactory.getLogger(NodeHelper.class); private NodeHelper() { @@ -385,16 +387,44 @@ protected static void deleteContainer(ProcessManager processManager, KubernetesM return; } ProcessController controller = installation.getController(); + // try graceful to stop first, then kill afterwards + // as the controller may issue a command that stops asynchronously, we need to check if the pid is alive + // until its graceful shutdown, before we go harder and try to kill it try { controller.stop(); } catch (Exception e) { LOG.warn("Error during stopping container. Will now attempt to forcibly kill the container.", e); } - try { - controller.kill(); - } catch (Exception e) { - LOG.warn("Error during killing container. Will now attempt to uninstall the container.", e); + + // TODO: more logging, and maybe configurable timeout + + boolean kill = true; + for (int i = 0; i < TIMEOUT; i++) { + Long pid; + try { + pid = installation.getActivePid(); + } catch (IOException e) { + // ignore, but force a pid value so we run for the timeout duration + pid = 1L; + } + final boolean alive = pid != null && pid.longValue() > 0; + + if (!alive) { + kill = false; + break; + } else { + // wait 1 sec + Thread.sleep(1000); + } + } + + if (kill) { + try { + controller.kill(); + } catch (Exception e) { + LOG.warn("Error during killing container. Will now attempt to uninstall the container.", e); + } } try { controller.uninstall();