Skip to content

Instantly share code, notes, and snippets.

@raul
Created October 7, 2011 06:42

Revisions

  1. raul revised this gist Oct 7, 2011. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # retry_upto(5) do ... end
    #
    # - retry up to 5 times, waiting 2 seconds between retries and retrying after any exception
    # - retry up to 5 times, waiting 2 seconds between retries
    #
    # retry_upto(5, :wait => 2) do ... end
    #
    @@ -16,22 +16,33 @@
    #
    # retry_upto(5, :wait => 1, :patience => 0.5) do ... end
    #
    # - retry up to 5 times, waiting 1 second initially and retrying with a randomly increasing patience
    #
    # retry_upto(5, :wait => 1, :patience => lambda{ |x| x + rand(3) } ) do ... end
    #
    # - retry up to 5 times without waiting between retries, retrying only after a ZeroDivisionError
    #
    # retry_upto(5, :rescue_only => ZeroDivisionError) do ... end
    #
    # - retry up to 5 times, waiting 2 seconds between retries, retrying only after a ZeroDivisionError
    #
    # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end
    #

    def retry_upto(max_retries = 1, options = {})
    yield
    rescue (options[:rescue_only] || Exception)
    raise if (max_retries -= 1) == 0
    sleep((options[:wait] || 0) * (options[:patience] || 1))
    if options[:patience].respond_to?('*')
    options[:wait] = options[:wait] * options[:patience]
    elsif options[:patience].respond_to?(:call)
    options[:wait] = options[:patience].call(options[:wait])
    end
    sleep(options[:wait] || 0)
    retry
    end


    # Extends enumerator to allow usage like:
    #
    # 5.times.retry do
  2. raul revised this gist Oct 7, 2011. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,7 @@ def retry_upto(max_retries = 1, options = {})
    yield
    rescue (options[:rescue_only] || Exception)
    raise if (max_retries -= 1) == 0
    options[:wait] *= options[:patience] if options[:wait] && options[:patience]
    sleep(options[:wait] || 0)
    sleep((options[:wait] || 0) * (options[:patience] || 1))
    retry
    end

  3. raul revised this gist Oct 7, 2011. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,14 @@
    #
    # retry_upto(5, :wait => 2) do ... end
    #
    # - retry up to 5 times, waiting 1 second initially and having increasing patience (2, 4, 8, ...)
    #
    # retry_upto(5, :wait => 1, :patience => 2) do ... end
    #
    # - retry up to 5 times, waiting 1 second initially and having decreasing patience (0.5, 0.25, 0.125, ...)
    #
    # retry_upto(5, :wait => 1, :patience => 0.5) do ... end
    #
    # - retry up to 5 times without waiting between retries, retrying only after a ZeroDivisionError
    #
    # retry_upto(5, :rescue_only => ZeroDivisionError) do ... end
    @@ -20,6 +28,7 @@ def retry_upto(max_retries = 1, options = {})
    yield
    rescue (options[:rescue_only] || Exception)
    raise if (max_retries -= 1) == 0
    options[:wait] *= options[:patience] if options[:wait] && options[:patience]
    sleep(options[:wait] || 0)
    retry
    end
  4. raul revised this gist Oct 7, 2011. 1 changed file with 17 additions and 6 deletions.
    23 changes: 17 additions & 6 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -17,11 +17,22 @@
    # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end

    def retry_upto(max_retries = 1, options = {})
    begin
    return yield if ((max_retries -= 1) > 0)
    rescue (options[:rescue_only] || Exception)
    sleep(options[:wait] || 0)
    retry
    end
    yield
    rescue (options[:rescue_only] || Exception)
    raise if (max_retries -= 1) == 0
    sleep(options[:wait] || 0)
    retry
    end

    # Extends enumerator to allow usage like:
    #
    # 5.times.retry do
    # ...
    # end
    #

    class Enumerator
    def retry(options = {}, &blk)
    retry_upto(self.count, options, &blk)
    end
    end
  5. raul revised this gist Oct 7, 2011. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Ruby's `retry` with steroids:
    # Ruby `retry` with steroids:
    #
    # - retry up to 5 times without waiting between them and retrying after any exception
    #
    @@ -17,12 +17,10 @@
    # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end

    def retry_upto(max_retries = 1, options = {})
    wait_secs = options[:wait] || 0
    exception_class = options[:rescue_only] || Exception
    begin
    return yield if ((max_retries -= 1) > 0)
    rescue exception_class => e
    sleep(wait_secs)
    rescue (options[:rescue_only] || Exception)
    sleep(options[:wait] || 0)
    retry
    end
    yield
  6. raul revised this gist Oct 7, 2011. 1 changed file with 23 additions and 2 deletions.
    25 changes: 23 additions & 2 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,29 @@
    def retry_upto(max_retries = 1, exception_class = Exception)
    # Ruby's `retry` with steroids:
    #
    # - retry up to 5 times without waiting between them and retrying after any exception
    #
    # retry_upto(5) do ... end
    #
    # - retry up to 5 times, waiting 2 seconds between retries and retrying after any exception
    #
    # retry_upto(5, :wait => 2) do ... end
    #
    # - retry up to 5 times without waiting between retries, retrying only after a ZeroDivisionError
    #
    # retry_upto(5, :rescue_only => ZeroDivisionError) do ... end
    #
    # - retry up to 5 times, waiting 2 seconds between retries, retrying only after a ZeroDivisionError
    #
    # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end

    def retry_upto(max_retries = 1, options = {})
    wait_secs = options[:wait] || 0
    exception_class = options[:rescue_only] || Exception
    begin
    return yield if ((max_retries -= 1) > 0)
    rescue exception_class => e
    sleep(wait_secs)
    retry
    end
    yield
    end
    end
  7. raul created this gist Oct 7, 2011.
    8 changes: 8 additions & 0 deletions retry_upto.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    def retry_upto(max_retries = 1, exception_class = Exception)
    begin
    return yield if ((max_retries -= 1) > 0)
    rescue exception_class => e
    retry
    end
    yield
    end