From 6ca28ae01d6b9d73cef662309a863872c14526bf Mon Sep 17 00:00:00 2001 From: Andris Briedis Date: Wed, 10 Jul 2019 11:43:04 +0300 Subject: [PATCH] Fixed race condition when starting jobs Calling Time.zone.now in the if statement and while calculating sleep delay sometimes resulted in an exception with message "time interval must be positive". This happened if job was about to start and we needed to sleep for a really short period of time + server was under high load. First the application was checking if difference between current time and scheduled run time was positive. If the expression returned true, sleep was called, but required delay was calculated once again and could result in a negative value being passed to the sleep function. --- lib/crono/cli.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/crono/cli.rb b/lib/crono/cli.rb index bbec31e..4867e63 100644 --- a/lib/crono/cli.rb +++ b/lib/crono/cli.rb @@ -120,7 +120,8 @@ module Crono def start_working_loop loop do next_time, jobs = Crono.scheduler.next_jobs - sleep(next_time - Time.zone.now) if next_time > Time.zone.now + now = Time.zone.now + sleep(next_time - now) if next_time > now jobs.each(&:perform) end end