From 5070ec104518d99a3c942e6fb43affd1fbb2cdfe Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 29 Aug 2024 08:56:20 +0100 Subject: [PATCH] Only read jobs metadata once service is up and running --- Sources/Jobs/Scheduler/JobSchedule.swift | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Sources/Jobs/Scheduler/JobSchedule.swift b/Sources/Jobs/Scheduler/JobSchedule.swift index 6337b1e..6b3ed1e 100644 --- a/Sources/Jobs/Scheduler/JobSchedule.swift +++ b/Sources/Jobs/Scheduler/JobSchedule.swift @@ -160,7 +160,13 @@ public struct JobSchedule: MutableCollection, Sendable { init(jobQueue: JobQueue, jobSchedule: JobSchedule) async { self.jobQueue = jobQueue - var jobSchedule = jobSchedule + self.jobSchedule = jobSchedule + } + + /// Run Job scheduler + public func run() async throws { + try await self.jobQueue.initializationComplete.waitUntilTriggered() + var jobSchedule = self.jobSchedule // Update next scheduled date for each job schedule based off the last scheduled date stored do { if let date = try await self.jobQueue.getMetadata(.jobScheduleLastDate) { @@ -171,15 +177,8 @@ public struct JobSchedule: MutableCollection, Sendable { } catch { self.jobQueue.logger.error("Failed to get last scheduled job date.") } - - self.jobSchedule = jobSchedule - } - - /// Run Job scheduler - public func run() async throws { - try await self.jobQueue.initializationComplete.waitUntilTriggered() let scheduledJobSequence = JobSequence( - jobSchedule: self.jobSchedule, + jobSchedule: jobSchedule, logger: self.jobQueue.logger ) for await job in scheduledJobSequence.cancelOnGracefulShutdown() {