Module: BlockGiven::Poller
- Defined in:
- lib/block_given/poller.rb
Overview
Blocking polling helper.
receipt = BlockGiven::Poller.poll(interval: 2, timeout: 120) { client.get_transaction_receipt(hash) }
The block is called until it returns a non-nil / non-false value, which is returned. Raises BlockGiven::TimeoutError when the timeout elapses.
Class Method Summary collapse
Class Method Details
.monotonic_now ⇒ Object
31 |
# File 'lib/block_given/poller.rb', line 31 def monotonic_now = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
.poll(interval:, timeout: nil, description: "condition") ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/block_given/poller.rb', line 15 def poll(interval:, timeout: nil, description: "condition") started = monotonic_now attempt = 0 loop do result = yield(attempt) return result if result attempt += 1 elapsed = monotonic_now - started raise TimeoutError, "timed out after #{timeout}s waiting for #{description}" if timeout && elapsed >= timeout remaining = timeout ? timeout - elapsed : interval sleep([interval, remaining].min) end end |