fix: Trigger timetable fetch without restarting

This commit is contained in:
2025-03-20 18:14:20 +01:00
parent 7a9731ac32
commit 8d6c24752b
2 changed files with 18 additions and 1 deletions
@@ -0,0 +1,5 @@
package de.twomartens.timetable.bahnApi.events
import org.springframework.context.ApplicationEvent
class FetchTasksCreatedEvent(source: Any) : ApplicationEvent(source)
@@ -1,5 +1,6 @@
package de.twomartens.timetable.bahnApi.service
import de.twomartens.timetable.bahnApi.events.FetchTasksCreatedEvent
import de.twomartens.timetable.bahnApi.model.Eva
import de.twomartens.timetable.bahnApi.model.FetchDates
import de.twomartens.timetable.bahnApi.model.TaskFactory
@@ -10,6 +11,7 @@ import de.twomartens.timetable.types.Hour
import de.twomartens.timetable.types.HourAtDay
import mu.KotlinLogging
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.ApplicationEventPublisher
import org.springframework.context.event.EventListener
import org.springframework.data.mongodb.core.BulkOperations
import org.springframework.data.mongodb.core.MongoTemplate
@@ -22,7 +24,8 @@ class ScheduledTaskService(
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository,
private val taskFactory: TaskFactory,
private val fetchTaskScheduler: FetchTaskScheduler,
private val mongoTemplate: MongoTemplate
private val mongoTemplate: MongoTemplate,
private val eventPublisher: ApplicationEventPublisher
) {
private var createdTime: Instant = Instant.EPOCH
private var lastUpdate: Instant = Instant.EPOCH
@@ -34,6 +37,13 @@ class ScheduledTaskService(
updateTaskCounterAndScheduleTasks(updateTime)
}
@EventListener(ApplicationReadyEvent::class)
fun onFetchTasksCreated(event: FetchTasksCreatedEvent) {
log.info { "Scheduled tasks created" }
val updateTime = Instant.ofEpochMilli(event.timestamp)
updateTaskCounterAndScheduleTasks(updateTime)
}
private fun updateTaskCounterAndScheduleTasks(updateTime: Instant) {
log.info { "Update tasks from database and schedule" }
val createdTasks = findTasksCreatedSince(lastUpdate)
@@ -50,6 +60,8 @@ class ScheduledTaskService(
val newTasks = buildScheduledTasks(tswRoute, fetchDates)
storeTasksInDatabaseAndStoreCreationTime(newTasks)
val event = FetchTasksCreatedEvent(this)
eventPublisher.publishEvent(event)
}
private fun calculateDatesToFetch(fetchedDate: LocalDate): FetchDates {