Skip to content

Commit

Permalink
Use new scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
schroda committed Jul 16, 2023
1 parent 9726bf6 commit 79944cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ package suwayomi.tachidesk.manga.impl.backup.proto
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import eu.kanade.tachiyomi.source.model.UpdateStrategy
import it.sauronsoftware.cron4j.Scheduler
import it.sauronsoftware.cron4j.Task
import it.sauronsoftware.cron4j.TaskExecutionContext
import mu.KotlinLogging
import okio.buffer
import okio.gzip
Expand Down Expand Up @@ -39,6 +36,7 @@ import suwayomi.tachidesk.manga.model.table.SourceTable
import suwayomi.tachidesk.manga.model.table.toDataClass
import suwayomi.tachidesk.server.ApplicationDirs
import suwayomi.tachidesk.server.serverConfig
import suwayomi.tachidesk.util.HAScheduler
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
Expand All @@ -55,25 +53,17 @@ object ProtoBackupExport : ProtoBackupBase() {
private const val lastAutomatedBackupKey = "lastAutomatedBackupKey"
private val preferences = Preferences.userNodeForPackage(ProtoBackupExport::class.java)

private val scheduler = Scheduler()

fun scheduleAutomatedBackupTask() {
scheduler.deschedule(backupSchedulerJobId)
HAScheduler.deschedule(backupSchedulerJobId)

if (!serverConfig.automatedBackups) {
return
}

if (!scheduler.isStarted) {
scheduler.start()
}

val task = object : Task() {
override fun execute(context: TaskExecutionContext?) {
cleanupAutomatedBackups()
createAutomatedBackup()
preferences.putLong(lastAutomatedBackupKey, System.currentTimeMillis())
}
val task = {
cleanupAutomatedBackups()
createAutomatedBackup()
preferences.putLong(lastAutomatedBackupKey, System.currentTimeMillis())
}

val (hour, minute) = serverConfig.backupTime.split(":").map { it.toInt() }
Expand All @@ -83,15 +73,13 @@ object ProtoBackupExport : ProtoBackupBase() {

// trigger last backup in case the server wasn't running on the scheduled time
val lastAutomatedBackup = preferences.getLong(lastAutomatedBackupKey, System.currentTimeMillis())
val wasPreviousBackupTriggered = (System.currentTimeMillis() - lastAutomatedBackup) < backupInterval.inWholeMilliseconds
val wasPreviousBackupTriggered =
(System.currentTimeMillis() - lastAutomatedBackup) < backupInterval.inWholeMilliseconds
if (!wasPreviousBackupTriggered) {
scheduler.launch(task)
task()
}

backupSchedulerJobId = scheduler.schedule(
"$backupMinute $backupHour */${backupInterval.inWholeDays} * *",
task
)
HAScheduler.schedule(task, "$backupMinute $backupHour */${backupInterval.inWholeDays} * *", "backup")
}

private fun createAutomatedBackup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package suwayomi.tachidesk.manga.impl.update

import eu.kanade.tachiyomi.source.model.UpdateStrategy
import it.sauronsoftware.cron4j.Task
import it.sauronsoftware.cron4j.TaskExecutionContext
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -24,11 +26,13 @@ import org.kodein.di.instance
import suwayomi.tachidesk.manga.impl.Category
import suwayomi.tachidesk.manga.impl.CategoryManga
import suwayomi.tachidesk.manga.impl.Chapter
import suwayomi.tachidesk.manga.impl.backup.proto.ProtoBackupExport
import suwayomi.tachidesk.manga.model.dataclass.CategoryDataClass
import suwayomi.tachidesk.manga.model.dataclass.IncludeInUpdate
import suwayomi.tachidesk.manga.model.dataclass.MangaDataClass
import suwayomi.tachidesk.manga.model.table.MangaStatus
import suwayomi.tachidesk.server.serverConfig
import suwayomi.tachidesk.util.HAScheduler
import java.util.Date
import java.util.Timer
import java.util.TimerTask
Expand All @@ -51,41 +55,46 @@ class Updater : IUpdater {
private val lastAutomatedUpdateKey = "lastAutomatedUpdateKey"
private val preferences = Preferences.userNodeForPackage(Updater::class.java)

private val updateTimer = Timer()
private var currentUpdateTask: TimerTask? = null
private var currentUpdateTaskId = ""

init {
scheduleUpdateTask()
}


private fun autoUpdateTask() {
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
preferences.putLong(lastAutomatedUpdateKey, System.currentTimeMillis())

if (status.value.running) {
logger.debug { "Global update is already in progress" }
return
}

logger.info { "Trigger global update (interval= ${serverConfig.globalUpdateInterval}h, lastAutomatedUpdate= ${Date(lastAutomatedUpdate)})" }
addCategoriesToUpdateQueue(Category.getCategoryList(), true)
}

private fun scheduleUpdateTask() {
HAScheduler.deschedule(currentUpdateTaskId)

if (!serverConfig.automaticallyTriggerGlobalUpdate) {
return
}

val minInterval = 6.hours
val interval = serverConfig.globalUpdateInterval.hours
val updateInterval = interval.coerceAtLeast(minInterval).inWholeMilliseconds

val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0)
val initialDelay = updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate) % updateInterval

currentUpdateTask?.cancel()
currentUpdateTask = object : TimerTask() {
override fun run() {
preferences.putLong(lastAutomatedUpdateKey, System.currentTimeMillis())

if (status.value.running) {
logger.debug { "Global update is already in progress, do not trigger global update" }
return
}

logger.info { "Trigger global update (interval= ${serverConfig.globalUpdateInterval}h, lastAutomatedUpdate= ${Date(lastAutomatedUpdate)})" }
addCategoriesToUpdateQueue(Category.getCategoryList(), true)
}
val updateInterval = interval.coerceAtLeast(minInterval)
val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, System.currentTimeMillis())

// trigger update in case the server wasn't running on the scheduled time
val wasPreviousUpdateTriggered =
(System.currentTimeMillis() - lastAutomatedUpdate) < updateInterval.inWholeMilliseconds
if (!wasPreviousUpdateTriggered) {
autoUpdateTask()
}

updateTimer.scheduleAtFixedRate(currentUpdateTask, initialDelay, updateInterval)
HAScheduler.schedule(::autoUpdateTask, "* */${updateInterval.inWholeHours} * * *", "global-update")
}

private fun getOrCreateUpdateChannelFor(source: String): Channel<UpdateJob> {
Expand Down

0 comments on commit 79944cd

Please sign in to comment.