Class: Timeloop
- Inherits:
-
Object
- Object
- Timeloop
- Extended by:
- Helper
- Defined in:
- lib/timeloop.rb,
lib/timeloop/version.rb
Overview
Execute a block of code periodically. The runtime of the block is taken in to account so a delay in one run does not impact the start times of future runs.
Defined Under Namespace
Modules: Helper
Constant Summary collapse
- VERSION =
'2.0.0'
Instance Method Summary collapse
-
#loop ⇒ Object
Runs provided block periodically.
-
#to_s ⇒ Object
Returns string representation of self,.
Methods included from Helper
Instance Method Details
#loop ⇒ Object
Runs provided block periodically.
Yields Integer index of the iteration to the provide block every period.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/timeloop.rb', line 11 def loop i = -1 super() do run_started_at = Time.now i += 1 logger.debug("#{to_s}: starting #{i}th run") yield(i) if block_given? break if i+1 >= max sleep til_next_start_time(Time.now - run_started_at) .tap{|s| logger.debug "#{to_s}: sleeping #{s} seconds until next run" } end end |
#to_s ⇒ Object
Returns string representation of self,
27 28 29 30 31 |
# File 'lib/timeloop.rb', line 27 def to_s ["#<Timeloop period=#{period}", max < Float::INFINITY ? ", max=#{max}" : "", ">"].join end |