Sha256: 35d12381ae5c8005637401f61283aada29126a708efa744cf423480b53d1e6a7
Contents?: true
Size: 1.12 KB
Versions: 1
Compression:
Stored size: 1.12 KB
Contents
# frozen_string_literal: true require 'tlopo/retry/version' require 'logger' require 'timeout' # Simple module so we have a namespace module Tlopo LOGGER ||= Logger.new $stderr module Setters def make_setter(*names) names.each do |name| define_method(name) do |val| instance_variable_set("@#{name}", val) self end end end end class Retry extend Setters make_setter :tries, :interval, :exponential_backoff, :error_types def initialize(**args) @tries = args[:tries] || 3 @interval = args[:interval] || 1 @exponential_backoff = args[:exponential_backoff] || false @error_types = args[:error_types] || [StandardError] end def run(&block) count = 1 while count <= @tries next unless block_given? begin return instance_eval(&block) rescue *@error_types => e count += 1 time = @exponential_backoff ? @interval**count : @interval sleep time e end end LOGGER.error e raise "Retries exhausted, error: #{e.message}" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tlopo-retry-0.2.0 | lib/tlopo/retry.rb |