Sha256: bbcabdab4a16865633481313310345090b1a58e39040b87e8ebad24848801001

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module ActiveJob
  module Retry
    class ExponentialOptionsValidator
      DELAY_MULTIPLIER_KEYS = %i(min_delay_multiplier max_delay_multiplier).freeze

      def initialize(options)
        @options = options
        @retry_limit = options[:limit] || options.fetch(:strategy, []).length
      end

      def validate!
        validate_strategy!
        validate_delay_multipliers!
      end

      private

      attr_reader :options, :retry_limit

      def validate_strategy!
        unless options[:strategy]
          raise InvalidConfigurationError, "You must define a backoff strategy"
        end

        return unless retry_limit

        unless retry_limit > 0
          raise InvalidConfigurationError,
                "Exponential backoff cannot be used with infinite or no retries"
        end

        return if options[:strategy].length == retry_limit

        raise InvalidConfigurationError, "Strategy must have a delay for each retry"
      end

      def validate_delay_multipliers!
        unless both_or_neither_multiplier_supplied?
          raise InvalidConfigurationError,
                "If one of min/max_delay_multiplier is supplied, both are required"
        end

        return unless options[:min_delay_multiplier] && options[:max_delay_multiplier]

        return if options[:min_delay_multiplier] <= options[:max_delay_multiplier]

        raise InvalidConfigurationError,
              "min_delay_multiplier must be less than or equal to max_delay_multiplier"
      end

      def both_or_neither_multiplier_supplied?
        supplied = DELAY_MULTIPLIER_KEYS.map { |key| options.key?(key) }
        supplied.none? || supplied.all?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activejob-retry-0.0.1 lib/active_job/retry/exponential_options_validator.rb