diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/service/ProcessTimetableTask.kt b/module-server/src/main/kotlin/de/twomartens/timetable/service/ProcessTimetableTask.kt index 414c825..df4f89f 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/service/ProcessTimetableTask.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/service/ProcessTimetableTask.kt @@ -1,7 +1,7 @@ package de.twomartens.timetable.service -import de.twomartens.timetable.model.db.Timetable import de.twomartens.timetable.model.dto.TimetableState +import de.twomartens.timetable.timetable.TimetableRepository import mu.KotlinLogging import org.springframework.batch.core.Job import org.springframework.batch.core.JobParametersBuilder @@ -14,7 +14,7 @@ import org.springframework.scheduling.annotation.Async class ProcessTimetableTask( private val asyncJobLauncher: JobLauncher, private val processTimetableJob: Job, - private val tswTimetable: Timetable, + private val timetableRepository: TimetableRepository, private val repository: ScheduledProcessTimetableTaskRepository, private val scheduledTask: ScheduledProcessTimetableTask) : Runnable { @@ -24,14 +24,15 @@ class ProcessTimetableTask( } fun processTimetable() { - if (tswTimetable.timetableState != TimetableState.TIMETABLES_FETCHED) { - log.info("Timetable ${tswTimetable.timetableId} is not in TIMETABLES_FETCHED state") + val timetable = timetableRepository.findByUserIdAndTimetableId(scheduledTask.userId, scheduledTask.timetableId)!! + if (timetable.timetableState != TimetableState.TIMETABLES_FETCHED) { + log.info("Timetable ${scheduledTask.timetableId} is not in TIMETABLES_FETCHED state") return } val jobParameters = JobParametersBuilder() - .addString("timetableId", tswTimetable.timetableId.value) - .addString("userId", tswTimetable.userId.value) + .addString("timetableId", scheduledTask.timetableId.value) + .addString("userId", scheduledTask.userId.value) .toJobParameters() try { diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/service/ScheduledProcessTimetableTask.kt b/module-server/src/main/kotlin/de/twomartens/timetable/service/ScheduledProcessTimetableTask.kt index de79cb4..65fc9ee 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/service/ScheduledProcessTimetableTask.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/service/ScheduledProcessTimetableTask.kt @@ -2,7 +2,6 @@ package de.twomartens.timetable.service import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId -import de.twomartens.timetable.model.db.Timetable import org.bson.types.ObjectId import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.Id @@ -16,7 +15,6 @@ import java.time.Instant data class ScheduledProcessTimetableTask( var userId: UserId, var timetableId: TimetableId, - var timetable: Timetable, var executionTime: Instant ) { @Id diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/service/TimetableProcessingScheduler.kt b/module-server/src/main/kotlin/de/twomartens/timetable/service/TimetableProcessingScheduler.kt index a76b727..a7276c7 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/service/TimetableProcessingScheduler.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/service/TimetableProcessingScheduler.kt @@ -1,6 +1,7 @@ package de.twomartens.timetable.service import de.twomartens.timetable.model.db.Timetable +import de.twomartens.timetable.timetable.TimetableRepository import org.springframework.batch.core.Job import org.springframework.batch.core.launch.JobLauncher import org.springframework.boot.context.event.ApplicationReadyEvent @@ -16,6 +17,7 @@ class TimetableProcessingScheduler( private val asyncJobLauncher: JobLauncher, private val threadPoolTaskScheduler: ThreadPoolTaskScheduler, private val repository: ScheduledProcessTimetableTaskRepository, + private val timetableRepository: TimetableRepository, private val clock: Clock, private val eventPublisher: ApplicationEventPublisher ) { @@ -24,7 +26,7 @@ class TimetableProcessingScheduler( val tasks = repository.findScheduledProcessTimetableTasksByExecutionTimeAfter(clock.instant()) tasks.forEach { val task = ProcessTimetableTask(asyncJobLauncher, processTimetableJob, - it.timetable, repository, it) + timetableRepository, repository, it) threadPoolTaskScheduler.schedule(task, it.executionTime) } } @@ -33,13 +35,13 @@ class TimetableProcessingScheduler( fun onProcessTimetableTaskScheduled(event: ProcessTimetableTaskScheduledEvent) { val source = event.source as ScheduledProcessTimetableTask val task = ProcessTimetableTask(asyncJobLauncher, processTimetableJob, - source.timetable, repository, source) + timetableRepository, repository, source) threadPoolTaskScheduler.schedule(task, source.executionTime) } fun scheduleTimetableProcessing(timetable: Timetable) { val task = ScheduledProcessTimetableTask(timetable.userId, timetable.timetableId, - timetable, timetable.fetchDate.plusDays(2) + timetable.fetchDate.plusDays(2) .atStartOfDay(clock.zone).toInstant()) repository.save(task) val event = ProcessTimetableTaskScheduledEvent(task)