exp_retry 0.0.11 → 0.0.12

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: 9850572e1bc83790a190b3a1dceefe49aa5347446baff1dedaba4153e9db44a5
4
- data.tar.gz: 49ec726f23195038cffcec915327332376143f7fd8a3174d52f654959ade8d0b
3
+ metadata.gz: dcf20e1954c741739c256c324c0511bd503d470177bddea6e440954df9b8b0e3
4
+ data.tar.gz: a698f18df11aea017d8a38c86c9840637008df8d0f94e7ac36cd50630a8eb3b1
5
5
  SHA512:
6
- metadata.gz: 32a086e6cf019fd5ff1bacffbac83158b7dded1c50ace23933fbcc1a65090adba06008fecdf798a44a6c5a70eee9778e3e519d3c15732234a870277c0989a4f3
7
- data.tar.gz: 4ff08790d9a50336a95eea5fe206cf9fcd83fbe6dd81a624ab27a1baee085b680d1c2ff03a137e0c7ece69b65a0fdc22d1f89367739ca5c7b4e6cc80a82fd5e1
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
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- exp_retry (0.0.11)
4
+ exp_retry (0.0.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
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.11'
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.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
- private
26
+ private
23
27
 
24
28
  def check(exception)
25
- retried > @retries ? raise(exception) : delay(exception)
29
+ raise(exception) unless retries.positive?
30
+
31
+ decrement
32
+ delay
26
33
  end
27
34
 
28
- def retried
29
- @retried ||= 0
30
- @retried += 1
35
+ def decrement
36
+ @retries -= 1
31
37
  end
32
38
 
33
- def delay(exception)
34
- output = "*** #{exception}. Retrying in #{2**@retried} seconds..."
39
+ def delay
40
+ output = "*** #{@exception}. Retrying in #{time} seconds..."
35
41
  @verbose ? puts(output) : print('-')
36
- sleep 2**@retried
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
@@ -0,0 +1,7 @@
1
+ Metrics/BlockLength:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ Max: 100
5
+
6
+ RSpec/ExampleLength:
7
+ Enabled: false
@@ -3,23 +3,25 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe ExpRetry do
6
- it 'should allow normal execution' do
7
- r = ExpRetry.new.call { 'Something' }
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 'should allow normal execution using for' do
13
- r = ExpRetry.for { 'Something' }
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 'should allow normal execution with a single failure' do
19
- @fail_once = true
20
- r = ExpRetry.new.call do
21
- if @fail_once
22
- @fail_once = false
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 'should allow normal execution with multiple exceptions' do
32
- @fail_once = true
33
- r = ExpRetry.new(exception: [RuntimeError, ArgumentError]).call do
34
- if @fail_once
35
- @fail_once = false
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 'should raise after too many retries' do
46
+ it 'raises after too many retries' do
45
47
  expect do
46
- ExpRetry.new.call { raise 'Fail forever' }
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 'should raise after too many retries with a specified exception class' do
52
+ it 'raises after too many retries with a specified exception class' do
51
53
  expect do
52
- ExpRetry.new(exception: RuntimeError).call { raise 'Fail forever again' }
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 'should not retry for a non-matching exception class' do
58
+ it 'does not retry for a non-matching exception class' do
57
59
  expect do
58
- ExpRetry.new(exception: RuntimeError).call { raise ArgumentError }
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/setup'
2
4
  Bundler.setup
3
5
  require 'simplecov'
@@ -7,5 +9,4 @@ SimpleCov.minimum_coverage(100)
7
9
  require 'exp_retry'
8
10
 
9
11
  RSpec.configure do |config|
10
- #
11
12
  end
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.11
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.2'
105
+ version: '2.3'
104
106
  required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  requirements:
106
108
  - - ">="