Skip to content

DiegoSalazar/DefRetry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DefRetry

An expressive, fully spec'd, gem to add the Retry Pattern to your methods and/or objects. With DefRetry you can define methods with retry logic built-in or you can wrap your code in a retry and specify options to customize the behavior.

Installation

Add this line to your application's Gemfile:

gem 'def_retry'

And then execute:

$ bundle

Or install it yourself as:

$ gem install def_retry

Usage

Defining a retryable method

require 'def_retry'

class ApiWrapper
  include DefRetry

  def_retry :get_data, on: ApiError do
    do_api_call
  end
end

This will define an instance method named :get_data and rescue the exception ApiError and retry the block

do
  do_api_call
end

3 times (the default)

Retrying a block of code

require 'def_retry'

class ApiWrapper
  include DefRetry

  def get_data
    @some_state = 'start'

    retry on: ApiError do
      @some_state = 'working'
      do_api_call
    end

    @some_state = 'done'
  end
end

This will retry just that block of code.

Options

These apply to both .def_retry and #retry:

  • on: a single class or an array of exception classes to be rescued
  • tries: Integer number of maximum retries to run. DefRetry will stop retrying if the retry cound reaches this number
  • sleep: Either a Proc that receives the current try count as its only argument or a Symbol naming one of these sleep strategies: constant, linear, exponential
  • on_retry: a callback to run everytime a retry happens i.e. the specified expception(s) are rescued
  • on_ensure: "a callback to run at the end before returning the block's value"

Contributing

  1. Fork it ( https://github.com/[my-github-username]/def_retry/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

Add retry logic to your methods/objects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages