exp_retry 0.0.11 → 0.0.12
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/.rubocop.yml +79 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +1 -1
- data/Rakefile +2 -0
- data/exp-retry.gemspec +4 -2
- data/lib/exp_retry.rb +19 -9
- data/spec/.rubocop.yml +7 -0
- data/spec/lib/exp_retry_spec.rb +22 -20
- data/spec/spec_helper.rb +2 -1
- 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: dcf20e1954c741739c256c324c0511bd503d470177bddea6e440954df9b8b0e3
|
4
|
+
data.tar.gz: a698f18df11aea017d8a38c86c9840637008df8d0f94e7ac36cd50630a8eb3b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e8d7f73a2a9e089035b92931f1a10da74223d37721ac5791f3e77e33845b120a1181203cb18d1aea5f906b1cf82322d8d743f1233f3cd7d97232bf7785de72b
|
7
|
+
data.tar.gz: e1157a628e18cf70532756d65edeeb32c456160b6c6897a6c7c379dedea7f498bf45adcd27288dafdcff70f4cd5159ac6c6d45c9d7fb26788c87f0d3948e0168
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.3
|
5
|
+
Exclude:
|
6
|
+
- bin/**/*
|
7
|
+
- config/**/*
|
8
|
+
- db/**/*
|
9
|
+
- vendor/**/*
|
10
|
+
- tmp/**/*
|
11
|
+
|
12
|
+
Layout/IndentFirstHashElement:
|
13
|
+
EnforcedStyle: consistent
|
14
|
+
Layout/AccessModifierIndentation:
|
15
|
+
EnforcedStyle: outdent
|
16
|
+
Layout/EmptyLinesAroundAccessModifier:
|
17
|
+
Enabled: true
|
18
|
+
Layout/AlignArray:
|
19
|
+
Enabled: true
|
20
|
+
Layout/AlignHash:
|
21
|
+
Enabled: true
|
22
|
+
Layout/EmptyLineAfterGuardClause:
|
23
|
+
Enabled: true
|
24
|
+
Layout/SpaceInsideBlockBraces:
|
25
|
+
EnforcedStyle: space
|
26
|
+
EnforcedStyleForEmptyBraces: no_space
|
27
|
+
Layout/SpaceInsideHashLiteralBraces:
|
28
|
+
EnforcedStyle: space
|
29
|
+
EnforcedStyleForEmptyBraces: no_space
|
30
|
+
Layout/SpaceInsideArrayLiteralBrackets:
|
31
|
+
EnforcedStyle: no_space
|
32
|
+
|
33
|
+
Lint/UnusedMethodArgument:
|
34
|
+
Enabled: true
|
35
|
+
Lint/UselessAssignment:
|
36
|
+
Enabled: true
|
37
|
+
|
38
|
+
Metrics/LineLength:
|
39
|
+
Max: 100
|
40
|
+
Metrics/MethodLength:
|
41
|
+
Enabled: true
|
42
|
+
Metrics/ClassLength:
|
43
|
+
Enabled: true
|
44
|
+
Max: 125
|
45
|
+
Metrics/ModuleLength:
|
46
|
+
Max: 125
|
47
|
+
Metrics/ParameterLists:
|
48
|
+
Enabled: true
|
49
|
+
Metrics/CyclomaticComplexity:
|
50
|
+
Enabled: true
|
51
|
+
Metrics/AbcSize:
|
52
|
+
Enabled: true
|
53
|
+
|
54
|
+
Naming/MemoizedInstanceVariableName:
|
55
|
+
Enabled: false
|
56
|
+
Naming/UncommunicativeMethodParamName:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
Style/Documentation:
|
60
|
+
Enabled: false
|
61
|
+
Style/FrozenStringLiteralComment:
|
62
|
+
Enabled: true
|
63
|
+
Style/NumericLiterals:
|
64
|
+
Enabled: true
|
65
|
+
Style/StringLiterals:
|
66
|
+
EnforcedStyle: single_quotes
|
67
|
+
Style/AndOr:
|
68
|
+
Enabled: true
|
69
|
+
Style/ClassCheck:
|
70
|
+
Enabled: true
|
71
|
+
Style/GuardClause:
|
72
|
+
Enabled: true
|
73
|
+
|
74
|
+
Security/Eval:
|
75
|
+
Enabled: true
|
76
|
+
Security/JSONLoad:
|
77
|
+
Enabled: true
|
78
|
+
Security/YAMLLoad:
|
79
|
+
Enabled: true
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/exp-retry.gemspec
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Gem::Specification.new do |s|
|
2
4
|
s.name = 'exp_retry'
|
3
|
-
s.version = '0.0.
|
5
|
+
s.version = '0.0.12'
|
4
6
|
s.date = '2019-06-15'
|
5
7
|
s.summary = 'Exponential backoff retry'
|
6
8
|
s.description = 'A simple exponential backoff retry wrapper.'
|
@@ -12,7 +14,7 @@ Gem::Specification.new do |s|
|
|
12
14
|
s.files = `git ls-files`.split("\n")
|
13
15
|
s.test_files = `git ls-files -- spec/*`.split("\n")
|
14
16
|
|
15
|
-
s.required_ruby_version = '~> 2.
|
17
|
+
s.required_ruby_version = '~> 2.3'
|
16
18
|
|
17
19
|
s.add_development_dependency 'rake', '~> 12.2', '>= 12.2.1'
|
18
20
|
s.add_development_dependency 'rspec', '~> 3.7', '>= 3.7.0'
|
data/lib/exp_retry.rb
CHANGED
@@ -2,14 +2,18 @@
|
|
2
2
|
|
3
3
|
# Exponential backoff retry wrapper
|
4
4
|
class ExpRetry
|
5
|
+
attr_accessor :retries
|
6
|
+
attr_reader :exception
|
7
|
+
|
5
8
|
def self.for(retries: 3, exception: StandardError, verbose: false)
|
6
9
|
new(retries: retries, exception: exception, verbose: verbose).call { yield }
|
7
10
|
end
|
8
11
|
|
9
|
-
def initialize(retries: 3, exception: StandardError, verbose: false)
|
12
|
+
def initialize(retries: 3, exception: StandardError, wait: 1000, verbose: false)
|
10
13
|
@retries = retries
|
11
14
|
@exception = exception
|
12
15
|
@verbose = verbose
|
16
|
+
@wait = wait
|
13
17
|
end
|
14
18
|
|
15
19
|
def call
|
@@ -19,20 +23,26 @@ class ExpRetry
|
|
19
23
|
retry
|
20
24
|
end
|
21
25
|
|
22
|
-
|
26
|
+
private
|
23
27
|
|
24
28
|
def check(exception)
|
25
|
-
|
29
|
+
raise(exception) unless retries.positive?
|
30
|
+
|
31
|
+
decrement
|
32
|
+
delay
|
26
33
|
end
|
27
34
|
|
28
|
-
def
|
29
|
-
@
|
30
|
-
@retried += 1
|
35
|
+
def decrement
|
36
|
+
@retries -= 1
|
31
37
|
end
|
32
38
|
|
33
|
-
def delay
|
34
|
-
output = "*** #{exception}. Retrying in #{
|
39
|
+
def delay
|
40
|
+
output = "*** #{@exception}. Retrying in #{time} seconds..."
|
35
41
|
@verbose ? puts(output) : print('-')
|
36
|
-
sleep
|
42
|
+
sleep time
|
43
|
+
end
|
44
|
+
|
45
|
+
def time
|
46
|
+
2**retries * (@wait.to_f / 1000)
|
37
47
|
end
|
38
48
|
end
|
data/spec/.rubocop.yml
ADDED
data/spec/lib/exp_retry_spec.rb
CHANGED
@@ -3,23 +3,25 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
RSpec.describe ExpRetry do
|
6
|
-
|
7
|
-
|
6
|
+
let(:wait) { 10 }
|
7
|
+
|
8
|
+
it 'allows normal execution' do
|
9
|
+
r = described_class.new.call { 'Something' }
|
8
10
|
|
9
11
|
expect(r).to eql('Something')
|
10
12
|
end
|
11
13
|
|
12
|
-
it '
|
13
|
-
r =
|
14
|
+
it 'allows normal execution using for' do
|
15
|
+
r = described_class.for { 'Something' }
|
14
16
|
|
15
17
|
expect(r).to eql('Something')
|
16
18
|
end
|
17
19
|
|
18
|
-
it '
|
19
|
-
|
20
|
-
r =
|
21
|
-
if
|
22
|
-
|
20
|
+
it 'allows normal execution with a single failure' do
|
21
|
+
fail_once = true
|
22
|
+
r = described_class.new(wait: wait).call do
|
23
|
+
if fail_once
|
24
|
+
fail_once = false
|
23
25
|
raise 'Fail once'
|
24
26
|
end
|
25
27
|
'Something'
|
@@ -28,11 +30,11 @@ RSpec.describe ExpRetry do
|
|
28
30
|
expect(r).to eql('Something')
|
29
31
|
end
|
30
32
|
|
31
|
-
it '
|
32
|
-
|
33
|
-
r =
|
34
|
-
if
|
35
|
-
|
33
|
+
it 'allows normal execution with multiple exceptions' do
|
34
|
+
fail_once = true
|
35
|
+
r = described_class.new(exception: [RuntimeError, ArgumentError], wait: wait).call do
|
36
|
+
if fail_once
|
37
|
+
fail_once = false
|
36
38
|
raise 'Fail once again'
|
37
39
|
end
|
38
40
|
'Something'
|
@@ -41,21 +43,21 @@ RSpec.describe ExpRetry do
|
|
41
43
|
expect(r).to eql('Something')
|
42
44
|
end
|
43
45
|
|
44
|
-
it '
|
46
|
+
it 'raises after too many retries' do
|
45
47
|
expect do
|
46
|
-
|
48
|
+
described_class.new(wait: wait).call { raise 'Fail forever' }
|
47
49
|
end.to raise_error(RuntimeError, 'Fail forever')
|
48
50
|
end
|
49
51
|
|
50
|
-
it '
|
52
|
+
it 'raises after too many retries with a specified exception class' do
|
51
53
|
expect do
|
52
|
-
|
54
|
+
described_class.new(exception: RuntimeError, wait: wait).call { raise 'Fail forever again' }
|
53
55
|
end.to raise_error(RuntimeError, 'Fail forever again')
|
54
56
|
end
|
55
57
|
|
56
|
-
it '
|
58
|
+
it 'does not retry for a non-matching exception class' do
|
57
59
|
expect do
|
58
|
-
|
60
|
+
described_class.new(exception: RuntimeError, wait: wait).call { raise ArgumentError }
|
59
61
|
end.to raise_error(ArgumentError)
|
60
62
|
end
|
61
63
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exp_retry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Fiander
|
@@ -77,6 +77,7 @@ extensions: []
|
|
77
77
|
extra_rdoc_files: []
|
78
78
|
files:
|
79
79
|
- ".gitignore"
|
80
|
+
- ".rubocop.yml"
|
80
81
|
- ".travis.yml"
|
81
82
|
- Gemfile
|
82
83
|
- Gemfile.lock
|
@@ -84,6 +85,7 @@ files:
|
|
84
85
|
- Readme.md
|
85
86
|
- exp-retry.gemspec
|
86
87
|
- lib/exp_retry.rb
|
88
|
+
- spec/.rubocop.yml
|
87
89
|
- spec/lib/exp_retry_spec.rb
|
88
90
|
- spec/spec_helper.rb
|
89
91
|
homepage: http://rubygems.org/gems/exp_retry
|
@@ -100,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
102
|
requirements:
|
101
103
|
- - "~>"
|
102
104
|
- !ruby/object:Gem::Version
|
103
|
-
version: '2.
|
105
|
+
version: '2.3'
|
104
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
107
|
requirements:
|
106
108
|
- - ">="
|