Skip to content

Commit c5b5b1e

Browse files
fatmcgavGavin Williams
authored andcommitted
Add TERM handling
This is useful to gracefully stop the Jenkins swarm process, for example when running in Kubernetes in conjunction with a preStop lifecycle hook. Signed-off-by: Gavin Williams <gavin.williams@elastic.co>
1 parent 86fb969 commit c5b5b1e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

client/src/main/java/hudson/plugins/swarm/Client.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.file.Path;
2525
import java.nio.file.Paths;
2626
import java.util.List;
27+
import java.util.Set;
2728
import java.util.logging.Level;
2829
import java.util.logging.Logger;
2930

@@ -187,6 +188,34 @@ private static void validateOptions(Options options) {
187188
*/
188189
static void run(SwarmClient swarmClient, Options options, String... args)
189190
throws InterruptedException {
191+
192+
// Gracefully handle TERM request
193+
Runtime.getRuntime().addShutdownHook(new Thread() {
194+
@Override
195+
public void run() {
196+
logger.info("Interrupting threads");
197+
Set<Thread> runningThreads = Thread.getAllStackTraces().keySet();
198+
for (Thread th : runningThreads) {
199+
if (th != Thread.currentThread() && !th.isDaemon()
200+
&& th.getClass().getName().startsWith("hudson.plugins.swarm")) {
201+
logger.info("Interrupting '" + th.getClass() + "' termination");
202+
th.interrupt();
203+
}
204+
}
205+
for (Thread th : runningThreads) {
206+
try {
207+
if (th != Thread.currentThread() && !th.isDaemon() && th.isInterrupted()) {
208+
logger.info("Waiting '" + th.getName() + "' termination");
209+
th.join();
210+
}
211+
} catch (InterruptedException ex) {
212+
logger.info("Shutdown interrupted");
213+
}
214+
}
215+
logger.info("Shutdown finished");
216+
}
217+
});
218+
190219
logger.info("Connecting to Jenkins controller");
191220
URL url = swarmClient.getUrl();
192221

0 commit comments

Comments
 (0)