Reputation: 4658
I'm using the following code to schedule a future task on a Swift Vapor Server. The expectation is that the task executes exactly at the start of the next hour (regardless of how much time has elapsed on the current hour). This works as expected when running on my MacOS with Xcode but seems to be randomly off by 1 second (up or down) when running on my Fly.io server.
let delay = 3600 // 1 hour in seconds
var targetDate = Date(timeIntervalSinceNow: delay)
targetDate = Calendar.current.date(bySetting: .minute, value: 0, of: targetDate)!
targetDate = Calendar.current.date(bySetting: .second, value: 0, of: targetDate)!
let seconds = Int64(targetDate.timeIntervalSinceNow)
_ = app.eventLoopGroup.next().scheduleTask(deadline: NIODeadline.now() + .seconds(seconds), {
self.run() //Should run at the start of every hour.
})
Using the code above, if the current time is 14:05:23, my expectation is that the above code runs exactly at 15:00:00. However on my deployed server the code seems to randomly run at one of the five times below:
This is obviously a bug with how Vapor interprets Swift's Date and Calendar. But I'm wondering if someone has a workaround for this?
Upvotes: 0
Views: 78