Method: RobustThread.loop
- Defined in:
- lib/robustthread.rb
.loop(opts = {}, &block) ⇒ Object
Loop an activity and exit it cleanly (see README)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/robustthread.rb', line 59 def loop(opts={}, &block) sleep_seconds = opts.delete(:seconds) || 2 self.new(opts) do |*args| Kernel.loop do break if self.say_goodnight block.call(*args) # We want to sleep for the right amount of time, but we also don't # want to wait until the sleep is done if our exit handler has been # called so we iterate over a loop, sleeping only 0.1 and checking # each iteration whether we need to die, and the timeout is a noop # indicating we need to continue. begin Timeout.timeout(sleep_seconds) do Kernel.loop do break if self.say_goodnight sleep 0.1 end end rescue Timeout::Error # OK end end end end |