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 =

available approaches are:

  • :min assumes always the MIN_THROUGHPUT.
  • :last last seen throughput.
  • :conservative the bigger of: min throughput seen or MIN_THROUGHPUT.
  • :optimistic the smoller of: max throughput seen or MAX_THROUGHPUT.
  • :average the accumulated avarage of througoutputs.
:min
TIMEOUT_FALLBACK =

on timeout swap to this approach.

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



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

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.



21
22
23
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 21

def job_api
  @job_api
end

#job_idObject (readonly)

Returns the value of attribute job_id.



21
22
23
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 21

def job_id
  @job_id
end

#timeout_approachObject



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

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

#totalObject (readonly)

Returns the value of attribute total.



21
22
23
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 21

def total
  @total
end

Instance Method Details

#await_completion!Object

rubocop:disable Metrics/AbcSize



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
83
84
85
86
87
88
89
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 43

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 || 1
    )

    # ratio = throughput!(timer.net_waited, count: timer.progress)
    # last throughput (rather than average as above)
    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



36
37
38
39
40
41
# File 'lib/ecoportal/api/v1/job/awaiter.rb', line 36

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