Class: Resqued::Backoff

Inherits:
Object
  • Object
show all
Defined in:
lib/resqued/backoff.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Backoff

Returns a new instance of Backoff.



3
4
5
6
7
8
# File 'lib/resqued/backoff.rb', line 3

def initialize(options = {})
  @time = options.fetch(:time) { Time }
  @min  = options.fetch(:min) { 1.0 }
  @max  = options.fetch(:max) { 16.0 }
  @backoff_duration = @min
end

Instance Method Details

#diedObject

Public: Tell backoff that the thing unexpectedly died.



18
19
20
21
# File 'lib/resqued/backoff.rb', line 18

def died
  @backoff_duration = @backoff_duration ? [@backoff_duration * 2.0, @max].min : @min
  @last_event = :died
end

#how_long?Boolean

Public: How much longer until ‘wait?` will be false?

Returns:

  • (Boolean)


29
30
31
# File 'lib/resqued/backoff.rb', line 29

def how_long?
  wait? ? next_start_at - now : nil
end

#startedObject

Public: Tell backoff that the thing we might want to back off from just started.



11
12
13
14
15
# File 'lib/resqued/backoff.rb', line 11

def started
  @last_started_at = now
  @backoff_duration = @min if @last_event == :start
  @last_event = :start
end

#wait?Boolean

Public: Check if we should wait before starting again.

Returns:

  • (Boolean)


24
25
26
# File 'lib/resqued/backoff.rb', line 24

def wait?
  @last_started_at && next_start_at > now
end