patiently_try 0.1.1 → 0.2.0
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/README.md +22 -3
- data/lib/patiently_try/version.rb +1 -1
- data/lib/patiently_try.rb +33 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10305ce860974756386078ebdb953dfeaff4dec3
|
4
|
+
data.tar.gz: b5009fc983d963ec0d5709ec2fe6f09d800155be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c90f36ffc4a6c6d9ed9595e31f8c55ace4fac943d25a809497b3c6290d5691a9be03faa7fd27536e34b871f06d135619ec860e5a107939d97b365496c08ec976
|
7
|
+
data.tar.gz: 93167bc6d4b94a5791052a58e06567164427b38916e984189c987df7d2123451c1eeb4859461011e52422b861d809f0454ce022dcaa31cfdc369edb6d9e0a6c5
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
[](https://badge.fury.io/rb/patiently_try)
|
2
|
+
[](https://travis-ci.org/Ninigi/patiently_try)
|
3
|
+
|
1
4
|
# PatientlyTry
|
2
5
|
|
3
|
-
Just another DSL gem to surround your code with retry blocks. I use it with
|
6
|
+
Just another DSL gem to surround your code with retry blocks. I use it with ActiveResource
|
4
7
|
to retry stuff, but you can use it with every piece of code that might, or might not raise
|
5
8
|
an error that can magically resolve itself.
|
6
9
|
|
@@ -35,11 +38,14 @@ end
|
|
35
38
|
|
36
39
|
I would recommend to keep the retried code block as small as possible, to avoid unintentional retries.
|
37
40
|
|
38
|
-
|
41
|
+
## Options
|
42
|
+
|
43
|
+
The following options are available:
|
39
44
|
|
40
45
|
* `retries: 100` - The number of retries you want to attempt before giving up and reraising the error.
|
41
|
-
* `wait: 0` - How long you want to wait
|
46
|
+
* `wait: 0` - How long you want to wait between retries (in seconds).
|
42
47
|
* `catch: [StandardError]` - If you want only specific errors to be caught (value can be an array or a single error).
|
48
|
+
* `raise_if_caught: []` - If you want to exclude an error from being caught through inheritance.
|
43
49
|
* `logging: true` - If you do not want any output, set this to false.
|
44
50
|
|
45
51
|
```ruby
|
@@ -51,6 +57,19 @@ patiently_try retries: 2, wait: 1, catch: [Timeout::Error, Errno::ECONNREFUSED]
|
|
51
57
|
end
|
52
58
|
```
|
53
59
|
|
60
|
+
The `:catch` and `:raise_if_caught` can be used together to limit the amount of errors that will be retried.
|
61
|
+
If only `:raise_if_caught` is set, it will retry every `StandardError` except the ones specified.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
patiently_try raise_if_caught: [ArgumentError] do
|
65
|
+
# This will trigger retries, since NoMethodError is a StandardError
|
66
|
+
raise NoMethodError
|
67
|
+
|
68
|
+
# This will not trigger a retry and fail on first try
|
69
|
+
raise StandardError
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
54
73
|
## Contributing
|
55
74
|
|
56
75
|
Bug reports and pull requests are welcome on GitHub at https://github.com/Ninigi/patiently_try. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/patiently_try.rb
CHANGED
@@ -2,24 +2,48 @@ require "patiently_try/version"
|
|
2
2
|
|
3
3
|
module PatientlyTry
|
4
4
|
def patiently_try(opts = {})
|
5
|
-
retries
|
6
|
-
wait
|
7
|
-
catch_errors
|
8
|
-
|
9
|
-
|
5
|
+
retries = opts[:retries] || 100
|
6
|
+
wait = opts[:wait] || 0
|
7
|
+
catch_errors = Array(opts[:catch] || StandardError)
|
8
|
+
excluded_errors = Array(opts[:raise_if_caught])
|
9
|
+
logging = opts[:logging].nil? ? true : opts[:logging]
|
10
|
+
try = 0
|
10
11
|
|
11
12
|
begin
|
12
13
|
yield
|
13
|
-
rescue *(
|
14
|
+
rescue *(catch_errors) => e
|
14
15
|
try += 1
|
15
|
-
puts "Failed with: #{e.inspect}" if logging
|
16
16
|
|
17
|
-
|
17
|
+
_log_error if logging
|
18
|
+
|
19
|
+
if try >= retries || _rescued_but_excluded?(e, excluded_errors)
|
20
|
+
_log_backtrace if logging
|
21
|
+
raise e
|
22
|
+
end
|
23
|
+
|
24
|
+
_log_retry if logging
|
18
25
|
|
19
|
-
puts "Retrying (#{try}/#{retries})"
|
20
26
|
sleep wait if wait && wait > 0
|
21
27
|
|
22
28
|
retry
|
23
29
|
end
|
24
30
|
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def _log_retry
|
35
|
+
puts "Retrying (#{try}/#{retries})"
|
36
|
+
end
|
37
|
+
|
38
|
+
def _rescued_but_excluded?(e, excluded_errors)
|
39
|
+
excluded_errors.to_a.inject(false) { |excl, error| excl || e.is_a?(error) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def _log_error(e)
|
43
|
+
puts "Failed with: #{e.inspect}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def _log_backtrace(e)
|
47
|
+
puts e.backtrace.join("\n")
|
48
|
+
end
|
25
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: patiently_try
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabian Zitter
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.6.13
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Surrounds your code with a try catch block and will retry for X times.
|