Fix/initial scheduling of global update (#1416)

* Fix setting initial global update delay

In case no update has run yet, and the "last automated update" defaulted to 0, the calculation always resulted in a multiple of the interval.

This resulted for e.g., interval 24, to always be scheduled at 00:00.
For e.g., interval 6, it was always one of the following times: 00:00, 06:00, 12:00, 18:00; depending on the current system time.

* Delete the existing "last automated update time"

So that the update will be triggered based on the time the server got started again after the update.

* Extract migrations into separate functions

* Cleanup migration execution logic
This commit is contained in:
schroda
2025-06-12 17:44:38 +02:00
committed by GitHub
parent f04060b31b
commit ecea2ecdf5
3 changed files with 90 additions and 33 deletions

View File

@@ -8,6 +8,8 @@ import suwayomi.tachidesk.manga.model.dataclass.MangaDataClass
interface IUpdater {
fun getLastUpdateTimestamp(): Long
fun deleteLastAutomatedUpdateTimestamp()
fun addCategoriesToUpdateQueue(
categories: List<CategoryDataClass>,
clear: Boolean?,

View File

@@ -116,10 +116,24 @@ class Updater : IUpdater {
override fun getLastUpdateTimestamp(): Long = preferences.getLong(lastUpdateKey, 0)
fun saveLastUpdateTimestamp() {
preferences.edit().putLong(lastUpdateKey, System.currentTimeMillis()).apply()
}
fun getLastAutomatedUpdateTimestamp(): Long = preferences.getLong(lastAutomatedUpdateKey, 0)
fun saveLastAutomatedUpdateTimestamp() {
preferences.edit().putLong(lastAutomatedUpdateKey, System.currentTimeMillis()).apply()
}
override fun deleteLastAutomatedUpdateTimestamp() {
preferences.edit().remove(lastAutomatedUpdateKey).apply()
}
private fun autoUpdateTask() {
try {
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
preferences.edit().putLong(lastAutomatedUpdateKey, System.currentTimeMillis()).apply()
val lastAutomatedUpdate = getLastAutomatedUpdateTimestamp()
saveLastAutomatedUpdateTimestamp()
if (getStatus().isRunning) {
logger.debug { "Global update is already in progress" }
@@ -150,12 +164,23 @@ class Updater : IUpdater {
serverConfig.globalUpdateInterval.value.hours
.coerceAtLeast(6.hours)
.inWholeMilliseconds
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
val timeToNextExecution = (updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate)).mod(updateInterval)
val lastAutomatedUpdate = getLastAutomatedUpdateTimestamp()
val isInitialScheduling = lastAutomatedUpdate == 0L
val timeToNextExecution =
if (!isInitialScheduling) {
(updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate)).mod(updateInterval)
} else {
updateInterval
}
if (isInitialScheduling) {
saveLastAutomatedUpdateTimestamp()
}
val wasPreviousUpdateTriggered =
System.currentTimeMillis() - (
if (lastAutomatedUpdate > 0) lastAutomatedUpdate else System.currentTimeMillis()
if (!isInitialScheduling) lastAutomatedUpdate else System.currentTimeMillis()
) < updateInterval
if (!wasPreviousUpdateTriggered) {
GlobalScope.launch {
@@ -316,7 +341,7 @@ class Updater : IUpdater {
clear: Boolean?,
forceAll: Boolean,
) {
preferences.edit().putLong(lastUpdateKey, System.currentTimeMillis()).apply()
saveLastUpdateTimestamp()
if (clear == true) {
reset()