Fix/ha scheduler not triggering missed executions due to not meeting 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.
This commit is contained in:
schroda
2023-07-23 18:40:22 +02:00
committed by GitHub
parent 027805c4d5
commit e53b9d4790
@@ -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) {
@@ -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
}
}