lib/take2.rb in take2-0.1.1 vs lib/take2.rb in take2-1.0.0

- old
+ new

@@ -21,15 +21,18 @@ def reset(options = {}) @configuration = Configuration.new(options) end def local_defaults(options) - configuration.validate_options(options) + configuration.validate!(options) end def configure - yield(config) if block_given? + if block_given? + yield(config) + config.validate!(config.to_hash) + end end end module InstanceMethods # Yields a block and retries on retriable errors n times. @@ -46,11 +49,11 @@ # puts "#{self.name} - Retrying.. #{tries} of #{self.retriable_configuration[:retries]} (#{error})" # } # backoff_strategy type: :exponential, start: 3 # # def give_me_food - # call_api_with_retry do + # with_retry do # # Some logic that might raise.. # # If it will raise retriable, magic happens. # # If not the original error re raised # end # end @@ -105,13 +108,13 @@ # class PizzaService # include Take2 # retriable_errors Net::HTTPRetriableError, Errno::ECONNRESET # end # Arguments: - # errors: List of retiable errors + # errors: List of retriable errors def retriable_errors(*errors) - message = 'All retriable errors must be StandardError decendants' + message = 'All retriable errors must be StandardError descendants' raise ArgumentError, message unless errors.all? { |e| e <= StandardError } self.retriable = errors end # Sets condition for retry attempt. @@ -141,19 +144,10 @@ def on_retry(proc) raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call) self.retry_proc = proc end - def sleep_before_retry(seconds) - unless (seconds.is_a?(Integer) || seconds.is_a?(Float)) && seconds.positive? - raise ArgumentError, 'Must be positive numer' - end - puts "DEPRECATION MESSAGE - The sleep_before_retry method is softly deprecated in favor of backoff_stategy \r - where the time to sleep is a starting point on the backoff intervals. Please implement it instead." - self.time_to_sleep = seconds - end - # Sets the backoff strategy # # Example: # class PizzaService # include Take2 @@ -177,26 +171,21 @@ private attr_accessor(*Take2::Configuration::CONFIG_ATTRS) def set_defaults - config = Take2.configuration.to_hash - Take2::Configuration::CONFIG_ATTRS.each do |attr| - instance_variable_set("@#{attr}", config[attr]) + Take2.config.to_hash.each do |k, v| + instance_variable_set("@#{k}", v) end end def response_status(response) return response.status if response.respond_to?(:status) response.status_code if response.respond_to?(:status_code) end def rest(config, tries) - seconds = if config[:time_to_sleep].to_f > 0 - config[:time_to_sleep].to_f - else - next_interval(config[:backoff_intervals], config[:retries], tries) - end + seconds = next_interval(config[:backoff_intervals], config[:retries], tries) sleep(seconds) end def next_interval(intervals, retries, current) intervals[retries - current]