class Waiting::Waiter

The class that patiently waits

Attributes

attempts[R]

The current attempt number

@return [Integer]

exp_base[RW]

The exp base

@return [Numeric]

interval[RW]

The interval

@return [Numeric]

max_attempts[RW]

The max attempts

@return [Numeric]

max_interval[RW]

The max interval

@return [Numeric]

Public Class Methods

new(exp_base: Waiting.default_exp_base, interval: Waiting.interval, max_attempts: Waiting.max_attempts, max_interval: Waiting.max_interval) click to toggle source

@param interval [Numeric] Polling interval in seconds. @param max_attempts [Numeric] Number of attempts before timing out. @param exp_base [Numeric] Increases the interval by the power of attempts. @param max_interval [Numeric] Interval limit for exponential backoff.

@yield Block to check if the wait is over. @yieldparam waiter [Waiting::Waiter] call #done if the wait is over

# File lib/waiting/waiter.rb, line 45
def initialize(exp_base: Waiting.default_exp_base,
               interval: Waiting.interval,
               max_attempts: Waiting.max_attempts,
               max_interval: Waiting.max_interval)

  @exp_base = exp_base
  @interval = interval
  @max_attempts = max_attempts
  @max_interval = max_interval

  @done = false
  @attempts = 0
end

Public Instance Methods

done() click to toggle source

Mark the waiter as done

# File lib/waiting/waiter.rb, line 61
def done
  @done = true
end
done?() click to toggle source

Is the waiter done?

@return [Boolean] if the waiter is done

# File lib/waiting/waiter.rb, line 69
def done?
  @done
end
wait() { |self| ... } click to toggle source

Waits for #done to be called

@raise [Waiting::TimedOutError] if #done is not called in time

# File lib/waiting/waiter.rb, line 77
def wait
  loop do
    if @attempts >= max_attempts
      raise Waiting::TimedOutError,
            "Timed out after #{interval * max_attempts}s"
    end

    yield(self)
    break if done?
    wait_once
  end
end

Private Instance Methods

wait_once() click to toggle source
# File lib/waiting/waiter.rb, line 92
def wait_once
  sleep [exp_base**attempts * interval, max_interval].compact.min
  @attempts += 1
end