def_retry 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d7d2f3cfa3b5fb99b74a57b3d40ebdbc03161ca
4
+ data.tar.gz: 3288a59b0594666400dcad6244f00c67182d4ad5
5
+ SHA512:
6
+ metadata.gz: e10727f4637076e2cc7ac9a233e003bf917e355e2eab7a660b00952b1f169d24808498658a9c23b93bb1c2206fecd175d755f2f332d2f80041e3be00d4ef9816
7
+ data.tar.gz: 5fbe0d54a06c2c5cb547bf5e3e4d75c37ec31c083cb341ef0103de7d69e6c19bc0b81b153f0cca73f56618bdb3c434c62630eeeb884a8bcbd01492ba63e87208
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in def_retry.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Diego Salazar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,96 @@
1
+ # DefRetry
2
+
3
+ An expressive, fully spec'd gem to add the Retry Pattern to your methods and/or objects. With DefRetry
4
+ you can define methods with retry logic built-in or you can wrap your code in a
5
+ `retry` and specify options to customize the behavior.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'def_retry'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install def_retry
20
+
21
+ ## Usage
22
+
23
+ ### Defining a retryable method
24
+
25
+ ```ruby
26
+ require 'def_retry'
27
+
28
+ class ApiWrapper
29
+ include DefRetry
30
+
31
+ def_retry :get_data, on: ApiError do
32
+ do_api_call
33
+ end
34
+ end
35
+ ```
36
+
37
+ This will define an instance method named `:get_data` and rescue the exception
38
+ `ApiError` and retry the block:
39
+
40
+ ```ruby
41
+ do
42
+ do_api_call
43
+ end
44
+ ```
45
+ 3 times (the default).
46
+
47
+ ### Retrying a block of code
48
+
49
+ ```ruby
50
+ require 'def_retry'
51
+
52
+ class ApiWrapper
53
+ include DefRetry
54
+
55
+ def get_data
56
+ @some_state = 'start'
57
+
58
+ retryable on: ApiError do
59
+ @some_state = 'working'
60
+ do_api_call
61
+ @some_state = 'done'
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ This will retry just that block of code.
68
+
69
+ ### Don't want to mixin?
70
+
71
+ Use `DefRetry.retry` directly:
72
+
73
+ ```ruby
74
+ require 'def_retry'
75
+
76
+ DefRetry.retry on: ApiError do
77
+ do_api_call
78
+ end
79
+ ```
80
+
81
+ ### Options
82
+
83
+ These apply to both `.def_retry` and `#retryable`:
84
+ - `:on`: A single class or an array of exception classes to be rescued
85
+ - `:tries`: Integer number of maximum retries to run. DefRetry will stop retrying if the retry count reaches this number
86
+ - `: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 (see: `DefRetry::Retrier::SLEEP_STRATEGIES`)
87
+ - `:on_retry`: A callback to run every time a retry happens i.e. the specified exception(s) are rescued. It will receive the exception that was rescued and the current try count as arguments, respectively.
88
+ - `:on_ensure`: A callback to run at the end before returning the block's value. It will receive the block's return value and the current try count as arguments, respectively.
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it ( https://github.com/DiegoSalazar/def_retry/fork )
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'def_retry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "def_retry"
8
+ spec.version = DefRetry::VERSION
9
+ spec.authors = ["Diego Salazar"]
10
+ spec.email = ["diego@greyrobot.com"]
11
+ spec.summary = %q{The Retry Pattern in a gem.}
12
+ spec.description = %q{Allows you to define methods that will retry on exception or declare blocks of code with retry.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,28 @@
1
+ require "def_retry/version"
2
+
3
+ module DefRetry
4
+ autoload :Retrier, 'def_retry/retrier'
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.send :include, InstanceMethods
9
+ end
10
+
11
+ def self.retry(options, &block)
12
+ Retrier.new(options, block).run
13
+ end
14
+
15
+ module ClassMethods
16
+ def def_retry(name, options = {}, &block)
17
+ define_method name do
18
+ DefRetry.retry options, &block
19
+ end
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ def retryable(options = {}, &block)
25
+ DefRetry.retry options, &block
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ module DefRetry
2
+ class Retrier
3
+ DEFAULT_TRIES = 3
4
+ SLEEP_STRATEGIES = {
5
+ constant: ->(n) { 1 },
6
+ linear: ->(n) { n },
7
+ exponential: ->(n) { n**2 }
8
+ }
9
+
10
+ def initialize(options, block)
11
+ @block = block
12
+ @tries = options.fetch :tries, DEFAULT_TRIES
13
+ @on_retry = options.fetch :on_retry, ->(e, n) {}
14
+ @on_ensure = options.fetch :on_ensure, ->(r, n) {}
15
+ @sleep = options.fetch :sleep, false
16
+
17
+ begin
18
+ @sleep = SLEEP_STRATEGIES.fetch @sleep if @sleep.is_a? Symbol
19
+ rescue KeyError
20
+ raise ArgumentError, "The :sleep option must be a Proc or one of: #{SLEEP_STRATEGIES.keys.join(', ')}"
21
+ end
22
+
23
+ begin
24
+ @exceptions = Array options.fetch(:on)
25
+ rescue KeyError
26
+ raise ArgumentError, 'You must specify which :exceptions to retry on'
27
+ end
28
+ end
29
+
30
+ def run
31
+ @try_count = 0
32
+ @return = nil
33
+
34
+ begin
35
+ @return = @block.call
36
+ rescue *@exceptions => e
37
+ @try_count += 1
38
+ sleep @sleep.call(@try_count) if @sleep
39
+ @on_retry.call e, @try_count
40
+
41
+ retry if @try_count < @tries
42
+ ensure
43
+ @on_ensure.call @return, @try_count
44
+ @return
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module DefRetry
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'def_retry'
3
+
4
+ class MockRetryable
5
+ include DefRetry
6
+
7
+ def_retry :api_call, on: Exception do
8
+ 1 + 1 # expensive API call!
9
+ end
10
+ end
11
+
12
+ describe DefRetry do
13
+ let(:mock_retryable) { MockRetryable }
14
+
15
+ context '.def_retry' do
16
+ it 'defines an instance method' do
17
+ expect(mock_retryable.new).to respond_to :api_call
18
+ end
19
+ end
20
+
21
+ context '#retryable' do
22
+ it "returns the block's value" do
23
+ mocked = mock_retryable.new
24
+ expect(mocked.retryable(on: Exception) { 2 + 2 }).to be 4
25
+ end
26
+ end
27
+ end
28
+
29
+ # Note: see retrier_spec.rb for full specs
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+ require 'def_retry/retrier'
3
+
4
+ describe DefRetry::Retrier do
5
+ let :block_exception do
6
+ -> {
7
+ @retries ||= 0
8
+ @retries += 1
9
+ raise Exception
10
+ }
11
+ end
12
+
13
+ context '#new' do
14
+ it 'requires exceptions to be specified' do
15
+ expect {
16
+ DefRetry::Retrier.new({}, -> {})
17
+ }.to raise_error ArgumentError
18
+ end
19
+
20
+ it 'complains about invalid sleep strategies' do
21
+ expect {
22
+ DefRetry::Retrier.new({
23
+ on: Exception,
24
+ sleep: :invalid
25
+ }, -> {})
26
+ }.to raise_error ArgumentError
27
+ end
28
+ end
29
+
30
+ context '#run' do
31
+ it 'executes the block and returns its value' do
32
+ the_block = -> { :ran }
33
+ retrier = DefRetry::Retrier.new({
34
+ on: Exception
35
+ }, the_block)
36
+
37
+ expect(retrier.run).to be :ran
38
+ end
39
+
40
+ it 'retries on exception :limit times' do
41
+ retrier = DefRetry::Retrier.new({
42
+ on: Exception,
43
+ tries: 2
44
+ }, block_exception)
45
+
46
+ retrier.run
47
+ expect(@retries).to be 2
48
+ end
49
+
50
+ it 'raises unspecified exceptions' do
51
+ retrier = DefRetry::Retrier.new({
52
+ on: ArgumentError
53
+ }, block_exception)
54
+
55
+ expect { retrier.run }.to raise_error Exception
56
+ end
57
+
58
+ it 'runs an on_retry callback' do
59
+ retrier = DefRetry::Retrier.new({
60
+ on: Exception,
61
+ on_retry: ->(e, n) { @did_retry = :yes }
62
+ }, block_exception)
63
+
64
+ retrier.run
65
+ expect(@did_retry).to be :yes
66
+ end
67
+
68
+ it 'runs an on_ensure callback' do
69
+ retrier = DefRetry::Retrier.new({
70
+ on: Exception,
71
+ on_ensure: ->(r, n) { @did_ensure = :yes }
72
+ }, -> {})
73
+
74
+ retrier.run
75
+ expect(@did_ensure).to be :yes
76
+ end
77
+
78
+ it 'passes the exception and retry count to on_retry' do
79
+ retrier = DefRetry::Retrier.new({
80
+ on: Exception,
81
+ on_retry: ->(e, r) {
82
+ @exception = e
83
+ @retry_count = r
84
+ }
85
+ }, block_exception)
86
+
87
+ retrier.run
88
+ expect(@exception).to be_kind_of Exception
89
+ expect(@retry_count).to be 3 # default limit
90
+ end
91
+
92
+ it 'passes the return value and retry count to on_ensure' do
93
+ retrier = DefRetry::Retrier.new({
94
+ on: Exception,
95
+ on_ensure: ->(v, r) {
96
+ @value = v
97
+ @retry_count = r
98
+ }
99
+ }, -> { :ran })
100
+
101
+ retrier.run
102
+ expect(@value).to be :ran
103
+ expect(@retry_count).to be 0 # default limit
104
+ end
105
+ end
106
+
107
+ context '#run with :constant sleep strategy' do
108
+ it 'sleeps for 2 seconds' do
109
+ retrier = DefRetry::Retrier.new({
110
+ on: Exception,
111
+ sleep: :constant,
112
+ # the 1st retry will sleep for 1 second
113
+ # the 2nd retry will sleep for 1 second
114
+ tries: 2
115
+ }, block_exception)
116
+
117
+ start_time = Time.now.to_i
118
+ retrier.run
119
+ end_time = Time.now.to_i
120
+
121
+ expect(end_time - start_time).to be 2
122
+ end
123
+ end
124
+
125
+ context '#run with :linear sleep strategy' do
126
+ it 'sleeps for 3 seconds' do
127
+ retrier = DefRetry::Retrier.new({
128
+ on: Exception,
129
+ sleep: :linear,
130
+ # the 1st retry will sleep for 1 second
131
+ # the 2nd retry will sleep for 2 seconds, and so on
132
+ tries: 2
133
+ }, block_exception)
134
+
135
+ start_time = Time.now.to_i
136
+ retrier.run
137
+ end_time = Time.now.to_i
138
+
139
+ expect(end_time - start_time).to be 3
140
+ end
141
+ end
142
+
143
+ context '#run with :exponential sleep strategy' do
144
+ it 'sleeps for 5 seconds' do
145
+ retrier = DefRetry::Retrier.new({
146
+ on: Exception,
147
+ sleep: :exponential,
148
+ # the 1st retry will sleep for 1**2 == 1 second
149
+ # the 2nd retry will sleep for 2**2 == 4 seconds
150
+ tries: 2
151
+ }, block_exception)
152
+
153
+ start_time = Time.now.to_i
154
+ retrier.run
155
+ end_time = Time.now.to_i
156
+
157
+ expect(end_time - start_time).to be 5
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,13 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: def_retry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diego Salazar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Allows you to define methods that will retry on exception or declare
56
+ blocks of code with retry.
57
+ email:
58
+ - diego@greyrobot.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - def_retry.gemspec
69
+ - lib/def_retry.rb
70
+ - lib/def_retry/retrier.rb
71
+ - lib/def_retry/version.rb
72
+ - spec/def_retry_spec.rb
73
+ - spec/retrier_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: The Retry Pattern in a gem.
99
+ test_files:
100
+ - spec/def_retry_spec.rb
101
+ - spec/retrier_spec.rb
102
+ - spec/spec_helper.rb