1
0
Fork 1
mirror of https://gitlab.com/mangadex-pub/mangadex_at_home.git synced 2024-01-19 02:48:37 +00:00

Third try at concurrency

This commit is contained in:
carbotaniuman 2020-08-11 14:42:00 -05:00
parent 89d9a9386f
commit c64ae4e339

View file

@ -28,7 +28,9 @@ import com.fasterxml.jackson.module.kotlin.readValue
import java.io.File
import java.io.FileReader
import java.io.IOException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
import mdnet.base.Main.dieWithError
@ -46,6 +48,7 @@ class ClientSettingsException(message: String) : Exception(message)
class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFolder: File) {
// this must remain single-threaded because of how the state mechanism works
private val executor = Executors.newSingleThreadScheduledExecutor()
private lateinit var scheduledFuture: ScheduledFuture<*>
private val database: Database
private val cache: DiskLruCache
@ -89,7 +92,7 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo
fun runLoop() {
LOGGER.info { "Mangadex@Home Client initialized - starting normal operation." }
executor.scheduleWithFixedDelay({
scheduledFuture = executor.scheduleWithFixedDelay({
try {
// this blocks the executor, so no worries about concurrency
reloadClientSettings()
@ -145,22 +148,31 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo
}
fun shutdown() {
LOGGER.info { "Mangadex@Home Client shutting down" }
val latch = CountDownLatch(1)
scheduledFuture.cancel(false)
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" }
}
latch.countDown()
}, 0, TimeUnit.SECONDS)
latch.await()
executor.shutdown()
LOGGER.info { "Mangadex@Home Client has shut down" }
}
/**