tries 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +7 -5
- data/Guardfile +1 -1
- data/README.md +20 -7
- data/lib/tries.rb +10 -6
- data/lib/tries/version.rb +1 -1
- data/spec/spec_helper.rb +0 -1
- data/spec/tries/tries_spec.rb +16 -16
- data/tries.gemspec +5 -5
- metadata +24 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d193d0d7a484d18a5547a11721670dbf6f84bdd9fafcf6969ec98f4fc114c6ac
|
4
|
+
data.tar.gz: faae79f6f420ff3a2d4a08aa5c2daadb78fc71ddb37f82feaf22c7e4b29f4ef1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91a392538f78b1524f258e7890b6d0b26890f663d12decf2477f6f33940a91dd8e03a8f0d767580baa0448be5c0b832441f5eef8bde3a29aa7021c90ee6839ae
|
7
|
+
data.tar.gz: 13695ccfd9c78d735a3b95971cafe23c909e1a983be8a3ea4d1344ba5132c0f29e453f8fc10468368fa64ba286e7d6f487481db5f324ae041d8d9bec21396d75
|
data/.travis.yml
CHANGED
data/Guardfile
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
# Tries
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/tries.png)](http://badge.fury.io/rb/tries)
|
4
|
-
[![Build Status](https://secure.travis-ci.org/
|
5
|
-
[![Dependency Status](https://gemnasium.com/krautcomputing/tries.png)](https://gemnasium.com/krautcomputing/tries)
|
6
|
-
[![Code Climate](https://codeclimate.com/github/krautcomputing/tries.png)](https://codeclimate.com/github/krautcomputing/tries)
|
4
|
+
[![Build Status](https://secure.travis-ci.org/manuelmeurer/tries.png)](http://travis-ci.org/manuelmeurer/tries)
|
7
5
|
|
8
6
|
Solidify your code and retry on petty exceptions.
|
9
7
|
|
@@ -11,8 +9,6 @@ Tries lets you retry a block of code multiple times, which is convenient for exa
|
|
11
9
|
|
12
10
|
You can specify exactly how often the block of code is retried and which exceptions are caught.
|
13
11
|
|
14
|
-
Read the accompanying [blog post](http://www.krautcomputing.com/blog/2012/12/19/new-gem-tries/).
|
15
|
-
|
16
12
|
## Requirements
|
17
13
|
|
18
14
|
Ruby >= 1.9.2
|
@@ -54,8 +50,8 @@ def method_that_raises_exception
|
|
54
50
|
puts "Counter is #{@error_counter}"
|
55
51
|
|
56
52
|
case @error_counter
|
57
|
-
when 1 then raise FooError
|
58
|
-
when 2 then raise FooError
|
53
|
+
when 1 then raise FooError, 'retry'
|
54
|
+
when 2 then raise FooError, 'retry'
|
59
55
|
when 3 then raise BarError
|
60
56
|
when 4 then raise StandardError
|
61
57
|
end
|
@@ -106,6 +102,19 @@ end
|
|
106
102
|
# => StandardError
|
107
103
|
```
|
108
104
|
|
105
|
+
### Rescue error on condition
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
3.tries if: ->(error) { error.message == 'retry' } do
|
109
|
+
method_that_raises_exception
|
110
|
+
end
|
111
|
+
|
112
|
+
# => Counter is 1
|
113
|
+
# => Counter is 2
|
114
|
+
# => Counter is 3
|
115
|
+
# => BarError
|
116
|
+
```
|
117
|
+
|
109
118
|
### Delay execution after error
|
110
119
|
|
111
120
|
`delay` is in seconds, fractions are possible
|
@@ -231,3 +240,7 @@ end
|
|
231
240
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
232
241
|
4. Push to the branch (`git push origin my-new-feature`)
|
233
242
|
5. Create new Pull Request
|
243
|
+
|
244
|
+
## Support
|
245
|
+
|
246
|
+
If you like this project, consider [buying me a coffee](https://www.buymeacoffee.com/279lcDtbF)! :)
|
data/lib/tries.rb
CHANGED
@@ -15,13 +15,16 @@ class Integer
|
|
15
15
|
exception_classes = Array(options[:on] || StandardError)
|
16
16
|
delay = options[:delay]
|
17
17
|
incremental = options[:incremental]
|
18
|
+
condition = options[:if]
|
18
19
|
|
19
20
|
begin
|
20
21
|
return yield
|
21
22
|
rescue *exception_classes => exception
|
23
|
+
raise exception if condition && !condition.call(exception)
|
22
24
|
next_delay = calculate_delay(delay, attempts, incremental) if delay
|
23
|
-
Tries.configuration.on_error
|
24
|
-
|
25
|
+
[Tries.configuration.on_error, options[:on_error]].compact.each do |on_error_callback|
|
26
|
+
on_error_callback.call(exception, attempts, next_delay)
|
27
|
+
end
|
25
28
|
Kernel.sleep next_delay if delay
|
26
29
|
retry if (attempts += 1) <= self
|
27
30
|
end
|
@@ -30,8 +33,9 @@ class Integer
|
|
30
33
|
end
|
31
34
|
|
32
35
|
private
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
|
37
|
+
def calculate_delay(delay, attempts, incremental)
|
38
|
+
return delay unless incremental
|
39
|
+
(delay * attempts * 100).round.to_f / 100
|
40
|
+
end
|
37
41
|
end
|
data/lib/tries/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/tries/tries_spec.rb
CHANGED
@@ -64,7 +64,7 @@ describe Tries do
|
|
64
64
|
|
65
65
|
context 'static delay' do
|
66
66
|
it 'sleeps the specified delay' do
|
67
|
-
Kernel.
|
67
|
+
expect(Kernel).to receive(:sleep).with(0.1).exactly(2).times
|
68
68
|
|
69
69
|
begin
|
70
70
|
3.tries on: FooError, delay: delay do
|
@@ -77,9 +77,9 @@ describe Tries do
|
|
77
77
|
|
78
78
|
context 'incremental delay' do
|
79
79
|
it 'sleeps incrementally' do
|
80
|
-
Kernel.
|
81
|
-
Kernel.
|
82
|
-
Kernel.
|
80
|
+
expect(Kernel).to receive(:sleep).with(0.1).ordered
|
81
|
+
expect(Kernel).to receive(:sleep).with(0.2).ordered
|
82
|
+
expect(Kernel).to receive(:sleep).with(0.3).ordered
|
83
83
|
|
84
84
|
begin
|
85
85
|
3.tries on: [FooError, BarError], delay: delay, incremental: true do
|
@@ -98,9 +98,9 @@ describe Tries do
|
|
98
98
|
Tries.configure do |config|
|
99
99
|
config.on_error = global_on_error
|
100
100
|
end
|
101
|
-
global_on_error.
|
102
|
-
global_on_error.
|
103
|
-
global_on_error.
|
101
|
+
expect(global_on_error).to receive(:call).with(an_instance_of(FooError), 1, 0.1).ordered
|
102
|
+
expect(global_on_error).to receive(:call).with(an_instance_of(FooError), 2, 0.2).ordered
|
103
|
+
expect(global_on_error).to receive(:call).with(an_instance_of(BarError), 3, 0.3).ordered
|
104
104
|
begin
|
105
105
|
3.tries on: [FooError, BarError], delay: 0.1, incremental: true do
|
106
106
|
raise_foo_foo_bar_bar_standard
|
@@ -113,9 +113,9 @@ describe Tries do
|
|
113
113
|
context 'when a local callback is set' do
|
114
114
|
it 'calls the local callback with the correct parameters' do
|
115
115
|
local_on_error = Proc.new {}
|
116
|
-
local_on_error.
|
117
|
-
local_on_error.
|
118
|
-
local_on_error.
|
116
|
+
expect(local_on_error).to receive(:call).with(an_instance_of(FooError), 1, 0.1).ordered
|
117
|
+
expect(local_on_error).to receive(:call).with(an_instance_of(FooError), 2, 0.2).ordered
|
118
|
+
expect(local_on_error).to receive(:call).with(an_instance_of(BarError), 3, 0.3).ordered
|
119
119
|
begin
|
120
120
|
3.tries on: [FooError, BarError], delay: 0.1, incremental: true, on_error: local_on_error do
|
121
121
|
raise_foo_foo_bar_bar_standard
|
@@ -128,12 +128,12 @@ describe Tries do
|
|
128
128
|
context 'when both a global and a local callback are set' do
|
129
129
|
it 'calls both callbacks with the correct parameters in the correct order' do
|
130
130
|
local_on_error, global_on_error = Proc.new {}, Proc.new {}
|
131
|
-
global_on_error.
|
132
|
-
local_on_error.
|
133
|
-
global_on_error.
|
134
|
-
local_on_error.
|
135
|
-
global_on_error.
|
136
|
-
local_on_error.
|
131
|
+
expect(global_on_error).to receive(:call).with(an_instance_of(FooError), 1, 0.1).ordered
|
132
|
+
expect(local_on_error).to receive(:call).with(an_instance_of(FooError), 1, 0.1).ordered
|
133
|
+
expect(global_on_error).to receive(:call).with(an_instance_of(FooError), 2, 0.2).ordered
|
134
|
+
expect(local_on_error).to receive(:call).with(an_instance_of(FooError), 2, 0.2).ordered
|
135
|
+
expect(global_on_error).to receive(:call).with(an_instance_of(BarError), 3, 0.3).ordered
|
136
|
+
expect(local_on_error).to receive(:call).with(an_instance_of(BarError), 3, 0.3).ordered
|
137
137
|
Tries.configure do |config|
|
138
138
|
config.on_error = global_on_error
|
139
139
|
end
|
data/tries.gemspec
CHANGED
@@ -9,11 +9,11 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.name = 'tries'
|
10
10
|
gem.version = Tries::VERSION
|
11
11
|
gem.platform = Gem::Platform::RUBY
|
12
|
-
gem.
|
12
|
+
gem.author = 'Manuel Meurer'
|
13
13
|
gem.email = 'manuel@krautcomputing.com'
|
14
14
|
gem.summary = 'Solidify your code and retry on petty exceptions'
|
15
15
|
gem.description = 'Solidify your code and retry on petty exceptions'
|
16
|
-
gem.homepage = '
|
16
|
+
gem.homepage = ''
|
17
17
|
gem.license = 'MIT'
|
18
18
|
|
19
19
|
gem.files = `git ls-files`.split($/)
|
@@ -22,9 +22,9 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.require_paths = ['lib']
|
23
23
|
|
24
24
|
gem.add_development_dependency 'rake', '>= 0.9.0'
|
25
|
-
gem.add_development_dependency 'rspec', '~>
|
26
|
-
gem.add_development_dependency 'rb-fsevent', '~> 0.9
|
27
|
-
gem.add_development_dependency 'guard-rspec', '~> 2
|
25
|
+
gem.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
gem.add_development_dependency 'rb-fsevent', '~> 0.9'
|
27
|
+
gem.add_development_dependency 'guard-rspec', '~> 4.2'
|
28
28
|
# Listen >= 2.0.0 only works with Ruby >= 1.9.3
|
29
29
|
gem.add_development_dependency 'listen', '< 2.0.0' if RUBY_VERSION < '1.9.3'
|
30
30
|
gem.add_runtime_dependency 'gem_config', '~> 0.2'
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Meurer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 0.9.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.9.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '3.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '3.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rb-fsevent
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.9
|
47
|
+
version: '0.9'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.9
|
54
|
+
version: '0.9'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: guard-rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 2
|
61
|
+
version: '4.2'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 2
|
68
|
+
version: '4.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: gem_config
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0.2'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.2'
|
83
83
|
description: Solidify your code and retry on petty exceptions
|
@@ -86,8 +86,8 @@ executables: []
|
|
86
86
|
extensions: []
|
87
87
|
extra_rdoc_files: []
|
88
88
|
files:
|
89
|
-
- .gitignore
|
90
|
-
- .travis.yml
|
89
|
+
- ".gitignore"
|
90
|
+
- ".travis.yml"
|
91
91
|
- Gemfile
|
92
92
|
- Guardfile
|
93
93
|
- LICENSE.txt
|
@@ -98,7 +98,7 @@ files:
|
|
98
98
|
- spec/spec_helper.rb
|
99
99
|
- spec/tries/tries_spec.rb
|
100
100
|
- tries.gemspec
|
101
|
-
homepage:
|
101
|
+
homepage: ''
|
102
102
|
licenses:
|
103
103
|
- MIT
|
104
104
|
metadata: {}
|
@@ -108,21 +108,19 @@ require_paths:
|
|
108
108
|
- lib
|
109
109
|
required_ruby_version: !ruby/object:Gem::Requirement
|
110
110
|
requirements:
|
111
|
-
- -
|
111
|
+
- - ">="
|
112
112
|
- !ruby/object:Gem::Version
|
113
113
|
version: '0'
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
|
-
- -
|
116
|
+
- - ">="
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: '0'
|
119
119
|
requirements: []
|
120
|
-
|
121
|
-
rubygems_version: 2.2.0
|
120
|
+
rubygems_version: 3.1.2
|
122
121
|
signing_key:
|
123
122
|
specification_version: 4
|
124
123
|
summary: Solidify your code and retry on petty exceptions
|
125
124
|
test_files:
|
126
125
|
- spec/spec_helper.rb
|
127
126
|
- spec/tries/tries_spec.rb
|
128
|
-
has_rdoc:
|