Archive

Archive for December, 2011

a tiny ruby retry class

December 31, 2011 Leave a comment

Here’s a tiny ruby retry class.

class Retryable
  def self.attempt(options = {}, &block)
    opts = { :tries => 1, :on => Exception }.merge(options)
    retry_exception, tries = opts[:on], opts[:tries]
    begin
      return yield
    rescue retry_exception
      retry if (tries -= 1) > 0
      raise
    end
    yield
  end
end

Usage is like this

Retryable.attempt do
     #something that might fail, but you want to try it a few times
end
Categories: tech Tags:
%d bloggers like this: