Class: Ears::PublisherRetryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ears/publisher_retry_handler.rb

Overview

A handler for retries and connection recovery when publishing messages.

Defined Under Namespace

Classes: PublishToStaleChannelError

Constant Summary collapse

CONNECTION_ERRORS =

Connection errors that should trigger retries

[
  PublishToStaleChannelError,
  Bunny::ChannelAlreadyClosed,
  Bunny::ConnectionClosedError,
  Bunny::ConnectionForced,
  Bunny::NetworkFailure,
  Bunny::TCPConnectionFailed,
  IOError,
  Timeout::Error,
].freeze
NON_RETRYABLE_ERRORS =

Confirmation errors should NOT be retried - they indicate the message was published but confirmation failed, so retrying could duplicate the message

[PublishConfirmationTimeout, PublishNacked].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ PublisherRetryHandler

Returns a new instance of PublisherRetryHandler.



26
27
28
29
# File 'lib/ears/publisher_retry_handler.rb', line 26

def initialize(config, logger)
  @config = config
  @logger = logger
end

Instance Method Details

#run(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ears/publisher_retry_handler.rb', line 31

def run(&block)
  attempt = 1
  begin
    block.call
  rescue *NON_RETRYABLE_ERRORS => e
    raise e
  rescue *CONNECTION_ERRORS => e
    attempt = handle_connection_error(e, attempt)
    retry
  rescue StandardError => e
    attempt = handle_standard_error(e, attempt)
    retry
  end
end