easy_retry 1.0.4 → 1.0.6
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 +8 -6
- data/Gemfile.lock +4 -1
- data/README.md +57 -2
- data/Rakefile +3 -3
- data/easy_retry.gemspec +37 -0
- data/lib/easy_retry/configuration.rb +14 -0
- data/lib/easy_retry/core.rb +33 -0
- data/lib/easy_retry/version.rb +1 -1
- data/lib/easy_retry.rb +15 -31
- metadata +21 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8a0431c79ed41fca0259b18f9e168981590f4cc6371ab9e87def6539ab973c0
|
4
|
+
data.tar.gz: '08e27ce1c40d16f298e0819bc169030801ab3ca2434f6c23c8af6883c22f4a15'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69451e39a83b916f610693a987de7bbdc5964f28aa2c1cff8bde674b4967c57766b15182d2ab37f54b0ab640d8280b5deb72209ccda0f69e8147dd0dcf7ef358
|
7
|
+
data.tar.gz: 5951fc0c51e11bc8d21a6d729af1c51da95c439e1429b271693e1c7e62b341277beb81ba2a41dfd3085fb8128356cf8a99452671d80a5ae1f301ac7135764483
|
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
|
+
## [Released]
|
4
|
+
|
5
|
+
## [1.0.6] - 2022-11-22
|
6
|
+
|
7
|
+
- Allow configuration of logger
|
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,18 @@
|
|
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
|
17
|
+
|
18
|
+
gem 'logger'
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
easy_retry (1.0.
|
4
|
+
easy_retry (1.0.6)
|
5
|
+
logger (~> 1.5.1)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
@@ -11,6 +12,7 @@ GEM
|
|
11
12
|
diff-lcs (1.5.0)
|
12
13
|
docile (1.4.0)
|
13
14
|
json (2.6.2)
|
15
|
+
logger (1.5.1)
|
14
16
|
method_source (1.0.0)
|
15
17
|
parallel (1.22.1)
|
16
18
|
parser (3.1.2.1)
|
@@ -62,6 +64,7 @@ PLATFORMS
|
|
62
64
|
|
63
65
|
DEPENDENCIES
|
64
66
|
easy_retry!
|
67
|
+
logger
|
65
68
|
pry
|
66
69
|
rake (~> 13.0)
|
67
70
|
rspec (~> 3.0)
|
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,59 @@ 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
|
+
|
104
|
+
## Configuration
|
105
|
+
|
106
|
+
You can configure EasyRetry by adding an initializer as follows:
|
107
|
+
|
108
|
+
```rb
|
109
|
+
EasyRetry.configure do |config|
|
110
|
+
# configuration options
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
### Logger
|
115
|
+
|
116
|
+
By default, EasyRetry uses [logger](https://rubygems.org/gems/logger) for logging errors. You can add your custom logger in the configuration using the `config.logger` option.
|
117
|
+
|
118
|
+
```rb
|
119
|
+
# For Example, using Rails.logger
|
120
|
+
config.logger = Rails.logger
|
121
|
+
```
|
122
|
+
|
123
|
+
NB: The logger should follow Rails Logger conventions.
|
124
|
+
|
70
125
|
## Retry delay
|
71
126
|
|
72
127
|
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 +134,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
79
134
|
|
80
135
|
## Contributing
|
81
136
|
|
82
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
137
|
+
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
138
|
|
84
139
|
## License
|
85
140
|
|
@@ -87,4 +142,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
87
142
|
|
88
143
|
## Code of Conduct
|
89
144
|
|
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/
|
145
|
+
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,37 @@
|
|
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
|
+
spec.add_dependency 'logger', '~> 1.5.1'
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Extend the Numeric class with a #tries method
|
4
|
+
class Numeric
|
5
|
+
# rubocop:disable Metrics/MethodLength
|
6
|
+
def tries(rescue_from: [StandardError])
|
7
|
+
raise ArgumentError, 'No block given' unless block_given?
|
8
|
+
|
9
|
+
rescue_from = Array(rescue_from)
|
10
|
+
max_retry = self
|
11
|
+
current_try = 1
|
12
|
+
result = nil
|
13
|
+
|
14
|
+
loop do
|
15
|
+
result = yield(current_try)
|
16
|
+
|
17
|
+
break
|
18
|
+
rescue *rescue_from => e
|
19
|
+
EasyRetry.logger.info "Error: #{e.message} (#{current_try}/#{max_retry})"
|
20
|
+
|
21
|
+
raise if current_try >= max_retry
|
22
|
+
|
23
|
+
sleep current_try * current_try
|
24
|
+
|
25
|
+
current_try += 1
|
26
|
+
end
|
27
|
+
|
28
|
+
result
|
29
|
+
end
|
30
|
+
# rubocop:enable Metrics/MethodLength
|
31
|
+
|
32
|
+
alias try tries
|
33
|
+
end
|
data/lib/easy_retry/version.rb
CHANGED
data/lib/easy_retry.rb
CHANGED
@@ -1,38 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
result = nil
|
14
|
-
|
15
|
-
loop do
|
16
|
-
result = yield(current_try)
|
17
|
-
|
18
|
-
break
|
19
|
-
rescue *rescue_from => e
|
20
|
-
if defined?(Rails)
|
21
|
-
Rails.logger.error "Error: #{e.message} (#{current_try}/#{max_retry})"
|
22
|
-
else
|
23
|
-
puts "Error: #{e.message} (#{current_try}/#{max_retry})"
|
24
|
-
end
|
25
|
-
|
26
|
-
sleep current_try * current_try
|
27
|
-
|
28
|
-
current_try += 1
|
3
|
+
require_relative 'easy_retry/core'
|
4
|
+
require_relative 'easy_retry/version'
|
5
|
+
require_relative 'easy_retry/configuration'
|
6
|
+
|
7
|
+
# EasyRetry core module
|
8
|
+
module EasyRetry
|
9
|
+
class << self
|
10
|
+
def configuration
|
11
|
+
@configuration ||= EasyRetry::Configuration.new
|
12
|
+
end
|
29
13
|
|
30
|
-
|
14
|
+
def configure
|
15
|
+
yield(configuration)
|
31
16
|
end
|
32
17
|
|
33
|
-
|
18
|
+
def logger
|
19
|
+
configuration.logger
|
20
|
+
end
|
34
21
|
end
|
35
|
-
# rubocop:enable Metrics/MethodLength
|
36
|
-
|
37
|
-
alias try tries
|
38
22
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
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.6
|
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-
|
12
|
-
dependencies:
|
11
|
+
date: 2022-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: logger
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.5.1
|
13
27
|
description: Easily retry a block of code a predetermined number of times
|
14
28
|
email:
|
15
29
|
- robin@goudeketting.nl
|
@@ -19,6 +33,7 @@ extra_rdoc_files: []
|
|
19
33
|
files:
|
20
34
|
- ".rspec"
|
21
35
|
- ".rubocop.yml"
|
36
|
+
- ".ruby-version"
|
22
37
|
- CHANGELOG.md
|
23
38
|
- CODE_OF_CONDUCT.md
|
24
39
|
- Gemfile
|
@@ -26,7 +41,10 @@ files:
|
|
26
41
|
- LICENSE.txt
|
27
42
|
- README.md
|
28
43
|
- Rakefile
|
44
|
+
- easy_retry.gemspec
|
29
45
|
- lib/easy_retry.rb
|
46
|
+
- lib/easy_retry/configuration.rb
|
47
|
+
- lib/easy_retry/core.rb
|
30
48
|
- lib/easy_retry/version.rb
|
31
49
|
- sig/easy_retry.rbs
|
32
50
|
homepage: https://github.com/GoudekettingRM/easy_retry
|