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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4f76c4e188cd73428d39ae170470fbcb9cd5fc0
4
- data.tar.gz: 86afb035db2b84b8cff826fc5c5480fa2fd073b2
3
+ metadata.gz: 10305ce860974756386078ebdb953dfeaff4dec3
4
+ data.tar.gz: b5009fc983d963ec0d5709ec2fe6f09d800155be
5
5
  SHA512:
6
- metadata.gz: 9d3f4ade2244163104ace00a2833427a5effacd67ac1195254c8f894935de58e5c38d1a8d45e789d1f5f9f7c03f840bf0598b6fc79e5bc27e7eac922f4e964d4
7
- data.tar.gz: 564f4fa5bd52f62f516e24c13461be359f7a5594af382f2afd6363d2d262e9987407ff11b94b8a76ce08d92b695cbe5863e822fc2f01c942025e1e5187e147f5
6
+ metadata.gz: c90f36ffc4a6c6d9ed9595e31f8c55ace4fac943d25a809497b3c6290d5691a9be03faa7fd27536e34b871f06d135619ec860e5a107939d97b365496c08ec976
7
+ data.tar.gz: 93167bc6d4b94a5791052a58e06567164427b38916e984189c987df7d2123451c1eeb4859461011e52422b861d809f0454ce022dcaa31cfdc369edb6d9e0a6c5
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
+ [![Gem Version](https://badge.fury.io/rb/patiently_try.svg)](https://badge.fury.io/rb/patiently_try)
2
+ [![Build Status](https://travis-ci.org/Ninigi/patiently_try.svg?branch=master)](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 ActiveSupport
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
- You can use the following options:
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 before retrying (in seconds)
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.
@@ -1,3 +1,3 @@
1
1
  module PatientlyTry
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
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 = opts[:retries] || 100
6
- wait = opts[:wait] || 0
7
- catch_errors = opts[:catch] || StandardError
8
- logging = opts[:logging].nil? ? true : opts[:logging]
9
- try = 0
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 *(Array(catch_errors)) => e
14
+ rescue *(catch_errors) => e
14
15
  try += 1
15
- puts "Failed with: #{e.inspect}" if logging
16
16
 
17
- raise e if try >= retries
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.1.1
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-04-19 00:00:00.000000000 Z
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.5.1
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.