Class: Racecar::Pause

Inherits:
Object
  • Object
show all
Defined in:
lib/racecar/pause.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: nil, max_timeout: nil, exponential_backoff: false) ⇒ Pause

Returns a new instance of Pause.



7
8
9
10
11
12
13
# File 'lib/racecar/pause.rb', line 7

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_countObject (readonly)

Returns the value of attribute pauses_count.



5
6
7
# File 'lib/racecar/pause.rb', line 5

def pauses_count
  @pauses_count
end

Instance Method Details

#backoff_intervalObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/racecar/pause.rb', line 48

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

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/racecar/pause.rb', line 38

def expired?
  return false if @timeout.nil?
  return true unless @ends_at
  Time.now >= @ends_at
end

#pause!Object



15
16
17
18
19
# File 'lib/racecar/pause.rb', line 15

def pause!
  @started_at = Time.now
  @ends_at = @started_at + backoff_interval unless @timeout.nil?
  @pauses_count += 1
end

#pause_durationObject



30
31
32
33
34
35
36
# File 'lib/racecar/pause.rb', line 30

def pause_duration
  if paused?
    Time.now - @started_at
  else
    0
  end
end

#paused?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/racecar/pause.rb', line 26

def paused?
  !@started_at.nil?
end

#reset!Object



44
45
46
# File 'lib/racecar/pause.rb', line 44

def reset!
  @pauses_count = 0
end

#resume!Object



21
22
23
24
# File 'lib/racecar/pause.rb', line 21

def resume!
  @started_at = nil
  @ends_at = nil
end