Skip to content

Commit

Permalink
Fix/ha scheduler not triggering missed executions due to not meeting …
Browse files Browse the repository at this point in the history
…the threshold (#612)

* Check correctly if task threshold was met

It was incorrectly considered to be met in case the remaining time till the next execution was less than the threshold.
Instead, it has to be greater, since that would mean, that the next execution is taking long enough to not be triggering a double execution

Thus, the current logic is not, as intended, preventing possible double executions and instead is making sure to only execute missed tasks in case it will lead to double executions...

* Always trigger missed executions

The idea to have a threshold to prevent double executions in case the next scheduled execution isn't too far in the future doesn't really work with big intervals (e.g. in the days range).
For such cases, multiple days left for the next executions could be considered to cause double executions.

Decreasing the threshold doesn't really work since then it wouldn't really work for low intervals.
Instead, it makes more sense to just allow possible double executions and to just live with it.
In case it would be a problem for a specific task, the task should handle this issue itself.
  • Loading branch information
schroda authored Jul 23, 2023
1 parent 027805c commit e53b9d4
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,8 @@ object HAScheduler {
if (systemWasInHibernation) {
logger.debug { "System hibernation detected, task was delayed by ${elapsedTime - interval.inWholeMilliseconds}ms" }
scheduledTasks.forEach {
val missedExecution = currentTime - it.getLastExecutionTime() - elapsedTime < 0
val taskInterval = it.getNextExecutionTime() - it.getLastExecutionTime()
// in case the next task execution doesn't take long the missed execution can be ignored to prevent a double execution
val taskThresholdMet = taskInterval * TASK_THRESHOLD > it.getTimeToNextExecution()

val triggerTask = missedExecution && taskThresholdMet
if (triggerTask) {
val wasLastExecutionMissed = currentTime - it.getLastExecutionTime() - elapsedTime < 0
if (wasLastExecutionMissed) {
logger.debug { "Task \"${it.name ?: it.id}\" missed its execution, executing now..." }

when (it) {
Expand All @@ -121,7 +116,7 @@ object HAScheduler {
}

// queue is ordered by next execution time, thus, loop can be exited early
if (!missedExecution) {
if (!wasLastExecutionMissed) {
return@forEach
}
}
Expand Down

0 comments on commit e53b9d4

Please sign in to comment.