lib/pallets/scheduler.rb in pallets-0.9.0 vs lib/pallets/scheduler.rb in pallets-0.10.0

- old
+ new

@@ -5,11 +5,11 @@ @needs_to_stop = false @thread = nil end def start - @thread ||= Thread.new { work } + @thread ||= Thread.new { wait_initial_bit; work } end def shutdown @needs_to_stop = true @@ -38,14 +38,20 @@ backend.reschedule_all(Time.now.to_f) wait_a_bit end end - def wait_a_bit - # Wait for roughly 10 seconds + def wait_initial_bit + # Randomly wait a bit before starting working, so that multiple processes + # will not hit the backend all at once + wait_a_bit(rand(Pallets.configuration.scheduler_polling_interval)) + end + + def wait_a_bit(seconds = Pallets.configuration.scheduler_polling_interval) + # Wait for roughly the configured number of seconds # We don't want to block the entire polling interval, since we want to # deal with shutdowns synchronously and as fast as possible - 10.times do + seconds.times do break if needs_to_stop? sleep 1 end end