Class: Lambda::MicroVMs::Waiter

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

Overview

Polls a resource until a desired lifecycle state is reached.

Constant Summary collapse

DEFAULT_DELAY =

Default polling delay, in seconds.

1.0
DEFAULT_TIMEOUT =

Default maximum wait time, in seconds.

60.0

Instance Method Summary collapse

Constructor Details

#initialize(delay: DEFAULT_DELAY, timeout: DEFAULT_TIMEOUT, sleeper: Kernel) ⇒ Waiter

Returns a new instance of Waiter.



12
13
14
15
16
# File 'lib/lambda/microvms/waiter.rb', line 12

def initialize(delay: DEFAULT_DELAY, timeout: DEFAULT_TIMEOUT, sleeper: Kernel)
  @delay = delay
  @timeout = timeout
  @sleeper = sleeper
end

Instance Method Details

#wait(message: 'condition') ⇒ Object

Poll until the block returns a truthy value or the timeout expires.

Parameters:

  • message (String) (defaults to: 'condition')

    human-readable condition for timeout errors

Yield Returns:

  • (Object, false, nil)

    truthy value when the condition is satisfied

Returns:

  • (Object)

    the truthy block result

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lambda/microvms/waiter.rb', line 24

def wait(message: 'condition')
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout

  loop do
    value = yield
    return value if value

    now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    break if now >= deadline

    @sleeper.sleep([@delay, deadline - now].min)
  end

  raise WaitTimeoutError, "timed out waiting for #{message}"
end