retryrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - 2.0.0
6
+ - jruby-19mode
7
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Lionel Barrow
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.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Retry
2
+
3
+ [![Build Status](https://travis-ci.org/lionelbarrow/retry.png)](https://travis-ci.org/lionelbarrow/YOUR_PROJECT_NAME)
4
+
5
+ A few functions for trying again when things don't go as planned.
6
+
7
+ ### Usage
8
+
9
+ Suppose you had a function, `some_func`, which might raise `SomeError`. If the function does raise that error, you'd like to try again. In that case, you could say:
10
+ ```ruby
11
+ Retry.on_exception(SomeError) do
12
+ some_func
13
+ end
14
+ ```
15
+
16
+ (Note that if the function raises a different error, no retry will be attempted.)
17
+
18
+ If you'd like to retry a couple of times (let's say three), you can do that:
19
+ ```ruby
20
+ Retry.several_times(SomeError, 3) do
21
+ some_func
22
+ end
23
+ ```
24
+
25
+ Sometimes you want to wait a bit before retrying. This example will retry twice, with a four second delay between each attempt:
26
+ ```ruby
27
+ Retry.with_delay(SomeError, 2, 4) do
28
+ some_func
29
+ end
30
+ ```
31
+
32
+ Another common pattern is to wait more and more time between each attempt; this is usually called "exponential backoff". This example will retry 3 times, with a delay of 2, 4, and 8 seconds between each attempt:
33
+ ```ruby
34
+ Retry.with_exponential_backoff(SomeError, 3, 2) do
35
+ some_func
36
+ end
37
+ ```
38
+
39
+ ### License
40
+
41
+ The MIT License (MIT)
42
+
43
+ Copyright (c) 2013 Lionel Barrow
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ "Software"), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
59
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
60
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
61
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
62
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "rspec/core/rake_task"
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module Retry
2
+ VERSION = "0.1.0"
3
+ end
data/lib/retry.rb ADDED
@@ -0,0 +1,29 @@
1
+ module Retry
2
+ def self.on_exception(exception_class, &block)
3
+ _with_options(exception_class, 1, 0, false, &block)
4
+ end
5
+
6
+ def self.several_times(exception_class, retries, &block)
7
+ _with_options(exception_class, retries, 0, false, &block)
8
+ end
9
+
10
+ def self.with_delay(exception_class, retries, seconds_to_wait, &block)
11
+ _with_options(exception_class, retries, seconds_to_wait, false, &block)
12
+ end
13
+
14
+ def self.with_exponential_backoff(exception_class, retries, base_seconds, &block)
15
+ _with_options(exception_class, retries, base_seconds, true, &block)
16
+ end
17
+
18
+ def self._with_options(exception_class, retries, base_seconds, exponential_backoff, &block)
19
+ retries.times do |attempt|
20
+ begin
21
+ return yield
22
+ rescue exception_class
23
+ sleep(base_seconds ** (attempt + 1)) if exponential_backoff
24
+ sleep(base_seconds) unless exponential_backoff
25
+ end
26
+ end
27
+ yield
28
+ end
29
+ end
data/retryrb.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../lib/retry/version', __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "retryrb"
5
+ spec.version = Retry::VERSION
6
+ spec.authors = ["lionelbarrow"]
7
+ spec.email = ["lionelbarrow@gmail.com"]
8
+ spec.description = "Provides a handful of generic functions for retry strategies."
9
+ spec.summary = "Provides generic functions for retry strategies."
10
+ spec.homepage = "https://www.github.com/lionelbarrow/retry"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.3"
19
+ spec.add_development_dependency "rspec", "~> 2.14"
20
+ spec.add_development_dependency "rake"
21
+ end
@@ -0,0 +1,114 @@
1
+ require "spec_helper"
2
+
3
+ describe Retry do
4
+ describe "_with_options" do
5
+ it "retries if the correct exception is thrown" do
6
+ Retry.should_receive(:sleep).once
7
+
8
+ count = 0
9
+ Retry._with_options(DummyError, 1, 1, true) do
10
+ count += 1
11
+ raise DummyError if count == 1
12
+ end
13
+ count.should == 2
14
+ end
15
+
16
+ it "lets the exception propogate if it does not match" do
17
+ expect do
18
+ Retry._with_options(DummyError, 1, 1, true) do
19
+ raise
20
+ end
21
+ end.to raise_error
22
+ end
23
+
24
+ it "sleeps an increasing number of seconds in between attempts" do
25
+ Retry.should_receive(:sleep).with(2).once
26
+ Retry.should_receive(:sleep).with(4).once
27
+
28
+ count = 0
29
+ Retry._with_options(DummyError, 2, 2, true) do
30
+ count += 1
31
+ raise DummyError unless count == 3
32
+ end
33
+ end
34
+
35
+ it "tries to retry the number of times specified" do
36
+ Retry.should_receive(:sleep).exactly(4).times
37
+
38
+ count = 0
39
+ Retry._with_options(DummyError, 4, 1, true) do
40
+ count += 1
41
+ raise DummyError if count < 5
42
+ end
43
+ count.should == 5
44
+ end
45
+
46
+ it "allows the exception to pass through after the retries have been exhausted" do
47
+ Retry.should_receive(:sleep).with(1).exactly(2).times
48
+
49
+ count = 0
50
+ expect do
51
+ Retry._with_options(DummyError, 2, 1, true) do
52
+ count += 1
53
+ raise DummyError if count < 4
54
+ end
55
+ end.to raise_error(DummyError)
56
+ count.should == 3
57
+ end
58
+
59
+ it "propagates the block's return value" do
60
+ Retry._with_options(DummyError, 1, 1, true) do
61
+ 3
62
+ end.should == 3
63
+ end
64
+ end
65
+
66
+ describe "with_delay" do
67
+ it "sleeps a constant amount of time between attempts" do
68
+ Retry.should_receive(:sleep).with(1).exactly(3).times
69
+
70
+ count = 0
71
+ Retry.with_delay(DummyError, 4, 1) do
72
+ count += 1
73
+ raise DummyError unless count == 4
74
+ end
75
+ count.should == 4
76
+ end
77
+ end
78
+
79
+ describe "with_exponential_backoff" do
80
+ it "sleeps an increasing amount of time between attempts" do
81
+ Retry.should_receive(:sleep).with(3).once
82
+ Retry.should_receive(:sleep).with(9).once
83
+ Retry.should_receive(:sleep).with(27).once
84
+
85
+ count = 0
86
+ Retry.with_exponential_backoff(DummyError, 3, 3) do
87
+ count += 1
88
+ raise DummyError unless count == 4
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "several_times" do
94
+ it "retries the specified number of times" do
95
+ count = 0
96
+ Retry.several_times(DummyError, 3) do
97
+ count += 1
98
+ raise DummyError unless count == 4
99
+ end
100
+ count.should == 4
101
+ end
102
+ end
103
+
104
+ describe "on_exception" do
105
+ it "retries once" do
106
+ count = 0
107
+ Retry.on_exception(DummyError) do
108
+ count += 1
109
+ raise DummyError unless count == 2
110
+ end
111
+ count.should == 2
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,10 @@
1
+ require "retry"
2
+
3
+ class DummyError < StandardError; end
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retryrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - lionelbarrow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.14'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.14'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Provides a handful of generic functions for retry strategies.
63
+ email:
64
+ - lionelbarrow@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/retry.rb
77
+ - lib/retry/version.rb
78
+ - retryrb.gemspec
79
+ - spec/retry/retry_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://www.github.com/lionelbarrow/retry
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Provides generic functions for retry strategies.
106
+ test_files:
107
+ - spec/retry/retry_spec.rb
108
+ - spec/spec_helper.rb