Skip to content

Instantly share code, notes, and snippets.

@movstox
Created November 19, 2019 02:05
Show Gist options
  • Save movstox/23f7341d06aca8063862f962f7330654 to your computer and use it in GitHub Desktop.
Save movstox/23f7341d06aca8063862f962f7330654 to your computer and use it in GitHub Desktop.
Metronom: evoke smth on
def with_scheduled_ticks(collection, start_in: 0, interval: 1, &block)
return unless block_given?
collection.each.with_index do |el, idx|
yield(el, start_in + idx*interval)
end
end
jobs = %w[job1 job2 job3]
# with_scheduled_ticks(collection, start_in: 10, interval: 1)
# with_scheduled_ticks(collection) do |el, seconds_from_now|
# puts "[#{el.inspect}] -- Tick in: #{seconds_from_now} secs"
# end
with_scheduled_ticks(jobs, start_in: 10, interval: 1) do |el, seconds_from_now|
puts "[#{el.inspect}] -- Tick in: #{seconds_from_now} secs"
end
# ["job1"] -- Tick in: 10 secs
# ["job2"] -- Tick in: 11 secs
# ["job3"] -- Tick in: 12 secs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment