easy_retry 1.0.5 → 1.0.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -3
- data/Gemfile +2 -0
- data/Gemfile.lock +4 -1
- data/README.md +21 -0
- data/easy_retry.gemspec +1 -2
- 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 +13 -30
- metadata +18 -2
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/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
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
@@ -101,6 +101,27 @@ The code above will produce the following output.
|
|
101
101
|
=> "This is try number 2"
|
102
102
|
```
|
103
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
|
+
|
104
125
|
## Retry delay
|
105
126
|
|
106
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.
|
data/easy_retry.gemspec
CHANGED
@@ -30,8 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ['lib']
|
32
32
|
|
33
|
-
|
34
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
33
|
+
spec.add_dependency 'logger', '~> 1.5.1'
|
35
34
|
|
36
35
|
# For more information and examples about making a new gem, check out our
|
37
36
|
# guide at: https://bundler.io/guides/creating_gem.html
|
@@ -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,39 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'easy_retry/core'
|
3
4
|
require_relative 'easy_retry/version'
|
5
|
+
require_relative 'easy_retry/configuration'
|
4
6
|
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
rescue_from = Array(rescue_from)
|
12
|
-
max_retry = self
|
13
|
-
current_try = 1
|
14
|
-
result = nil
|
15
|
-
|
16
|
-
loop do
|
17
|
-
result = yield(current_try)
|
18
|
-
|
19
|
-
break
|
20
|
-
rescue *rescue_from => e
|
21
|
-
if defined?(Rails)
|
22
|
-
Rails.logger.error "Error: #{e.message} (#{current_try}/#{max_retry})"
|
23
|
-
else
|
24
|
-
puts "Error: #{e.message} (#{current_try}/#{max_retry})"
|
25
|
-
end
|
26
|
-
|
27
|
-
raise if current_try >= max_retry
|
28
|
-
|
29
|
-
sleep current_try * current_try
|
7
|
+
# EasyRetry core module
|
8
|
+
module EasyRetry
|
9
|
+
class << self
|
10
|
+
def configuration
|
11
|
+
@configuration ||= EasyRetry::Configuration.new
|
12
|
+
end
|
30
13
|
|
31
|
-
|
14
|
+
def configure
|
15
|
+
yield(configuration)
|
32
16
|
end
|
33
17
|
|
34
|
-
|
18
|
+
def logger
|
19
|
+
configuration.logger
|
20
|
+
end
|
35
21
|
end
|
36
|
-
# rubocop:enable Metrics/MethodLength
|
37
|
-
|
38
|
-
alias try tries
|
39
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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
|
@@ -9,7 +9,21 @@ autorequire:
|
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
11
|
date: 2022-11-22 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
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
|
@@ -29,6 +43,8 @@ files:
|
|
29
43
|
- Rakefile
|
30
44
|
- easy_retry.gemspec
|
31
45
|
- lib/easy_retry.rb
|
46
|
+
- lib/easy_retry/configuration.rb
|
47
|
+
- lib/easy_retry/core.rb
|
32
48
|
- lib/easy_retry/version.rb
|
33
49
|
- sig/easy_retry.rbs
|
34
50
|
homepage: https://github.com/GoudekettingRM/easy_retry
|