From 1a42044347502c59cd9c39d1be67f654df02908a Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 31 Jan 2022 07:51:35 -0800 Subject: [PATCH] Jenkins should terminate cleanly on `SIGTERM` (#6230) --- core/src/main/java/hudson/lifecycle/Lifecycle.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/src/main/java/hudson/lifecycle/Lifecycle.java b/core/src/main/java/hudson/lifecycle/Lifecycle.java index a46452420e..fce497456b 100644 --- a/core/src/main/java/hudson/lifecycle/Lifecycle.java +++ b/core/src/main/java/hudson/lifecycle/Lifecycle.java @@ -54,6 +54,19 @@ import org.apache.commons.io.FileUtils; public abstract class Lifecycle implements ExtensionPoint { private static Lifecycle INSTANCE = null; + public Lifecycle() { + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + Jenkins jenkins = Jenkins.getInstanceOrNull(); + if (jenkins != null) { + try { + jenkins.cleanUp(); + } catch (Throwable t) { + LOGGER.log(Level.SEVERE, "Failed to clean up. Shutdown will continue.", t); + } + } + })); + } + /** * Gets the singleton instance. *