mirror of
https://github.com/2martens/tsw-timetable.git
synced 2026-10-17 16:06:51 +02:00
fix: Trigger timetable fetch without restarting
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package de.twomartens.timetable.bahnApi.events
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationEvent
|
||||||
|
|
||||||
|
class FetchTasksCreatedEvent(source: Any) : ApplicationEvent(source)
|
||||||
+13
-1
@@ -1,5 +1,6 @@
|
|||||||
package de.twomartens.timetable.bahnApi.service
|
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.Eva
|
||||||
import de.twomartens.timetable.bahnApi.model.FetchDates
|
import de.twomartens.timetable.bahnApi.model.FetchDates
|
||||||
import de.twomartens.timetable.bahnApi.model.TaskFactory
|
import de.twomartens.timetable.bahnApi.model.TaskFactory
|
||||||
@@ -10,6 +11,7 @@ import de.twomartens.timetable.types.Hour
|
|||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent
|
import org.springframework.boot.context.event.ApplicationReadyEvent
|
||||||
|
import org.springframework.context.ApplicationEventPublisher
|
||||||
import org.springframework.context.event.EventListener
|
import org.springframework.context.event.EventListener
|
||||||
import org.springframework.data.mongodb.core.BulkOperations
|
import org.springframework.data.mongodb.core.BulkOperations
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate
|
import org.springframework.data.mongodb.core.MongoTemplate
|
||||||
@@ -22,7 +24,8 @@ class ScheduledTaskService(
|
|||||||
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository,
|
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository,
|
||||||
private val taskFactory: TaskFactory,
|
private val taskFactory: TaskFactory,
|
||||||
private val fetchTaskScheduler: FetchTaskScheduler,
|
private val fetchTaskScheduler: FetchTaskScheduler,
|
||||||
private val mongoTemplate: MongoTemplate
|
private val mongoTemplate: MongoTemplate,
|
||||||
|
private val eventPublisher: ApplicationEventPublisher
|
||||||
) {
|
) {
|
||||||
private var createdTime: Instant = Instant.EPOCH
|
private var createdTime: Instant = Instant.EPOCH
|
||||||
private var lastUpdate: Instant = Instant.EPOCH
|
private var lastUpdate: Instant = Instant.EPOCH
|
||||||
@@ -34,6 +37,13 @@ class ScheduledTaskService(
|
|||||||
updateTaskCounterAndScheduleTasks(updateTime)
|
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) {
|
private fun updateTaskCounterAndScheduleTasks(updateTime: Instant) {
|
||||||
log.info { "Update tasks from database and schedule" }
|
log.info { "Update tasks from database and schedule" }
|
||||||
val createdTasks = findTasksCreatedSince(lastUpdate)
|
val createdTasks = findTasksCreatedSince(lastUpdate)
|
||||||
@@ -50,6 +60,8 @@ class ScheduledTaskService(
|
|||||||
val newTasks = buildScheduledTasks(tswRoute, fetchDates)
|
val newTasks = buildScheduledTasks(tswRoute, fetchDates)
|
||||||
|
|
||||||
storeTasksInDatabaseAndStoreCreationTime(newTasks)
|
storeTasksInDatabaseAndStoreCreationTime(newTasks)
|
||||||
|
val event = FetchTasksCreatedEvent(this)
|
||||||
|
eventPublisher.publishEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateDatesToFetch(fetchedDate: LocalDate): FetchDates {
|
private fun calculateDatesToFetch(fetchedDate: LocalDate): FetchDates {
|
||||||
|
|||||||
Reference in New Issue
Block a user