Method: HoldOn.breaker

Defined in:
lib/holdon.rb

.breaker(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/holdon.rb', line 52

def self.breaker(options = {})
  timeout  = options.fetch(:timeout, 30)
  interval = options.fetch(:interval, 1)
  message  = options[:message]

  start = Time.now
  loop do
    yield

    # If the time remaining is less than the interval,
    # then we'll only sleep for the time remaining.
    inv = [interval, (timeout - (Time.now - start))].min
    if inv > 0
      sleep inv
    else
      raise Timeout, message
    end

  end
end