From 948b6292fe143584b8b57c894e2ac2889ac666b1 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 20 Jun 2019 13:29:15 +0100 Subject: [PATCH] add shutdown hook to shutdown core on SIGTERM --- core/src/main/groovy/com/muwire/core/Core.groovy | 7 +++++++ gui/griffon-app/lifecycle/Ready.groovy | 3 +++ 2 files changed, 10 insertions(+) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 1a5ff091..900b6a83 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -1,6 +1,7 @@ package com.muwire.core import java.nio.charset.StandardCharsets +import java.util.concurrent.atomic.AtomicBoolean import com.muwire.core.connection.ConnectionAcceptor import com.muwire.core.connection.ConnectionEstablisher @@ -73,6 +74,8 @@ public class Core { private final DownloadManager downloadManager private final DirectoryWatcher directoryWatcher final FileManager fileManager + + private final AtomicBoolean shutdown = new AtomicBoolean() public Core(MuWireSettings props, File home, String myVersion) { this.home = home @@ -241,6 +244,10 @@ public class Core { } public void shutdown() { + if (!shutdown.compareAndSet(false, true)) { + log.info("already shutting down") + return + } log.info("shutting down download manageer") downloadManager.shutdown() log.info("shutting down connection acceeptor") diff --git a/gui/griffon-app/lifecycle/Ready.groovy b/gui/griffon-app/lifecycle/Ready.groovy index 61bdd49b..5386a428 100644 --- a/gui/griffon-app/lifecycle/Ready.groovy +++ b/gui/griffon-app/lifecycle/Ready.groovy @@ -98,6 +98,9 @@ class Ready extends AbstractLifecycleHandler { "Can't connect to I2P router", JOptionPane.WARNING_MESSAGE) System.exit(0) } + Runtime.getRuntime().addShutdownHook({ + core.shutdown() + }) core.startServices() application.context.put("muwire-settings", props) application.context.put("core",core)