Skip to content

Commit

Permalink
patch: fix build error && deletion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenSrcerer committed Apr 7, 2024
1 parent cc85b72 commit f1bb9fa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.quarkus.scheduler.Scheduled
import jakarta.enterprise.context.ApplicationScoped
import jakarta.enterprise.event.Observes
import online.danielstefani.paddy.schedule.ScheduleRepository
import reactor.core.publisher.Mono
import java.util.concurrent.TimeUnit

@ApplicationScoped
Expand Down Expand Up @@ -35,7 +36,7 @@ class CronController(
// Load schedules at startup
fun loadSchedules(@Observes event: StartupEvent) {
val schedules = scheduleRepository.getAll()
.onEach { cronService.reloadSchedule(it.id!!) }
.onEach { cronService.reloadSchedule(it) }
.size

Log.info("[cron] Loaded <$schedules> schedule(s) to execute.")
Expand All @@ -46,6 +47,8 @@ class CronController(
val scheduleId = mqtt5Publish.topic.levels.last().toLong()
Log.debug("[cron] Received an update for schedule <$scheduleId>.")

cronService.reloadSchedule(scheduleId)
Mono.fromCallable { scheduleRepository.get(scheduleId) }
.map { cronService.reloadSchedule(it) }
.subscribe()
}
}
12 changes: 6 additions & 6 deletions src/main/kotlin/online/danielstefani/paddy/cron/CronService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class CronService(
.log()
}

/*
Effectively removes the schedule only if it is null.
*/
fun reloadSchedule(schedule: Schedule?) {
if (schedule == null) {
Log.error("[cron->service] Could not reload schedule, it was null after updater!")
return
}
scheduledCrons.removeIf { it.id == schedule?.id }

scheduledCrons.removeIf { it.id == schedule.id }
scheduledCrons.add(schedule)
if (schedule != null)
scheduledCrons.add(schedule)
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ open class Schedule {

val isReady = nextExec < timeNowAdjusted

Log.trace("""[cron->service] Schedule <${id!!}> status:
Log.info("""[cron->service] Schedule <${id!!}> status:
[Single]: ${isSingle()}
[Ready]: $isReady
[Cron]: ${periodic ?: "N/A"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ScheduleRepository(
fun get(id: Long): Schedule? {
return with(factory.openSession()) {
this.load(Schedule::class.java, id, SCHEDULE_LOAD_DEPTH)
}
}?.also { it.load() }
}

fun getAll(): Collection<Schedule> {
Expand Down

0 comments on commit f1bb9fa

Please sign in to comment.