retry_it 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +22 -0
- data/README.md +110 -0
- data/Rakefile +10 -0
- data/bin/console +6 -0
- data/bin/setup +8 -0
- data/lib/retry_it.rb +32 -0
- data/lib/retry_it/version.rb +3 -0
- data/retry_it.gemspec +29 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3133fcf53875f5554c5fbdbb62f7e0cd413efd2db0ad11e30afa99e06be263b3
|
4
|
+
data.tar.gz: 4bb4e6ce29a77b4f3b3bd816bf0766b58540641894b4298ee86b2534f3867771
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7f5dd9a4ffa9728d2ff13b6f2f898ebe9762c9a2f9801c140025b3a8f52363a3e966298bded772382cd1756941908917362ec1aa2f54ab5901299456a90016d
|
7
|
+
data.tar.gz: 98571c88ef2f8758d45f71965d1c2607eeeb00e4f0f8775509207d97129edbc74608042d7c65cea4f642530df6adf9544040d10e86bd271c82228280f3521a27
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
retryable (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.11.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
bundler (~> 1.16)
|
17
|
+
minitest (~> 5.0)
|
18
|
+
rake (~> 10.0)
|
19
|
+
retryable!
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
1.16.5
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# RetryIt
|
2
|
+
|
3
|
+
Easily retry a code block a set amount of times, before giving up. Useful for unreliable external I/O, such as accessing
|
4
|
+
HTTP servers that periodically throw errors, but are expected to work most of the time.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'retry_it'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install retry_it
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Include RetryIt in your class:
|
25
|
+
|
26
|
+
```
|
27
|
+
class APIClient
|
28
|
+
include RetryIt
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
You can then call `retry_it` when running code that fails intermittently:
|
33
|
+
|
34
|
+
```
|
35
|
+
class APIClient
|
36
|
+
include RetryIt
|
37
|
+
|
38
|
+
def get_data
|
39
|
+
retry_it(errors: [Timeout::Error]) do
|
40
|
+
HTTP.get('http://foo.com')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
If `HTTP.get` throws an error, `retry_it` will wait a few seconds, and then try
|
47
|
+
again. It will repeat this a few times before eventually giving up, and raising
|
48
|
+
the last error it received.
|
49
|
+
|
50
|
+
`retry_it` accepts these arguments:
|
51
|
+
|
52
|
+
* `max_runs`: controls the maximum number of times the block should be fun.
|
53
|
+
Defaults to RetryIt::MAX_RUNS
|
54
|
+
* `errors`: an Array of subclasses of Exception, which indicate which Exceptions
|
55
|
+
are considered retryable.
|
56
|
+
* `timeout`: how many seconds we will wait between retries. Defaults to
|
57
|
+
RetryIt::DEFAULT_TIMEOUT_S
|
58
|
+
* `logger`: A Logger object. If provided, when a retry occurs, an info-level
|
59
|
+
message will be logged.
|
60
|
+
* `should_retry_proc`: A Proc that can be used to more finely control when a
|
61
|
+
retry occurs. The Proc is given one parameter: the
|
62
|
+
Exception object. The Proc must return a boolean value.
|
63
|
+
A true indicates that a retry should occur. Useful for
|
64
|
+
when it is sometimes desired to retry from an error, but
|
65
|
+
not always (For instance, an HTTP error with code 504 is
|
66
|
+
retryable, but a 404 probably isn't)
|
67
|
+
|
68
|
+
Examples:
|
69
|
+
|
70
|
+
```
|
71
|
+
# Retry only on HTTPErrors with code 504. All other errors will not trigger a
|
72
|
+
# retry.
|
73
|
+
retry_it(timeout: 60, errors: [HTTPError], should_retry_proc: Proc.new { |e| e.code == 504 }) do
|
74
|
+
some_api_request
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
# Use a logger to be notified when a retry occurs:
|
79
|
+
require 'logger'
|
80
|
+
|
81
|
+
logger = Logger.new STDOUT
|
82
|
+
retry_it(max_runs: 100, timeout: 60, errors: [Error], logger: logger) do
|
83
|
+
some_api_request
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Keep in mind that you can pass methods as parameters using `method`:
|
88
|
+
|
89
|
+
```
|
90
|
+
class APIClient
|
91
|
+
include RetryIt
|
92
|
+
|
93
|
+
def download_data
|
94
|
+
retry_it(should_retry_proc: method(:is_retryable), errors: [HTTPError]) do
|
95
|
+
api.get("foo.com")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def is_retryable(error)
|
100
|
+
error.code == 504
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
|
106
|
+
## Development
|
107
|
+
|
108
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
109
|
+
|
110
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/retry_it.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "retry_it/version"
|
2
|
+
|
3
|
+
module RetryIt
|
4
|
+
MAX_RUNS = 10
|
5
|
+
DEFAULT_TIMEOUT_S = 10 # seconds
|
6
|
+
DEFAULT_EXCEPTIONS = []
|
7
|
+
|
8
|
+
def retry_it(max_runs: MAX_RUNS,
|
9
|
+
errors: DEFAULT_EXCEPTIONS,
|
10
|
+
timeout: DEFAULT_TIMEOUT_S,
|
11
|
+
should_retry_proc: nil,
|
12
|
+
logger: nil)
|
13
|
+
retries = 0
|
14
|
+
begin
|
15
|
+
yield
|
16
|
+
rescue *errors => e
|
17
|
+
retries += 1
|
18
|
+
if retries < max_runs && (!should_retry_proc.is_a?(Proc) || should_retry_proc.call(e))
|
19
|
+
if logger
|
20
|
+
logger.info "Error (#{e.class}), retrying ##{retries} of #{max_runs}. Sleeping for #{timeout}"
|
21
|
+
end
|
22
|
+
if timeout > 0
|
23
|
+
sleep timeout
|
24
|
+
end
|
25
|
+
retry
|
26
|
+
else
|
27
|
+
raise
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/retry_it.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "retry_it/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "retry_it"
|
8
|
+
spec.version = RetryIt::VERSION
|
9
|
+
spec.authors = ["rgould"]
|
10
|
+
spec.email = ["richard.gould@daliaresearch.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Easily retry code that fails intermittently}
|
13
|
+
spec.description = %q{Easily retry code that fails intermittently. Perfect for interacting with that flakey HTTP API.}
|
14
|
+
spec.homepage = "https://github.com/DaliaResearch/retry_it"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: retry_it
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rgould
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-26 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Easily retry code that fails intermittently. Perfect for interacting
|
56
|
+
with that flakey HTTP API.
|
57
|
+
email:
|
58
|
+
- richard.gould@daliaresearch.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/retry_it.rb
|
72
|
+
- lib/retry_it/version.rb
|
73
|
+
- retry_it.gemspec
|
74
|
+
homepage: https://github.com/DaliaResearch/retry_it
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.7.6
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Easily retry code that fails intermittently
|
98
|
+
test_files: []
|