From 535d21ec8e85f1e6f98ca25a855ce2177b83a112 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Tue, 11 Aug 2020 14:31:47 -0500 Subject: [PATCH] Fix associated bugs --- src/main/kotlin/mdnet/base/MangaDexClient.kt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/mdnet/base/MangaDexClient.kt b/src/main/kotlin/mdnet/base/MangaDexClient.kt index 27aa096..0e2e0e9 100644 --- a/src/main/kotlin/mdnet/base/MangaDexClient.kt +++ b/src/main/kotlin/mdnet/base/MangaDexClient.kt @@ -28,7 +28,6 @@ import com.fasterxml.jackson.module.kotlin.readValue import java.io.File import java.io.FileReader import java.io.IOException -import java.lang.AssertionError import java.util.concurrent.Executors import java.util.concurrent.TimeUnit import java.util.regex.Pattern @@ -40,6 +39,7 @@ import mdnet.cache.HeaderMismatchException import org.http4k.server.Http4kServer import org.jetbrains.exposed.sql.Database import org.slf4j.LoggerFactory +import java.util.concurrent.atomic.AtomicBoolean // Exception class to handle when Client Settings have invalid values class ClientSettingsException(message: String) : Exception(message) @@ -47,6 +47,8 @@ class ClientSettingsException(message: String) : Exception(message) class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFolder: File) { // just for scheduling one task, so single-threaded private val executor = Executors.newSingleThreadScheduledExecutor() + private val isReloading = AtomicBoolean(false) + private val database: Database private val cache: DiskLruCache private var settings: ClientSettings @@ -89,6 +91,7 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo executor.scheduleWithFixedDelay({ try { + // this blocks the executor, so no worries about concurrency reloadClientSettings() } catch (e: Exception) { LOGGER.warn(e) { "Reload of ClientSettings failed" } @@ -104,7 +107,9 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo settings.webSettings?.let { webSettings -> val imageServer = requireNotNull(imageServer) - if (webUi != null) throw AssertionError() + if (webUi != null) { + throw AssertionError() + } LOGGER.info { "WebUI starting" } webUi = getUiServer(webSettings, imageServer.statistics, imageServer.statsMap).also { it.start() @@ -115,7 +120,9 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo // Precondition: settings must be filled with up-to-date settings private fun startImageServer() { - if (imageServer != null) throw AssertionError() + if (imageServer != null) { + throw AssertionError() + } LOGGER.info { "Server manager starting" } imageServer = ServerManager(settings.serverSettings, settings.devSettings, settings.maxCacheSizeInMebibytes, cache, database).also { it.start() @@ -179,7 +186,9 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo if (stopWebUi) { LOGGER.info { "Stopping WebUI to reload ClientSettings" } - stopWebUi() + if(webUi != null) { + stopWebUi() + } } if (restartServer) {