Class: Dor::Services::Client::AsyncResult

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/client/async_result.rb

Overview

A helper for monitoring asynchonous jobs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:) ⇒ AsyncResult

Returns a new instance of AsyncResult.

Parameters:

  • url (String)

    the url of the background result



13
14
15
# File 'lib/dor/services/client/async_result.rb', line 13

def initialize(url:)
  @url = url
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/dor/services/client/async_result.rb', line 10

def url
  @url
end

Instance Method Details

#complete?Boolean

Checks to see if the result is complete.

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/dor/services/client/async_result.rb', line 31

def complete?
  @results = Dor::Services::Client.background_job_results.show(job_id: job_id_from(url: url))
  @results[:status] == 'complete'
end

#errorsObject



36
37
38
# File 'lib/dor/services/client/async_result.rb', line 36

def errors
  @results[:output][:errors]
end

#wait_until_complete(seconds_between_requests: 3.0, timeout_in_seconds: 180, backoff_factor: 2.0, max_seconds_between_requests: 60) ⇒ Object

Polls using exponential backoff, so as not to overrwhelm the server.

Parameters:

  • seconds_between_requests (Float) (defaults to: 3.0)

    (3.0) initially, how many seconds between polling requests

  • timeout_in_seconds (Integer) (defaults to: 180)

    (180) timeout after this many seconds

  • backoff_factor (Float) (defaults to: 2.0)

    (2.0) how quickly to backoff. This should be > 1.0 and probably ought to be <= 2.0

Returns:

  • true if successful false if unsuccessful.



22
23
24
25
26
27
28
# File 'lib/dor/services/client/async_result.rb', line 22

def wait_until_complete(seconds_between_requests: 3.0,
                        timeout_in_seconds: 180,
                        backoff_factor: 2.0,
                        max_seconds_between_requests: 60)
  poll_until_complete(seconds_between_requests, timeout_in_seconds, backoff_factor, max_seconds_between_requests)
  errors.nil?
end