Skip to content

Commit

Permalink
Correctly set the "firstExecutionTime" of a "HATask"
Browse files Browse the repository at this point in the history
In case an initial delay is used for "Timer::scheduleAtFixedRate" (e.g. when rescheduling) then the "firstExecutionTime" of the "HATask" was incorrect, since it considered the first execution to be based on the actual interval.
This caused
 - calculations for execution times (e.g. "timeToNextExecution", "nextExecutionTime") to be incorrect
 - the ordering of the "scheduledTasks" queue to be incorrect
  • Loading branch information
schroda committed Jul 24, 2023
1 parent e53b9d4 commit 776870d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class HACronTask(id: String, val cronExpr: String, execute: () -> Unit, name: St
}
}

class HATask(id: String, val interval: Long, execute: () -> Unit, val timerTask: TimerTask, name: String?) : BaseHATask(id, execute, name) {
private val firstExecutionTime = System.currentTimeMillis() + interval
class HATask(id: String, val interval: Long, execute: () -> Unit, val timerTask: TimerTask, name: String?, initialDelay: Long = interval) : BaseHATask(id, execute, name) {
private val firstExecutionTime = System.currentTimeMillis() + initialDelay

private fun getElapsedTimeOfCurrentInterval(): Long {
val timeSinceFirstExecution = System.currentTimeMillis() - firstExecutionTime
Expand Down Expand Up @@ -179,7 +179,7 @@ object HAScheduler {
val intervalDifference = interval - task.interval
val remainingTimeTillNextExecution = (timeToNextExecution + intervalDifference).coerceAtLeast(0)

scheduledTasks.add(HATask(taskId, interval, task.execute, timerTask, task.name))
scheduledTasks.add(HATask(taskId, interval, task.execute, timerTask, task.name, initialDelay = remainingTimeTillNextExecution))
timer.scheduleAtFixedRate(timerTask, remainingTimeTillNextExecution, interval)
}

Expand Down

0 comments on commit 776870d

Please sign in to comment.