Fixed missing counter reset

This commit is contained in:
2023-10-26 18:55:51 +02:00
parent 0ea147fa8e
commit 50ec898257
@@ -15,7 +15,8 @@ class TaskFactory {
private val _tasksPerHour: MutableMap<HourAtDay, Int> = mutableMapOf() private val _tasksPerHour: MutableMap<HourAtDay, Int> = mutableMapOf()
fun initializeWithExistingTasks(existingTasks: List<ScheduledFetchTask>) { fun initializeWithExistingTasks(existingTasks: List<ScheduledFetchTask>) {
existingTasks.forEach { countTask(it) } resetCounter()
countTasks(existingTasks)
} }
fun createTaskAndUpdateCounter(eva: Eva, hourAtDay: HourAtDay): ScheduledFetchTask { fun createTaskAndUpdateCounter(eva: Eva, hourAtDay: HourAtDay): ScheduledFetchTask {
@@ -41,6 +42,10 @@ class TaskFactory {
return hourAtDay.dateTime.plusSeconds(secondInHourToExecute.toLong()) return hourAtDay.dateTime.plusSeconds(secondInHourToExecute.toLong())
} }
private fun countTasks(existingTasks: List<ScheduledFetchTask>) {
existingTasks.forEach { countTask(it) }
}
private fun countTask(scheduledFetchTask: ScheduledFetchTask) { private fun countTask(scheduledFetchTask: ScheduledFetchTask) {
val scheduledTime = scheduledFetchTask.scheduledExecutionDateTime val scheduledTime = scheduledFetchTask.scheduledExecutionDateTime
val hourAtDay = HourAtDay.of(scheduledTime) val hourAtDay = HourAtDay.of(scheduledTime)
@@ -55,4 +60,7 @@ class TaskFactory {
private fun getNumberOfTasksInHour(hourAtDay: HourAtDay) = private fun getNumberOfTasksInHour(hourAtDay: HourAtDay) =
_tasksPerHour.computeIfAbsent(hourAtDay) { ZERO } _tasksPerHour.computeIfAbsent(hourAtDay) { ZERO }
} private fun resetCounter() {
_tasksPerHour.clear()
}
}