Sha256: 848ef3eae10f3b648d65257b9a74c9ce8d4948f0652248fd4f9ed4fad2427541

Contents?: true

Size: 1.56 KB

Versions: 9

Compression:

Stored size: 1.56 KB

Contents

require 'active_job/retry/errors'

module ActiveJob
  module Retry
    class VariableOptionsValidator
      DELAY_MULTIPLIER_KEYS = [:min_delay_multiplier, :max_delay_multiplier].freeze

      def initialize(options)
        @options = options
      end

      def validate!
        validate_banned_basic_option!(:limit)
        validate_banned_basic_option!(:delay)
        validate_delays!
        validate_delay_multipliers!
      end

      private

      attr_reader :options, :retry_limit

      def validate_banned_basic_option!(key)
        return unless options[key]

        raise InvalidConfigurationError, "Cannot use #{key} with VariableBackoffStrategy"
      end

      def validate_delays!
        return if options[:delays]

        raise InvalidConfigurationError,
              'You must define an array of delays between attempts'
      end

      def validate_delay_multipliers!
        validate_delay_multipliers_supplied_together!

        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 validate_delay_multipliers_supplied_together!
        supplied = DELAY_MULTIPLIER_KEYS.map { |key| options.key?(key) }
        return if supplied.none? || supplied.all?

        raise InvalidConfigurationError,
              'If one of min/max_delay_multiplier is supplied, both are required'
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
activejob-retry-0.5.1 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.5.0 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.4.2 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.4.1 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.4.0 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.3.1 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.3.0 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.2.0 lib/active_job/retry/variable_options_validator.rb
activejob-retry-0.1.1 lib/active_job/retry/variable_options_validator.rb