Class: Ecoportal::API::V1::Job::Awaiter

Inherits:
Object
  • Object
show all
Includes:
Common::Client::TimeOut, StatusFrequency
Defined in:
lib/ecoportal/api/v1/job/awaiter.rb,
lib/ecoportal/api/v1/job/awaiter/timer.rb,
lib/ecoportal/api/v1/job/awaiter/status_frequency.rb

Defined Under Namespace

Modules: StatusFrequency Classes: Timer

Constant Summary collapse

TIMEOUT_APPROACH =

:conservative # adaptative timeout

:min
TIMEOUT_FALLBACK =
:min
TIMER_ARGS =
%i[
  total timeout
  start last ldelay
  status lstatus
].freeze

Constants included from StatusFrequency

StatusFrequency::DELAY_STATUS_CHECK, StatusFrequency::MIN_STATUS_CHECK

Constants included from Common::Client::TimeOut

Common::Client::TimeOut::MIN_SIZE, Common::Client::TimeOut::TIMEOUT_MARGIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_api, job_id:, total:) ⇒ Awaiter

Returns a new instance of Awaiter.



17
18
19
20
21
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 17

def initialize(job_api, job_id:, total:)
  @job_api = job_api
  @job_id  = job_id
  @total   = total
end

Instance Attribute Details

#job_apiObject (readonly)

Returns the value of attribute job_api.



15
16
17
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 15

def job_api
  @job_api
end

#job_idObject (readonly)

Returns the value of attribute job_id.



15
16
17
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 15

def job_id
  @job_id
end

#timeout_approachObject



25
26
27
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 25

def timeout_approach
  @timeout_approach ||= self.class::TIMEOUT_APPROACH
end

#totalObject (readonly)

Returns the value of attribute total.



15
16
17
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 15

def total
  @total
end

Instance Method Details

#await_completion!Object

rubocop:disable Metrics/AbcSize



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 37

def await_completion! # rubocop:disable Metrics/AbcSize
  timeout = timeout_for(total, approach: timeout_approach)

  first = 1
  timer = Timer.new(
    total:   total,
    timeout: timeout,
    ldelay:  first
  )

  delay_status_check = nil

  loop do
    sleep(first.tap {first = nil}) if first

    timer = timer.new(
      status: job_api.status(job_id),
      ldelay: delay_status_check
    )

    # ratio = throughput!(timer.net_waited, count: timer.progress)
    ratio = throughput!(timer.lwaited, count: timer.increased)

    break timer.status if timer.complete?

    timer.on_timeout! do
      @timeout_approach = self.class::TIMEOUT_FALLBACK
    end

    delay_status_check = status_check_in(
      timer.pending,
      timeout_in: timer.timeout_in
    )

    msg  = " ... Awaiting #{delay_status_check} s. -- "
    msg << " TimeOut: #{timer.time_left} s. "
    msg << "(job '#{job_id}') "
    msg << "Done: #{timer.progress} (est. #{ratio.round(2)} rec/s) "
    msg << "     \r"

    print msg
    $stdout.flush

    sleep(delay_status_check)
  end
end

#new(**kargs) ⇒ Object

Allows to preserve the learned throughput



30
31
32
33
34
35
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 30

def new(**kargs)
  self.class.new(job_api, **kargs).tap do |out|
    out.throughput       = throughput
    out.timeout_approach = timeout_approach
  end
end