From 6aeb0a179567b944ecf81e1bbfdd474c1ee171f1 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 21 Dec 2024 07:48:59 +0000 Subject: [PATCH] Latest scheduling should update based off the latest of nextScheduledData or now (#44) --- Sources/Jobs/Scheduler/JobSchedule.swift | 18 +++++--- Tests/JobsTests/JobSchedulerTests.swift | 57 +++++++++++++++++++++--- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Sources/Jobs/Scheduler/JobSchedule.swift b/Sources/Jobs/Scheduler/JobSchedule.swift index bf25859..12c2995 100644 --- a/Sources/Jobs/Scheduler/JobSchedule.swift +++ b/Sources/Jobs/Scheduler/JobSchedule.swift @@ -101,9 +101,9 @@ public struct JobSchedule: MutableCollection, Sendable { mutating func updateNextScheduledDate(jobIndex: Int) { let dateFrom: Date = switch self.self[jobIndex].accuracy { - case .latest: .now + case .latest: Swift.max(.now, self[jobIndex].nextScheduledDate) case .all: self[jobIndex].nextScheduledDate - default: .now + default: Swift.max(.now, self[jobIndex].nextScheduledDate) } if let nextScheduledDate = self[jobIndex].schedule.nextDate(after: dateFrom) { self[jobIndex].nextScheduledDate = nextScheduledDate @@ -112,6 +112,12 @@ public struct JobSchedule: MutableCollection, Sendable { } } + mutating func setInitialNextDate(after date: Date) { + for index in 0.. 0 { + try await Task.sleep(until: .now + .seconds(timeInterval)) + } self.jobSchedule.updateNextScheduledDate(jobIndex: job.offset) return (job.element.jobParameters, nextScheduledDate) } catch { @@ -179,9 +187,7 @@ public struct JobSchedule: MutableCollection, Sendable { date = .now } self.jobQueue.logger.debug("Last scheduled date \(date).") - for index in 0.. .now) + } + func testJobScheduleSequence() async throws { struct Job1: JobParameters { static let jobName = "Job1"