Module: Tenable::Pollable

Included in:
Resources::Base
Defined in:
lib/tenable/pollable.rb

Overview

Shared polling behavior for resources that need to wait on async operations.

Uses monotonic clock to avoid issues with system clock changes.

Instance Method Summary collapse

Instance Method Details

#poll_until(timeout:, poll_interval:, label:) { ... } ⇒ Object

Polls a block until it returns a truthy value or the timeout expires.

Parameters:

  • timeout (Integer)

    maximum seconds to wait

  • poll_interval (Integer)

    seconds between polls

  • label (String)

    descriptive label for timeout error messages

Yields:

  • block that returns a truthy value when the operation is complete

Yield Returns:

  • (Object, nil)

    truthy to stop polling, nil/false to continue

Returns:

  • (Object)

    the truthy value returned by the block

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tenable/pollable.rb', line 17

def poll_until(timeout:, poll_interval:, label:)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  loop do
    if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
      raise Tenable::TimeoutError, "#{label} timed out after #{timeout}s"
    end

    result = yield
    return result if result

    sleep(poll_interval)
  end
end