Module: OCI::Waiter::WorkRequest

Defined in:
lib/oci/waiter.rb

Overview

Work request waiter utility method.

Class Method Summary collapse

Class Method Details

.wait_for_state(client, work_request_id, eval_success_proc = nil, eval_error_proc = nil, max_interval_seconds: 30, max_wait_seconds: 1200) ⇒ OCI::Response

Wait until the work request succeeds or fails, or max_wait_seconds is reached. The work request will be polled at an increasing rate, with a maximum of max_interval_seconds between requests.

Parameters:

  • client (Client)

    A client that is able to call get_work_request()

  • work_request_id, (String)

    the work request identifier

  • eval_success_proc (Proc) (defaults to: nil)

    the proc to evaluate if a wait condition has succeeded.

  • eval_error_proc (Proc) (defaults to: nil)

    the proc to evaluate if an error based terminal condition has occurred.

  • max_interval_seconds (Integer) (defaults to: 30)

    The maximum interval between queries, in seconds.

  • max_wait_seconds (Integer) (defaults to: 1200)

    The maximum total time to wait, in seconds.

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/oci/waiter.rb', line 33

def self.wait_for_state(client,
                        work_request_id,
                        eval_success_proc = nil,
                        eval_error_proc = nil,
                        max_interval_seconds: 30,
                        max_wait_seconds: 1200)
  raise 'Work request ID not specified.' unless work_request_id
  raise 'Evaluation proc for determining success not specified' unless eval_success_proc

  interval_seconds = 1
  start_time = Time.now

  loop do
    response = client.get_work_request(work_request_id)
    return response if eval_success_proc && eval_success_proc.call(response.data)
    return response if eval_error_proc && eval_error_proc.call(response.data)

    elapsed_seconds = (Time.now - start_time).to_i

    if elapsed_seconds + interval_seconds > max_wait_seconds
      raise OCI::Errors::MaximumWaitTimeExceededError, 'Maximum wait time has been exceeded.' \
        unless max_wait_seconds > elapsed_seconds

      # Make one last request right at the maximum wait time.
      interval_seconds = max_wait_seconds - elapsed_seconds
    end

    sleep(interval_seconds)

    interval_seconds *= 2
    interval_seconds = max_interval_seconds if interval_seconds > max_interval_seconds
  end
end