Class: SdrClient::RedesignedClient::JobStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/sdr_client/redesigned_client/job_status.rb

Overview

Wraps operations waiting for results from jobs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id:) ⇒ JobStatus

Returns a new instance of JobStatus.



11
12
13
14
15
16
17
18
19
20
# File 'lib/sdr_client/redesigned_client/job_status.rb', line 11

def initialize(job_id:)
  @job_id = job_id
  @result = {
    status: 'not started',
    output: {
      errors: nil,
      druid: ''
    }
  }
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/sdr_client/redesigned_client/job_status.rb', line 9

def result
  @result
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/sdr_client/redesigned_client/job_status.rb', line 22

def complete?
  @result = client.get(path: path)
  @result[:status] == 'complete'
end

#druidObject



27
28
29
# File 'lib/sdr_client/redesigned_client/job_status.rb', line 27

def druid
  @result[:output][:druid]
end

#errorsObject



31
32
33
# File 'lib/sdr_client/redesigned_client/job_status.rb', line 31

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

#wait_until_complete(secs_between_requests: 3.0, timeout_in_secs: 180, backoff_factor: 2.0, max_secs_between_requests: 60) ⇒ Boolean

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

Parameters:

  • secs_between_requests (Float) (defaults to: 3.0)

    (3.0) initially, how many secs between polling requests

  • timeout_in_secs (Integer) (defaults to: 180)

    (180) timeout after this many secs

  • 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:

  • (Boolean)

    true if successful false if unsuccessful.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sdr_client/redesigned_client/job_status.rb', line 40

def wait_until_complete(secs_between_requests: 3.0, # rubocop:disable Metrics/MethodLength
                        timeout_in_secs: 180,
                        backoff_factor: 2.0,
                        max_secs_between_requests: 60)
  begin
    Timeout.timeout(timeout_in_secs) do
      loop do
        break if complete?

        yield if block_given?

        sleep(secs_between_requests)
        # Exponential backoff, limited to max_secs_between_requests
        secs_between_requests = [secs_between_requests * backoff_factor, max_secs_between_requests].min
      end
    end
  rescue Timeout::Error
    @result[:output][:errors] = ["Not complete after #{timeout_in_secs} seconds"]
  end

  errors.nil?
end