take2 0.0.0 → 0.0.1
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/CHANGELOG.md +5 -0
- data/Gemfile +0 -2
- data/README.md +1 -1
- data/lib/take2.rb +76 -13
- data/lib/take2/version.rb +1 -1
- data/spec/take2_spec.rb +9 -9
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8ec07213b11c7223692259cee1c4fae9af86069
|
4
|
+
data.tar.gz: e3050a3a0aac4b6454193e2051a899e4b2b52635
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73b036448036c9903c5cae7243ff7dfe1df679f04f5482ac413a40aeb3587652fd7f66ac457be8feb624706bf4534e73cede712935e47ffb7131f4dc89fe1cac
|
7
|
+
data.tar.gz: 83cfcac40ee6e3c24f546091a016ab93b6a72527a9a66a3f713df9a7376625cd29c1d339fc876597c24e0832e6ffb08a2364d7b271087b57ac392a39597fc04a
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1 +1 @@
|
|
1
|
-
#
|
1
|
+
# Work in process
|
data/lib/take2.rb
CHANGED
@@ -29,6 +29,30 @@ module Take2
|
|
29
29
|
|
30
30
|
module InstanceMethods
|
31
31
|
|
32
|
+
# Yields a block and retries on retriable errors n times.
|
33
|
+
# The raised error could be the defined retriable or it child.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
# class KatorzaService
|
37
|
+
# include Take2
|
38
|
+
#
|
39
|
+
# number_of_retries 3
|
40
|
+
# retriable_errors Net::HTTPRetriableError, Net::HTTPServerError
|
41
|
+
# retriable_condition proc { |error| response_status(error.response) < 500 }
|
42
|
+
# on_retry proc do |error, tries|
|
43
|
+
# puts "#{self.class.name} - Retrying.. #{tries} of #{self.class.retriable_configuration[:retries]} (#{error})"
|
44
|
+
# end
|
45
|
+
# sleep_before_retry 3
|
46
|
+
#
|
47
|
+
# def give_me_food
|
48
|
+
# call_api_with_retry do
|
49
|
+
# # Some logic that might raise..
|
50
|
+
# # If it will raise retriable, magic happens.
|
51
|
+
# # If not the original error re raised
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# end
|
32
56
|
def call_api_with_retry
|
33
57
|
config = self.class.retriable_configuration
|
34
58
|
tries ||= config[:retries]
|
@@ -42,41 +66,85 @@ module Take2
|
|
42
66
|
tries -= 1
|
43
67
|
retry
|
44
68
|
end
|
45
|
-
end
|
46
|
-
log_error e
|
69
|
+
end
|
47
70
|
raise e
|
48
71
|
end
|
49
72
|
end
|
50
|
-
|
51
|
-
def log_error(error)
|
52
|
-
# Overrider this method in the includer
|
53
|
-
true
|
54
|
-
end
|
55
73
|
|
56
74
|
end
|
57
75
|
|
58
76
|
module ClassMethods
|
59
|
-
|
77
|
+
# Sets number of retries.
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
# class KatorzaService
|
81
|
+
# include Take2
|
82
|
+
# number_of_retries 3
|
83
|
+
# end
|
84
|
+
# Arguments:
|
85
|
+
# num: Positive integer
|
60
86
|
def number_of_retries(num)
|
87
|
+
raise ArgumentError, 'Must be positive Integer' unless num.is_a?(Integer) && num.positive?
|
61
88
|
self.retries = num
|
62
89
|
end
|
63
90
|
|
91
|
+
# Sets list of errors on which the block will retry.
|
92
|
+
#
|
93
|
+
# Example:
|
94
|
+
# class KatorzaService
|
95
|
+
# include Take2
|
96
|
+
# retriable_errors Net::HTTPRetriableError, Errno::ECONNRESET
|
97
|
+
# end
|
98
|
+
# Arguments:
|
99
|
+
# errors: List of retiable errors
|
64
100
|
def retriable_errors(*errors)
|
65
101
|
self.retriable = errors
|
66
102
|
end
|
67
103
|
|
104
|
+
# Sets condition for retry attempt.
|
105
|
+
# If set, it MUST result to +false+ with number left retries greater that zero in order to retry.
|
106
|
+
#
|
107
|
+
# Example:
|
108
|
+
# class KatorzaService
|
109
|
+
# include Take2
|
110
|
+
# retriable_condition proc { |error| error.response.status_code < 500 }
|
111
|
+
# end
|
112
|
+
# Arguments:
|
113
|
+
# proc: Proc. The +proc+ called by default with the raised error argument
|
68
114
|
def retriable_condition(proc)
|
115
|
+
raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
|
69
116
|
self.retry_condition_proc = proc
|
70
117
|
end
|
71
118
|
|
119
|
+
# Defines a proc that is called *before* retry attempt.
|
120
|
+
#
|
121
|
+
# Example:
|
122
|
+
# class KatorzaService
|
123
|
+
# include Take2
|
124
|
+
# on_retry proc { |error, tries| puts "Retrying.. #{tries} of #{self.class.retriable_configuration[:retries]}" }
|
125
|
+
# end
|
126
|
+
# Arguments:
|
127
|
+
# proc: Proc. The +proc+ called by default with the raised error and number of left retries.
|
72
128
|
def on_retry(proc)
|
129
|
+
raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
|
73
130
|
self.retry_proc = proc
|
74
131
|
end
|
75
132
|
|
133
|
+
# Sets number of seconds to sleep before next retry.
|
134
|
+
#
|
135
|
+
# Example:
|
136
|
+
# class KatorzaService
|
137
|
+
# include Take2
|
138
|
+
# sleep_before_retry 1.5
|
139
|
+
# end
|
140
|
+
# Arguments:
|
141
|
+
# seconds: Positive number.
|
76
142
|
def sleep_before_retry(seconds)
|
143
|
+
raise ArgumentError, 'Must be positive numer' unless (seconds.is_a?(Integer) || seconds.is_a?(Float)) && seconds.positive?
|
77
144
|
self.time_to_sleep = seconds
|
78
145
|
end
|
79
146
|
|
147
|
+
# Exposes current class configuration
|
80
148
|
def retriable_configuration
|
81
149
|
Take2::Configuration::CONFIG_ATTRS.each_with_object({}) do |key, hash|
|
82
150
|
hash[key] = send(key)
|
@@ -99,11 +167,6 @@ module Take2
|
|
99
167
|
response.status_code if response.respond_to? :status_code
|
100
168
|
end
|
101
169
|
|
102
|
-
def log_client_error(error, tries)
|
103
|
-
# Override this method in the includer
|
104
|
-
true
|
105
|
-
end
|
106
|
-
|
107
170
|
end
|
108
171
|
|
109
172
|
end
|
data/lib/take2/version.rb
CHANGED
data/spec/take2_spec.rb
CHANGED
@@ -57,7 +57,7 @@ RSpec.describe Take2 do
|
|
57
57
|
describe '#retriable_errors' do
|
58
58
|
|
59
59
|
it 'sets the :retriable_errors attribute' do
|
60
|
-
retriables = IOError
|
60
|
+
retriables = IOError
|
61
61
|
klass.retriable_errors retriables
|
62
62
|
expect(subject[:retriable]).to eql [retriables]
|
63
63
|
end
|
@@ -124,10 +124,10 @@ RSpec.describe Take2 do
|
|
124
124
|
end.to change{@tries}.from(0).to(1)
|
125
125
|
end
|
126
126
|
|
127
|
-
it 'logs the error' do
|
128
|
-
|
129
|
-
|
130
|
-
end
|
127
|
+
# it 'logs the error' do
|
128
|
+
# expect(object).to receive(:log_error).with(error)
|
129
|
+
# object.call_api_with_retry { wrath_the_gods_with error } rescue nil
|
130
|
+
# end
|
131
131
|
|
132
132
|
end
|
133
133
|
|
@@ -158,10 +158,10 @@ RSpec.describe Take2 do
|
|
158
158
|
object.call_api_with_retry { wrath_the_gods_with retriable_error } rescue nil
|
159
159
|
end
|
160
160
|
|
161
|
-
it 'logs the error' do
|
162
|
-
|
163
|
-
|
164
|
-
end
|
161
|
+
# it 'logs the error' do
|
162
|
+
# expect(object).to receive(:log_error).with(retriable_error)
|
163
|
+
# object.call_api_with_retry { wrath_the_gods_with retriable_error } rescue nil
|
164
|
+
# end
|
165
165
|
|
166
166
|
it 're raises the original error' do
|
167
167
|
expect do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: take2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Magids
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- ".gitignore"
|
22
|
+
- CHANGELOG.md
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|