diff --git a/src/main/kotlin/mdnet/base/MangaDexClient.kt b/src/main/kotlin/mdnet/base/MangaDexClient.kt index 0e2e0e9..704c700 100644 --- a/src/main/kotlin/mdnet/base/MangaDexClient.kt +++ b/src/main/kotlin/mdnet/base/MangaDexClient.kt @@ -45,16 +45,17 @@ import java.util.concurrent.atomic.AtomicBoolean class ClientSettingsException(message: String) : Exception(message) class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFolder: File) { - // just for scheduling one task, so single-threaded + // this must remain single-threaded because of how the state mechanism works private val executor = Executors.newSingleThreadScheduledExecutor() - private val isReloading = AtomicBoolean(false) private val database: Database private val cache: DiskLruCache private var settings: ClientSettings + // state that must only be accessed from the thread on the executor private var imageServer: ServerManager? = null private var webUi: Http4kServer? = null + // end protected state init { settings = try { @@ -145,20 +146,22 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo } fun shutdown() { - LOGGER.info { "Mangadex@Home Client shutting down" } - if (webUi != null) { - stopWebUi() - } - if (imageServer != null) { - stopImageServer() - } - LOGGER.info { "Mangadex@Home Client has shut down" } + executor.schedule({ + LOGGER.info { "Mangadex@Home Client shutting down" } + if (webUi != null) { + stopWebUi() + } + if (imageServer != null) { + stopImageServer() + } + LOGGER.info { "Mangadex@Home Client has shut down" } - try { - cache.close() - } catch (e: IOException) { - LOGGER.error(e) { "Cache failed to close" } - } + try { + cache.close() + } catch (e: IOException) { + LOGGER.error(e) { "Cache failed to close" } + } + }, 0, TimeUnit.SECONDS) } /** diff --git a/src/main/kotlin/mdnet/base/ServerManager.kt b/src/main/kotlin/mdnet/base/ServerManager.kt index 9b6a789..6a08d49 100644 --- a/src/main/kotlin/mdnet/base/ServerManager.kt +++ b/src/main/kotlin/mdnet/base/ServerManager.kt @@ -37,7 +37,7 @@ class ServerManager(serverSettings: ServerSettings, devSettings: DevSettings, ma // this must remain single-threaded because of how the state mechanism works private val executor = Executors.newSingleThreadScheduledExecutor() - // state that must only be accessed from the thread on the executorService + // state that must only be accessed from the thread on the executor private var state: State private var serverHandler: ServerHandler // end protected state