Class: Waiting::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/waiting/waiter.rb

Overview

The class that patiently waits

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp_base: Waiting.default_exp_base, interval: Waiting.interval, max_attempts: Waiting.max_attempts, max_interval: Waiting.max_interval) {|waiter| ... } ⇒ Waiter

Returns a new instance of Waiter.

Parameters:

  • interval (Numeric) (defaults to: Waiting.interval)

    Polling interval in seconds.

  • max_attempts (Numeric) (defaults to: Waiting.max_attempts)

    Number of attempts before timing out.

  • exp_base (Numeric) (defaults to: Waiting.default_exp_base)

    Increases the interval by the power of attempts.

  • max_interval (Numeric) (defaults to: Waiting.max_interval)

    Interval limit for exponential backoff.

Yields:

  • Block to check if the wait is over.

Yield Parameters:



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#attemptsInteger (readonly)

The current attempt number

Returns:

  • (Integer)


11
12
13
# File 'lib/waiting/waiter.rb', line 11

def attempts
  @attempts
end

#exp_baseNumeric

The exp base

Returns:

  • (Numeric)


17
18
19
# File 'lib/waiting/waiter.rb', line 17

def exp_base
  @exp_base
end

#intervalNumeric

The interval

Returns:

  • (Numeric)


23
24
25
# File 'lib/waiting/waiter.rb', line 23

def interval
  @interval
end

#max_attemptsNumeric

The max attempts

Returns:

  • (Numeric)


29
30
31
# File 'lib/waiting/waiter.rb', line 29

def max_attempts
  @max_attempts
end

#max_intervalNumeric

The max interval

Returns:

  • (Numeric)


35
36
37
# File 'lib/waiting/waiter.rb', line 35

def max_interval
  @max_interval
end

Instance Method Details

#doneObject

Mark the waiter as done



61
62
63
# File 'lib/waiting/waiter.rb', line 61

def done
  @done = true
end

#done?Boolean

Is the waiter done?

Returns:

  • (Boolean)

    if the waiter is done



69
70
71
# File 'lib/waiting/waiter.rb', line 69

def done?
  @done
end

#waitObject

Waits for #done to be called

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
# 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