Sha256: 1d3a8a17693dbf2f5a755d537cb6adf65ff646df97adbf3e880a8d61c5ca9a3d

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module EventSourcery
  module EventProcessing
    module ErrorHandlers
      class ExponentialBackoffRetry
        include EventSourcery::EventProcessing::ErrorHandlers::ErrorHandler

        # The starting value for the retry interval used with {with_error_handling}.
        #
        # @api private
        DEFAULT_RETRY_INTERVAL = 1

        # The maximum retry interval value to be used with {with_error_handling}.
        #
        # @api private
        MAX_RETRY_INTERVAL = 64

        def initialize(processor_name:)
          @processor_name = processor_name
          @retry_interval = DEFAULT_RETRY_INTERVAL
        end

        # Will yield the block and attempt to retry in an exponential backoff.
        def with_error_handling
          yield
        rescue => error
          report_error(error)
          update_retry_interval(error)
          sleep(@retry_interval)
          retry
        end

        private

        def update_retry_interval(error)
          if error.instance_of?(EventSourcery::EventProcessingError)
            if @error_event_uuid == error.event.uuid
              @retry_interval *= 2 if @retry_interval < MAX_RETRY_INTERVAL
            else
              @error_event_uuid = error.event.uuid
              @retry_interval = DEFAULT_RETRY_INTERVAL
            end
          else
            @retry_interval = DEFAULT_RETRY_INTERVAL
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
event_sourcery-0.15.0 lib/event_sourcery/event_processing/error_handlers/exponential_backoff_retry.rb