Class: Racecar::Pause
- Inherits:
-
Object
- Object
- Racecar::Pause
- Defined in:
- lib/racecar/pause.rb
Instance Attribute Summary collapse
-
#pauses_count ⇒ Object
readonly
Returns the value of attribute pauses_count.
Class Method Summary collapse
Instance Method Summary collapse
- #backoff_interval ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(timeout: nil, max_timeout: nil, exponential_backoff: false) ⇒ Pause
constructor
A new instance of Pause.
- #pause! ⇒ Object
- #pause_duration ⇒ Object
- #paused? ⇒ Boolean
- #reset! ⇒ Object
- #resume! ⇒ Object
Constructor Details
#initialize(timeout: nil, max_timeout: nil, exponential_backoff: false) ⇒ Pause
Returns a new instance of Pause.
23 24 25 26 27 28 29 |
# File 'lib/racecar/pause.rb', line 23 def initialize(timeout: nil, max_timeout: nil, exponential_backoff: false) @started_at = nil @pauses_count = 0 @timeout = timeout @max_timeout = max_timeout @exponential_backoff = exponential_backoff end |
Instance Attribute Details
#pauses_count ⇒ Object (readonly)
Returns the value of attribute pauses_count.
5 6 7 |
# File 'lib/racecar/pause.rb', line 5 def pauses_count @pauses_count end |
Class Method Details
.new_from_config(config) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/racecar/pause.rb', line 7 def self.new_from_config(config) timeout = if config.pause_timeout == -1 || config.pause_timeout == 0 nil elsif config.pause_timeout > 0 config.pause_timeout else raise ArgumentError, "Invalid value for pause_timeout: must be integer greater or equal -1" end new( timeout: timeout, max_timeout: config.max_pause_timeout, exponential_backoff: config.pause_with_exponential_backoff ) end |
Instance Method Details
#backoff_interval ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/racecar/pause.rb', line 64 def backoff_interval return Float::INFINITY if @timeout.nil? backoff_factor = @exponential_backoff ? 2**@pauses_count : 1 timeout = backoff_factor * @timeout timeout = @max_timeout if @max_timeout && timeout > @max_timeout timeout end |
#expired? ⇒ Boolean
54 55 56 57 58 |
# File 'lib/racecar/pause.rb', line 54 def expired? return false if @timeout.nil? return true unless @ends_at Time.now >= @ends_at end |
#pause! ⇒ Object
31 32 33 34 35 |
# File 'lib/racecar/pause.rb', line 31 def pause! @started_at = Time.now @ends_at = @started_at + backoff_interval unless @timeout.nil? @pauses_count += 1 end |
#pause_duration ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/racecar/pause.rb', line 46 def pause_duration if paused? Time.now - @started_at else 0 end end |
#paused? ⇒ Boolean
42 43 44 |
# File 'lib/racecar/pause.rb', line 42 def paused? !@started_at.nil? end |
#reset! ⇒ Object
60 61 62 |
# File 'lib/racecar/pause.rb', line 60 def reset! @pauses_count = 0 end |
#resume! ⇒ Object
37 38 39 40 |
# File 'lib/racecar/pause.rb', line 37 def resume! @started_at = nil @ends_at = nil end |