Initialize scheduled tasks after application launch from database

This commit is contained in:
2023-10-04 08:08:53 +02:00
parent 6f589e048f
commit 805f0a7219
7 changed files with 90 additions and 12 deletions
@@ -1,7 +1,6 @@
package de.twomartens.timetable package de.twomartens.timetable
import de.twomartens.timetable.bahnApi.service.BahnApiService import de.twomartens.timetable.service.ScheduledTaskService
import de.twomartens.timetable.bahnApi.service.BahnDatabaseService
import mu.KotlinLogging import mu.KotlinLogging
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.boot.context.event.ApplicationReadyEvent
@@ -9,23 +8,16 @@ import org.springframework.boot.runApplication
import org.springframework.context.event.EventListener import org.springframework.context.event.EventListener
import org.springframework.data.mongodb.config.EnableMongoAuditing import org.springframework.data.mongodb.config.EnableMongoAuditing
import org.springframework.scheduling.annotation.EnableScheduling import org.springframework.scheduling.annotation.EnableScheduling
import java.time.LocalDate
import java.time.LocalTime
import java.time.Month
@EnableMongoAuditing @EnableMongoAuditing
@EnableScheduling @EnableScheduling
@SpringBootApplication @SpringBootApplication
open class MainApplication( open class MainApplication(
private val bahnApiService: BahnApiService, private val scheduledTaskService: ScheduledTaskService
private val bahnDatabaseService: BahnDatabaseService
) { ) {
@EventListener(ApplicationReadyEvent::class) @EventListener(ApplicationReadyEvent::class)
fun ready() { fun ready() {
val timetable = bahnApiService.fetchTimetable(8000207, scheduledTaskService.initializeScheduledTasks()
LocalDate.of(2023, Month.OCTOBER, 1),
LocalTime.of(3, 0))
log.info { "bs" }
} }
companion object { companion object {
@@ -12,7 +12,7 @@ import java.time.Instant
import java.time.LocalDateTime import java.time.LocalDateTime
@Document @Document
@CompoundIndex(def = "{'eva': 1, 'dateTime': 1}") @CompoundIndex(def = "{'eva': 1, 'dateTime': 1}", unique = true)
data class BahnTimetable( data class BahnTimetable(
var eva: Int, var eva: Int,
var dateTime: LocalDateTime, var dateTime: LocalDateTime,
@@ -0,0 +1,30 @@
package de.twomartens.timetable.bahnApi.model.db
import org.bson.types.ObjectId
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.Id
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.mapping.Document
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
@Document
@CompoundIndex(def = "{'eva': 1, 'fetchedDate': 1, 'fetchedTime': 1", unique = true)
data class ScheduledFetchTask(
var eva: Int,
var fetchedDate: LocalDate,
var fetchedTime: LocalTime,
var scheduledExecutionDateTime: LocalDateTime
) {
@Id
var id: ObjectId = ObjectId()
@CreatedDate
lateinit var created: Instant
@LastModifiedDate
lateinit var lastModified: Instant
}
@@ -0,0 +1,6 @@
package de.twomartens.timetable.bahnApi.repository
import de.twomartens.timetable.bahnApi.model.db.ScheduledFetchTask
import org.springframework.data.mongodb.repository.MongoRepository
interface ScheduledFetchTaskRepository : MongoRepository<ScheduledFetchTask, String>
@@ -0,0 +1,15 @@
package de.twomartens.timetable.bahnApi.tasks
import de.twomartens.timetable.bahnApi.model.db.ScheduledFetchTask
import de.twomartens.timetable.bahnApi.repository.ScheduledFetchTaskRepository
import org.springframework.scheduling.annotation.Async
@Async
open class DeleteScheduledTask(
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository,
private val scheduledFetchTask: ScheduledFetchTask
) : Runnable {
override fun run() {
scheduledFetchTaskRepository.delete(scheduledFetchTask)
}
}
@@ -1,5 +1,6 @@
package de.twomartens.timetable.bahnApi.tasks package de.twomartens.timetable.bahnApi.tasks
import de.twomartens.timetable.bahnApi.repository.ScheduledFetchTaskRepository
import de.twomartens.timetable.bahnApi.service.BahnApiService import de.twomartens.timetable.bahnApi.service.BahnApiService
import de.twomartens.timetable.bahnApi.service.BahnDatabaseService import de.twomartens.timetable.bahnApi.service.BahnDatabaseService
import org.springframework.scheduling.annotation.Async import org.springframework.scheduling.annotation.Async
@@ -0,0 +1,34 @@
package de.twomartens.timetable.service
import de.twomartens.timetable.bahnApi.repository.ScheduledFetchTaskRepository
import de.twomartens.timetable.bahnApi.service.BahnApiService
import de.twomartens.timetable.bahnApi.service.BahnDatabaseService
import de.twomartens.timetable.bahnApi.tasks.DeleteScheduledTask
import de.twomartens.timetable.bahnApi.tasks.FetchTimetableTask
import de.twomartens.timetable.property.TimeProperties
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
import org.springframework.stereotype.Service
@Service
class ScheduledTaskService(
private val timeProperties: TimeProperties,
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository,
private val threadPoolTaskScheduler: ThreadPoolTaskScheduler,
private val bahnApiService: BahnApiService,
private val bahnDatabaseService: BahnDatabaseService,
private val threadPoolTaskExecutor: ThreadPoolTaskExecutor
) {
fun initializeScheduledTasks() {
val scheduledTasks = scheduledFetchTaskRepository.findAll()
scheduledTasks.forEach {
val zonedExecutionTime = it.scheduledExecutionDateTime
.atZone(timeProperties.defaultTimeZone)
val deleteTask = DeleteScheduledTask(scheduledFetchTaskRepository, it)
val timetableTask = FetchTimetableTask(it.eva, it.fetchedDate, it.fetchedTime,
bahnApiService, bahnDatabaseService, threadPoolTaskExecutor)
threadPoolTaskScheduler.schedule(timetableTask, zonedExecutionTime.toInstant())
threadPoolTaskScheduler.schedule(deleteTask, zonedExecutionTime.toInstant())
}
}
}