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

Inherits:
Struct
  • Object
show all
Defined in:
lib/ecoportal/api/v1/job/awaiter/timer.rb

Overview

The class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Timer

Returns a new instance of Timer.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(...)
  @timestamp = Time.now
  super
  yield(self) if block_given?
end

Instance Attribute Details

#timestampObject (readonly)

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 42

def complete?
  status.complete?(total)
end

#increasedObject



58
59
60
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 58

def increased
  status.progress_increase(lstatus)
end

#job_idObject



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

def job_id
  status.id
end

#lastObject



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

def last
  original_last || start
end

#lwaitedObject



66
67
68
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 66

def lwaited
  timestamp - last
end

#net_waitedObject



70
71
72
73
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 70

def net_waited
  last_delay = ldelay || 0
  waited - (last_delay / 2)
end

#new(**kargs, &block) ⇒ Object



34
35
36
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 34

def new(**kargs, &block)
  self.class.new(**new_kargs.merge(kargs), &block)
end

#on_timeout!(&block) ⇒ Object



107
108
109
110
111
112
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 107

def on_timeout!(&block)
  return start_timeout!(&block) if start_timeout?
  return timeout!(&block)       if timeout?

  false
end

#original_lastObject



29
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 29

alias_method :original_last, :last

#original_startObject



24
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 24

alias_method :original_start, :start

#pendingObject



50
51
52
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 50

def pending
  status.pending(total)
end

#progressObject



54
55
56
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 54

def progress
  status.progress
end

#startObject



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

def start
  original_start || timestamp
end

#start_timeout! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 126

def start_timeout!
  return false unless start_timeout?

  yield(self)

  msg  = "Job '#{job_id}' not started (size: #{total}).\n"
  msg << "  Start timed out after #{max_start_delay} seconds.\n"
  msg << "  Current status: #{status}"

  raise API::Errors::StartTimeOut, msg
end

#start_timeout?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 101

def start_timeout?
  return false if status.started?

  !time_left_to_start.positive?
end

#started?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 46

def started?
  status.started?
end

#time_leftObject



75
76
77
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 75

def time_left
  (timeout_in - waited).round(2)
end

#time_left_to_startObject



83
84
85
86
87
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 83

def time_left_to_start
  return max_start_delay if started?

  (max_start_delay - waited).round(2)
end

#time_left_totalObject



79
80
81
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 79

def time_left_total
  (timeout - waited).round(2)
end

#timeout! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 114

def timeout!
  return false unless timeout?

  yield(self)

  msg  = "Job '#{job_id}' not complete (size: #{total}).\n"
  msg << "  Timed out after #{timeout} seconds.\n"
  msg << "  Current status: #{status}"

  raise API::Errors::TimeOut, msg
end

#timeout?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 97

def timeout?
  !time_left_total.positive?
end

#timeout_inObject



89
90
91
92
93
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 89

def timeout_in
  return time_left_total if started?

  time_left_to_start
end

#waitedObject



62
63
64
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 62

def waited
  timestamp - start
end