easy_retry 1.0.5 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1628eebabfc2dd25f53d106aa0c665b20c2cc804ee7ff66a7372e784b02594d
4
- data.tar.gz: 0a1c561a8b510b25c580f4a6df1e3d2af2b913459c30c8c44d30d7985203bb96
3
+ metadata.gz: 830b86bc6808d551a48e8cf1c12618ad9de85798f1528bf99c7cab98a609353a
4
+ data.tar.gz: 7c633deca8936603e22f2673cf5dde56c5b934565ed04cdf50452e69c2b2688f
5
5
  SHA512:
6
- metadata.gz: 8ace3d6f0a2e94bcc8e29cf04c9bf1b93a8e5e6d17a45c6b21d50d191a3279d50815f7f805aa21665aa114724ea6d064a9ade21e2939582464798ba6d01bf5d9
7
- data.tar.gz: 839d5afade1c5e160fb2c7c181f7b952f30bd147246c3956b268ad0039e3343bab1ba8b4a90b197e3cc0565cc193cdce77780c94ec6b8c7147d4ed3eaeda3652
6
+ metadata.gz: 57b5e797ced529a355a651b81860377394ce12231435857ebc036b34022b84db369c7338a2a390bdcc8eb2de5fad017f749038c651c034ee5069cbf96bbe507a
7
+ data.tar.gz: 4ac048a61a96bdee00821f5f9d3109320a519610feb0ba4f060c5358bbd36090d71db1d8c3bbc39a87b23444e7ec3a2af5aac13d77e5cb06d1efe225f9beadda
data/CHANGELOG.md CHANGED
@@ -1,10 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [1.0.6] - TBD
3
+ ## [Released]
4
4
 
5
- - Allow configuration of logger
5
+ ## [1.0.7] - 2022-11-23
6
6
 
7
- ## [Released]
7
+ - Log the error class rather than `Error`.
8
+
9
+ ## [1.0.6] - 2022-11-22
10
+
11
+ - Allow configuration of logger
8
12
 
9
13
  ## [1.0.5] - 2022-11-22
10
14
 
data/Gemfile CHANGED
@@ -14,3 +14,5 @@ gem 'rubocop', '~> 1.21'
14
14
  gem 'pry'
15
15
 
16
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.5)
4
+ easy_retry (1.0.7)
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.
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module EasyRetry
6
+ # Configuration class
7
+ class Configuration
8
+ attr_accessor :logger
9
+
10
+ def initialize
11
+ @logger = Logger.new($stdout)
12
+ end
13
+ end
14
+ 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 "#{e.class.name}: #{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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyRetry
4
- VERSION = '1.0.5'
4
+ VERSION = "1.0.7"
5
5
  end
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
- # Extend the Numeric class with a #tries method
6
- class Numeric
7
- # rubocop:disable Metrics/MethodLength
8
- def tries(rescue_from: [StandardError])
9
- raise ArgumentError, 'No block given' unless block_given?
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
- current_try += 1
14
+ def configure
15
+ yield(configuration)
32
16
  end
33
17
 
34
- result
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,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.5
4
+ version: 1.0.7
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-22 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-11-23 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
@@ -27,8 +41,9 @@ files:
27
41
  - LICENSE.txt
28
42
  - README.md
29
43
  - Rakefile
30
- - easy_retry.gemspec
31
44
  - lib/easy_retry.rb
45
+ - lib/easy_retry/configuration.rb
46
+ - lib/easy_retry/core.rb
32
47
  - lib/easy_retry/version.rb
33
48
  - sig/easy_retry.rbs
34
49
  homepage: https://github.com/GoudekettingRM/easy_retry
data/easy_retry.gemspec DELETED
@@ -1,38 +0,0 @@
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