Class: Ecoportal::API::V1::Job::Awaiter::Timer
- Inherits:
-
Struct
- Object
- Struct
- Ecoportal::API::V1::Job::Awaiter::Timer
- Defined in:
- lib/ecoportal/api/v1/job/awaiter/timer.rb
Overview
The class
Instance Attribute Summary collapse
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #complete? ⇒ Boolean
- #increased ⇒ Object
-
#initialize {|_self| ... } ⇒ Timer
constructor
A new instance of Timer.
- #job_id ⇒ Object
-
#last ⇒ Object
Previous timestamp.
-
#lwaited ⇒ Object
Time waited since last timestamp (relative).
-
#net_waited ⇒ Object
Absolute wait minus half of the last sleep.
- #new(**kargs, &block) ⇒ Object
- #on_timeout!(&block) ⇒ Object
- #original_last ⇒ Object
- #original_start ⇒ Object
- #pending ⇒ Object
- #progress ⇒ Object
-
#start ⇒ Object
Previous start or current timestamp.
- #start_timeout! {|_self| ... } ⇒ Object
- #start_timeout? ⇒ Boolean
- #started? ⇒ Boolean
- #time_left ⇒ Object
-
#time_left_to_start ⇒ Object
Time left before Job start times out.
-
#time_left_total ⇒ Object
Total time left before timing out.
- #timeout! {|_self| ... } ⇒ Object
-
#timeout? ⇒ Boolean
timeout library is evil.
-
#timeout_in ⇒ Object
Time left before timing out job started or job finished.
-
#waited ⇒ Object
Total time waited (absolute).
Constructor Details
#initialize {|_self| ... } ⇒ Timer
Returns a new instance of Timer.
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
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
16 17 18 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 16 def @timestamp end |
Instance Method Details
#complete? ⇒ Boolean
49 50 51 52 53 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 49 def complete? return false unless status status.complete?(total) end |
#increased ⇒ Object
69 70 71 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 69 def increased status.progress_increase(lstatus) end |
#job_id ⇒ Object
45 46 47 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 45 def job_id status.id end |
#last ⇒ Object
Previous timestamp.
34 35 36 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 34 def last original_last || start end |
#lwaited ⇒ Object
Time waited since last timestamp (relative).
79 80 81 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 79 def lwaited - last end |
#net_waited ⇒ Object
Absolute wait minus half of the last sleep.
84 85 86 87 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 84 def net_waited last_delay = ldelay || 0 waited - (last_delay / 2) end |
#new(**kargs, &block) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 38 def new(**kargs, &block) self.class.new( **new_kargs.merge(kargs), &block ) end |
#on_timeout!(&block) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 125 def on_timeout!(&block) return start_timeout!(&block) if start_timeout? return timeout!(&block) if timeout? false end |
#original_last ⇒ Object
32 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 32 alias_method :original_last, :last |
#original_start ⇒ Object
24 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 24 alias_method :original_start, :start |
#pending ⇒ Object
61 62 63 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 61 def pending status.pending(total) end |
#progress ⇒ Object
65 66 67 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 65 def progress status.progress end |
#start ⇒ Object
in practice this translates into the very first timestamp (so it's an absolute start)
Previous start or current timestamp.
28 29 30 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 28 def start original_start || end |
#start_timeout! {|_self| ... } ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 144 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
119 120 121 122 123 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 119 def start_timeout? return false if status.started? !time_left_to_start.positive? end |
#started? ⇒ Boolean
55 56 57 58 59 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 55 def started? return false unless status status.started? end |
#time_left ⇒ Object
89 90 91 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 89 def time_left (timeout_in - waited).round(2) end |
#time_left_to_start ⇒ Object
Time left before Job start times out.
107 108 109 110 111 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 107 def time_left_to_start return max_start_delay if started? (max_start_delay - waited).round(2) end |
#time_left_total ⇒ Object
Total time left before timing out.
102 103 104 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 102 def time_left_total (timeout - waited).round(2) end |
#timeout! {|_self| ... } ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 132 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
timeout library is evil. So we make poor-man timeout. https://jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/
115 116 117 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 115 def timeout? !time_left_total.positive? end |
#timeout_in ⇒ Object
Time left before timing out job started or job finished.
95 96 97 98 99 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 95 def timeout_in return time_left_total if started? time_left_to_start end |
#waited ⇒ Object
Total time waited (absolute).
74 75 76 |
# File 'lib/ecoportal/api/v1/job/awaiter/timer.rb', line 74 def waited - start end |