easy_retry 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -2
- data/.ruby-version +1 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +6 -6
- data/Gemfile.lock +1 -1
- data/README.md +36 -2
- data/Rakefile +3 -3
- data/easy_retry.gemspec +38 -0
- data/lib/easy_retry/version.rb +1 -1
- data/lib/easy_retry.rb +5 -4
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1628eebabfc2dd25f53d106aa0c665b20c2cc804ee7ff66a7372e784b02594d
|
4
|
+
data.tar.gz: 0a1c561a8b510b25c580f4a6df1e3d2af2b913459c30c8c44d30d7985203bb96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ace3d6f0a2e94bcc8e29cf04c9bf1b93a8e5e6d17a45c6b21d50d191a3279d50815f7f805aa21665aa114724ea6d064a9ade21e2939582464798ba6d01bf5d9
|
7
|
+
data.tar.gz: 839d5afade1c5e160fb2c7c181f7b952f30bd147246c3956b268ad0039e3343bab1ba8b4a90b197e3cc0565cc193cdce77780c94ec6b8c7147d4ed3eaeda3652
|
data/.rubocop.yml
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [1.0.6] - TBD
|
4
|
+
|
5
|
+
- Allow configuration of logger
|
6
|
+
|
7
|
+
## [Released]
|
8
|
+
|
9
|
+
## [1.0.5] - 2022-11-22
|
10
|
+
|
11
|
+
- Raise before sleeping to not sleep for try^2 seconds before raising on the last try.
|
12
|
+
- Allow passing of single error
|
13
|
+
- Expand ReadMe
|
14
|
+
|
3
15
|
## [1.0.4] - 2022-11-17
|
4
16
|
|
5
17
|
- Bugfix for EasyRetry to work in Rails _and_ non-Rails projects
|
data/Gemfile
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source 'https://rubygems.org'
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in easy_retry.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
gem
|
8
|
+
gem 'rake', '~> 13.0'
|
9
9
|
|
10
|
-
gem
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
11
|
|
12
|
-
gem
|
12
|
+
gem 'rubocop', '~> 1.21'
|
13
13
|
|
14
|
-
gem
|
14
|
+
gem 'pry'
|
15
15
|
|
16
|
-
gem
|
16
|
+
gem 'simplecov', require: false
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
![Specs](https://github.com/goudekettingrm/easy_retry/actions/workflows/main.yml/badge.svg)
|
2
2
|
|
3
|
+
<a href="https://www.buymeacoffee.com/goudekettingrm" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-blue.png" alt="Buy Me A Coffee" height="41" width="174"></a>
|
4
|
+
|
3
5
|
# EasyRetry
|
4
6
|
|
5
7
|
<i>Easily retry a block of code a predetermined number of times.</i>
|
@@ -67,6 +69,38 @@ The code above will not rescue from the `ActiveRecord::RecordInvalid` error and
|
|
67
69
|
from (pry):16:in `block in __pry__'
|
68
70
|
```
|
69
71
|
|
72
|
+
Passing an array is not necessary if you need to only rescue from a single error
|
73
|
+
|
74
|
+
```rb
|
75
|
+
4.tries(rescue_from: ZeroDivisionError) do |try|
|
76
|
+
raise ZeroDivisionError if try < 2
|
77
|
+
raise ActiveRecord::RecordInvalid if try < 4
|
78
|
+
puts "Success!"
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
This will generate the same output.
|
83
|
+
|
84
|
+
## Block results
|
85
|
+
|
86
|
+
EasyRetry gives you back the result of the first time the block you passed successfully runs. This can be useful when you need to use the result of the block for other tasks that you do not necessarily want to place in the block.
|
87
|
+
|
88
|
+
```rb
|
89
|
+
result = 2.tries do |try|
|
90
|
+
raise 'Woops' if try < 2
|
91
|
+
"This is try number #{try}"
|
92
|
+
end
|
93
|
+
|
94
|
+
puts result
|
95
|
+
```
|
96
|
+
|
97
|
+
The code above will produce the following output.
|
98
|
+
|
99
|
+
```
|
100
|
+
Error: Woops (1/2)
|
101
|
+
=> "This is try number 2"
|
102
|
+
```
|
103
|
+
|
70
104
|
## Retry delay
|
71
105
|
|
72
106
|
The delay for each retry is based on the iteration count. The delay after each failed attempt is _n^2_, where _n_ is the current iteration that failed. E.g. after the first try, EasyRetry waits 1 second, after the second try it waits 4 seconds, then 9, then 16, then 25, then 36, etc.
|
@@ -79,7 +113,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
79
113
|
|
80
114
|
## Contributing
|
81
115
|
|
82
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
116
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/GoudekettingRM/easy_retry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/GoudekettingRM/easy_retry/blob/main/CODE_OF_CONDUCT.md).
|
83
117
|
|
84
118
|
## License
|
85
119
|
|
@@ -87,4 +121,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
87
121
|
|
88
122
|
## Code of Conduct
|
89
123
|
|
90
|
-
Everyone interacting in the EasyRetry project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
124
|
+
Everyone interacting in the EasyRetry project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/GoudekettingRM/easy_retry/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
5
|
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
7
7
|
|
8
|
-
require
|
8
|
+
require 'rubocop/rake_task'
|
9
9
|
|
10
10
|
RuboCop::RakeTask.new
|
11
11
|
|
data/easy_retry.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/easy_retry/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'easy_retry'
|
7
|
+
spec.version = EasyRetry::VERSION
|
8
|
+
spec.authors = ['Robin Goudeketting, Peter Duijnstee']
|
9
|
+
spec.email = ['robin@goudeketting.nl']
|
10
|
+
|
11
|
+
spec.summary = 'Easily retry a block of code a predetermined number of times'
|
12
|
+
spec.description = 'Easily retry a block of code a predetermined number of times'
|
13
|
+
spec.homepage = 'https://github.com/GoudekettingRM/easy_retry'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = '>= 2.6.0'
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/GoudekettingRM/easy_retry'
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/GoudekettingRM/easy_retry/blob/main/CHANGELOG.md'
|
20
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = 'exe'
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
end
|
data/lib/easy_retry/version.rb
CHANGED
data/lib/easy_retry.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'easy_retry/version'
|
4
4
|
|
5
5
|
# Extend the Numeric class with a #tries method
|
6
6
|
class Numeric
|
7
7
|
# rubocop:disable Metrics/MethodLength
|
8
8
|
def tries(rescue_from: [StandardError])
|
9
|
-
raise ArgumentError,
|
9
|
+
raise ArgumentError, 'No block given' unless block_given?
|
10
10
|
|
11
|
+
rescue_from = Array(rescue_from)
|
11
12
|
max_retry = self
|
12
13
|
current_try = 1
|
13
14
|
result = nil
|
@@ -23,11 +24,11 @@ class Numeric
|
|
23
24
|
puts "Error: #{e.message} (#{current_try}/#{max_retry})"
|
24
25
|
end
|
25
26
|
|
27
|
+
raise if current_try >= max_retry
|
28
|
+
|
26
29
|
sleep current_try * current_try
|
27
30
|
|
28
31
|
current_try += 1
|
29
|
-
|
30
|
-
raise e if current_try > max_retry
|
31
32
|
end
|
32
33
|
|
33
34
|
result
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_retry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Goudeketting, Peter Duijnstee
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Easily retry a block of code a predetermined number of times
|
14
14
|
email:
|
@@ -19,6 +19,7 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- ".rspec"
|
21
21
|
- ".rubocop.yml"
|
22
|
+
- ".ruby-version"
|
22
23
|
- CHANGELOG.md
|
23
24
|
- CODE_OF_CONDUCT.md
|
24
25
|
- Gemfile
|
@@ -26,6 +27,7 @@ files:
|
|
26
27
|
- LICENSE.txt
|
27
28
|
- README.md
|
28
29
|
- Rakefile
|
30
|
+
- easy_retry.gemspec
|
29
31
|
- lib/easy_retry.rb
|
30
32
|
- lib/easy_retry/version.rb
|
31
33
|
- sig/easy_retry.rbs
|