Class: SneakersHandlers::ExponentialBackoffHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sneakers_handlers/exponential_backoff_handler.rb

Constant Summary collapse

DEFAULT_MAX_RETRY_ATTEMPTS =
25
DEFAULT_BACKOFF_FUNCTION =
-> (attempt_number) { (attempt_number + 1) ** 2 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, queue, options) ⇒ ExponentialBackoffHandler

Returns a new instance of ExponentialBackoffHandler.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 31

def initialize(channel, queue, options)
  @queue = queue
  @channel = channel
  @options = options
  @max_retries = options[:max_retries] || DEFAULT_MAX_RETRY_ATTEMPTS
  @backoff_function  = options[:backoff_function] || DEFAULT_BACKOFF_FUNCTION

  create_error_exchange!

  queue.bind(primary_exchange, routing_key: queue.name)
end

Instance Attribute Details

#backoff_functionObject (readonly)

Returns the value of attribute backoff_function.



26
27
28
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 26

def backoff_function
  @backoff_function
end

#channelObject (readonly)

Returns the value of attribute channel.



26
27
28
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 26

def channel
  @channel
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



26
27
28
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 26

def max_retries
  @max_retries
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 26

def options
  @options
end

#queueObject (readonly)

Returns the value of attribute queue.



26
27
28
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 26

def queue
  @queue
end

Instance Method Details

#acknowledge(delivery_info, _, _) ⇒ Object



43
44
45
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 43

def acknowledge(delivery_info, _, _)
  channel.acknowledge(delivery_info.delivery_tag, false)
end

#error(delivery_info, properties, message, err) ⇒ Object



51
52
53
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 51

def error(delivery_info, properties, message, err)
  retry_message(delivery_info, properties, message, err.inspect)
end

#noop(_delivery_info, _properties, _message) ⇒ Object



59
60
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 59

def noop(_delivery_info, _properties, _message)
end

#reject(delivery_info, properties, message, _requeue = true) ⇒ Object



47
48
49
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 47

def reject(delivery_info, properties, message, _requeue = true)
  retry_message(delivery_info, properties, message, :reject)
end

#timeout(delivery_info, properties, message) ⇒ Object



55
56
57
# File 'lib/sneakers_handlers/exponential_backoff_handler.rb', line 55

def timeout(delivery_info, properties, message)
  retry_message(delivery_info, properties, message, :timeout)
end