Fix/updater scheduling auto updates (#615)

* Trigger missed auto global update immediately on server start

In case the last execution was missed, it was never immediately scheduled.
Thus, it had to be waited for the next scheduled execution to be executed.

* Schedule auto global updates at a later point during the startup

In case a global update was triggered immediately, the server setup wasn't far enough causing an error due to trying to use things (e.g. database) that weren't initialized yet
This commit is contained in:
schroda
2023-07-26 02:33:57 +02:00
committed by GitHub
parent 9e4c90f220
commit 7ebefa7c42
2 changed files with 11 additions and 7 deletions

View File

@@ -52,10 +52,6 @@ class Updater : IUpdater {
private var currentUpdateTaskId = ""
init {
scheduleUpdateTask()
}
private fun autoUpdateTask() {
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
preferences.putLong(lastAutomatedUpdateKey, System.currentTimeMillis())
@@ -69,7 +65,7 @@ class Updater : IUpdater {
addCategoriesToUpdateQueue(Category.getCategoryList(), true)
}
private fun scheduleUpdateTask() {
fun scheduleUpdateTask() {
HAScheduler.deschedule(currentUpdateTaskId)
val isAutoUpdateDisabled = serverConfig.globalUpdateInterval == 0.0
@@ -79,9 +75,14 @@ class Updater : IUpdater {
val updateInterval = serverConfig.globalUpdateInterval.hours.coerceAtLeast(6.hours).inWholeMilliseconds
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
val initialDelay = updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate) % updateInterval
val timeToNextExecution = updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate) % updateInterval
HAScheduler.schedule(::autoUpdateTask, updateInterval, initialDelay, "global-update")
val wasPreviousUpdateTriggered = System.currentTimeMillis() - (if (lastAutomatedUpdate > 0) lastAutomatedUpdate else System.currentTimeMillis()) < updateInterval
if (!wasPreviousUpdateTriggered) {
autoUpdateTask()
}
HAScheduler.schedule(::autoUpdateTask, updateInterval, timeToNextExecution, "global-update")
}
private fun getOrCreateUpdateChannelFor(source: String): Channel<UpdateJob> {

View File

@@ -184,6 +184,9 @@ fun applicationSetup() {
// AES/CBC/PKCS7Padding Cypher provider for zh.copymanga
Security.addProvider(BouncyCastleProvider())
// start automated global updates
updater.scheduleUpdateTask()
// start automated backups
ProtoBackupExport.scheduleAutomatedBackupTask()