mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-14 08:14:34 -05:00
Feature/listen to server config value changes (#617)
* Make server config value changes subscribable * Make server config value changes subscribable - Update usage * Add util functions to listen to server config value changes * Listen to server config value changes - Auto backups * Listen to server config value changes - Auto global update * Listen to server config value changes - WebUI auto updates * Listen to server config value changes - Javalin update ip and port * Listen to server config value changes - Update socks proxy * Listen to server config value changes - Update debug log level * Listen to server config value changes - Update system tray icon * Update config values one at a time In case settings are changed in quick succession it's possible that each setting update reverts the change of the previous changed setting because the internal config hasn't been updated yet. E.g. 1. settingA changed 2. settingB changed 3. settingA updates config file 4. settingB updates config file (internal config hasn't been updated yet with change from settingA) 5. settingA updates internal config (settingA updated) 6. settingB updates internal config (settingB updated, settingA outdated) now settingA is unchanged because settingB reverted its change while updating the config with its new value * Always add log interceptor to OkHttpClient In case debug logs are disabled then the KotlinLogging log level will be set to level > debug and thus, these logs won't get logged * Rename "maxParallelUpdateRequests" to "maxSourcesInParallel" * Use server setting "maxSourcesInParallel" for downloads * Listen to server config value changes - downloads * Always use latest server settings - Browser * Always use latest server settings - folders * [Test] Fix type error
This commit is contained in:
@@ -33,6 +33,7 @@ import suwayomi.tachidesk.util.HAScheduler
|
||||
import java.util.Date
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.prefs.Preferences
|
||||
import kotlin.math.absoluteValue
|
||||
import kotlin.time.Duration.Companion.hours
|
||||
|
||||
class Updater : IUpdater {
|
||||
@@ -45,13 +46,36 @@ class Updater : IUpdater {
|
||||
private val tracker = ConcurrentHashMap<Int, UpdateJob>()
|
||||
private val updateChannels = ConcurrentHashMap<String, Channel<UpdateJob>>()
|
||||
|
||||
private val semaphore = Semaphore(serverConfig.maxParallelUpdateRequests)
|
||||
private var maxSourcesInParallel = 20 // max permits, necessary to be set to be able to release up to 20 permits
|
||||
private val semaphore = Semaphore(maxSourcesInParallel)
|
||||
|
||||
private val lastAutomatedUpdateKey = "lastAutomatedUpdateKey"
|
||||
private val preferences = Preferences.userNodeForPackage(Updater::class.java)
|
||||
|
||||
private var currentUpdateTaskId = ""
|
||||
|
||||
init {
|
||||
serverConfig.subscribeTo(serverConfig.globalUpdateInterval, ::scheduleUpdateTask)
|
||||
serverConfig.subscribeTo(
|
||||
serverConfig.maxSourcesInParallel,
|
||||
{ value ->
|
||||
val newMaxPermits = value.coerceAtLeast(1).coerceAtMost(20)
|
||||
val permitDifference = maxSourcesInParallel - newMaxPermits
|
||||
maxSourcesInParallel = newMaxPermits
|
||||
|
||||
val addMorePermits = permitDifference < 0
|
||||
for (i in 1..permitDifference.absoluteValue) {
|
||||
if (addMorePermits) {
|
||||
semaphore.release()
|
||||
} else {
|
||||
semaphore.acquire()
|
||||
}
|
||||
}
|
||||
},
|
||||
ignoreInitialValue = false
|
||||
)
|
||||
}
|
||||
|
||||
private fun autoUpdateTask() {
|
||||
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
|
||||
preferences.putLong(lastAutomatedUpdateKey, System.currentTimeMillis())
|
||||
@@ -61,19 +85,19 @@ class Updater : IUpdater {
|
||||
return
|
||||
}
|
||||
|
||||
logger.info { "Trigger global update (interval= ${serverConfig.globalUpdateInterval}h, lastAutomatedUpdate= ${Date(lastAutomatedUpdate)})" }
|
||||
logger.info { "Trigger global update (interval= ${serverConfig.globalUpdateInterval.value}h, lastAutomatedUpdate= ${Date(lastAutomatedUpdate)})" }
|
||||
addCategoriesToUpdateQueue(Category.getCategoryList(), clear = true, forceAll = false)
|
||||
}
|
||||
|
||||
fun scheduleUpdateTask() {
|
||||
HAScheduler.deschedule(currentUpdateTaskId)
|
||||
|
||||
val isAutoUpdateDisabled = serverConfig.globalUpdateInterval == 0.0
|
||||
val isAutoUpdateDisabled = serverConfig.globalUpdateInterval.value == 0.0
|
||||
if (isAutoUpdateDisabled) {
|
||||
return
|
||||
}
|
||||
|
||||
val updateInterval = serverConfig.globalUpdateInterval.hours.coerceAtLeast(6.hours).inWholeMilliseconds
|
||||
val updateInterval = serverConfig.globalUpdateInterval.value.hours.coerceAtLeast(6.hours).inWholeMilliseconds
|
||||
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
|
||||
val timeToNextExecution = (updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate)).mod(updateInterval)
|
||||
|
||||
@@ -150,9 +174,9 @@ class Updater : IUpdater {
|
||||
val mangasToUpdate = categoriesToUpdateMangas
|
||||
.asSequence()
|
||||
.filter { it.updateStrategy == UpdateStrategy.ALWAYS_UPDATE }
|
||||
.filter { if (serverConfig.excludeUnreadChapters) { (it.unreadCount ?: 0L) == 0L } else true }
|
||||
.filter { if (serverConfig.excludeNotStarted) { it.lastReadAt != null } else true }
|
||||
.filter { if (serverConfig.excludeCompleted) { it.status != MangaStatus.COMPLETED.name } else true }
|
||||
.filter { if (serverConfig.excludeUnreadChapters.value) { (it.unreadCount ?: 0L) == 0L } else true }
|
||||
.filter { if (serverConfig.excludeNotStarted.value) { it.lastReadAt != null } else true }
|
||||
.filter { if (serverConfig.excludeCompleted.value) { it.status != MangaStatus.COMPLETED.name } else true }
|
||||
.filter { forceAll || !excludedCategories.any { category -> mangasToCategoriesMap[it.id]?.contains(category) == true } }
|
||||
.toList()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user