Skip to content

Commit

Permalink
wrote readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Salazar committed Jul 1, 2014
1 parent e66cc15 commit 1c320a1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# DefRetry

TODO: Write a gem description
An expressive way to add retry code to your methods and/or classes. 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

Expand All @@ -18,7 +20,61 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
### Defining a retryable method

```ruby
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

```ruby
do
do_api_call
end
```
3 times (the default)

### Retrying a block of code

```ruby
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

Expand Down
16 changes: 8 additions & 8 deletions lib/def_retry/retrier.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DefRetry
class Retrier
DEFAULT_LIMIT = 3
DEFAULT_TRIES = 3
SLEEP_STRATEGIES = {
constant: ->(n) { 1 },
linear: ->(n) { n },
Expand All @@ -9,7 +9,7 @@ class Retrier

def initialize(options, block)
@block = block
@limit = options.fetch :limit, DEFAULT_LIMIT
@tries = options.fetch :tries, DEFAULT_TRIES
@on_retry = options.fetch :on_retry, ->(e, n) {}
@on_ensure = options.fetch :on_ensure, ->(r, n) {}
@sleep = options.fetch :sleep, false
Expand All @@ -28,19 +28,19 @@ def initialize(options, block)
end

def run
@retry_count = 0
@try_count = 0
@return = nil

begin
@return = @block.call
rescue *@exceptions => e
@retry_count += 1
sleep @sleep.call(@retry_count) if @sleep
@on_retry.call e, @retry_count
@try_count += 1
sleep @sleep.call(@try_count) if @sleep
@on_retry.call e, @try_count

retry if @retry_count < @limit
retry if @try_count < @tries
ensure
@on_ensure.call @return, @retry_count
@on_ensure.call @return, @try_count
@return
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/retrier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
it 'retries on exception :limit times' do
retrier = DefRetry::Retrier.new({
on: Exception,
limit: 2
tries: 2
}, block_exception)

retrier.run
Expand Down Expand Up @@ -111,7 +111,7 @@
sleep: :constant,
# the 1st retry will sleep for 1 second
# the 2nd retry will sleep for 1 second
limit: 2
tries: 2
}, block_exception)

start_time = Time.now.to_i
Expand All @@ -129,7 +129,7 @@
sleep: :linear,
# the 1st retry will sleep for 1 second
# the 2nd retry will sleep for 2 seconds, and so on
limit: 2
tries: 2
}, block_exception)

start_time = Time.now.to_i
Expand All @@ -147,7 +147,7 @@
sleep: :exponential,
# the 1st retry will sleep for 1**2 == 1 second
# the 2nd retry will sleep for 2**2 == 4 seconds
limit: 2
tries: 2
}, block_exception)

start_time = Time.now.to_i
Expand Down

0 comments on commit 1c320a1

Please sign in to comment.