try-until 0.4.0 → 0.4.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 +8 -8
- data/lib/try_until/repeatedly.rb +18 -11
- data/lib/try_until/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YmU5N2NkZTJlMjgxODcyZTZkZjM4YzM1MDQyMWY2NGE2M2YzNjZmZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjljNDU1NmFkZjgyNjFhNmVmZGFkNDFkNTUzZjdiYjcxYzJmYWEzNg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODgzMDIxMzY3ZWY3Mzc1OWI5MTdjYTQ3OWZlZjI0NGNjZmIzY2QzM2QwOWVm
|
10
|
+
ZWY5MDIyYTBiM2I3MWU4OTdiNTIyMDc3ZGQwY2E4OTczYjhiYWE0ZjExY2Rm
|
11
|
+
MzA2NzhkYmNiODlmMmVlNGNlNjhkY2Q4YWJhYjVhMTBiMjNhMTk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzgzZDBhNGM3MzBkMGFjYmVmNzM5NTg1MThhMzhmNjRhMDg4YjlmMmIwODIx
|
14
|
+
ZWM2ZmY2YTRjZGM1M2U2NDFkZjMyNzVlMzU0M2QzZTdiZDE1ZWZlZTQ0Yjk1
|
15
|
+
NWNkZWU3Zjg2NmIyZDJkYzdlMTFhN2VjOTM1NDdkZjVhMjY0OWM=
|
data/lib/try_until/repeatedly.rb
CHANGED
@@ -9,7 +9,7 @@ module TryUntil
|
|
9
9
|
# .rescues([ ArgumentError, IOError ])
|
10
10
|
# .stop_when(lambda { |response| JSON.parse(response.body)['id'] == 'some_id' })
|
11
11
|
# .log_to($stdout)
|
12
|
-
#
|
12
|
+
# .execute
|
13
13
|
#
|
14
14
|
# Not all of the above settings are required. These are the default values:
|
15
15
|
# attempts = 3
|
@@ -23,6 +23,7 @@ module TryUntil
|
|
23
23
|
|
24
24
|
def initialize(probe)
|
25
25
|
@probe = probe
|
26
|
+
defaults
|
26
27
|
end
|
27
28
|
|
28
29
|
def attempts(int_num)
|
@@ -60,19 +61,14 @@ module TryUntil
|
|
60
61
|
# In case of errors it will rescue those and continue, provided the type
|
61
62
|
# of error is among the ones defined in @rescues.
|
62
63
|
def execute
|
63
|
-
@attempts = 3 unless @attempts
|
64
|
-
@interval = 0 unless @interval
|
65
|
-
@delay = 0 unless @delay
|
66
|
-
@rescues = [] unless @rescues
|
67
|
-
@stop_when = lambda { |response| false } unless @stop_when
|
68
|
-
@log_to = NullPrinter.new unless @log_to
|
69
|
-
|
70
64
|
Kernel.sleep(@delay) if @delay > 0
|
71
65
|
count = 0
|
66
|
+
condition_met = false
|
72
67
|
while count < @attempts
|
73
68
|
begin
|
74
69
|
result = @probe.sample
|
75
70
|
if @stop_when.call(result)
|
71
|
+
condition_met = true
|
76
72
|
log_outcome(count, 'CONDITION_MET')
|
77
73
|
return result
|
78
74
|
end
|
@@ -81,8 +77,10 @@ module TryUntil
|
|
81
77
|
log_outcome(count, exception.class)
|
82
78
|
raise exception, "During final attempt (#{@attempts} configured) target returned #{exception}" if count + 1 == @attempts
|
83
79
|
ensure
|
84
|
-
|
85
|
-
|
80
|
+
unless condition_met
|
81
|
+
count += 1
|
82
|
+
Kernel.sleep @interval if @interval > 0
|
83
|
+
end
|
86
84
|
end
|
87
85
|
end
|
88
86
|
raise "After #{@attempts} attempts, the expected result was not returned!"
|
@@ -90,12 +88,21 @@ module TryUntil
|
|
90
88
|
|
91
89
|
def configuration
|
92
90
|
{ :probe => @probe.to_s, :attempts => @attempts, :interval => @interval,
|
93
|
-
:rescues => @rescues, :
|
91
|
+
:delay => @delay, :rescues => @rescues, :log_to => @log_to }
|
94
92
|
end
|
95
93
|
|
96
94
|
private
|
97
95
|
def log_outcome(count, outcome)
|
98
96
|
@log_to.printf("#{Time.new}|attempt ##{count + 1}|outcome: #{outcome}|#{@attempts - count - 1} attempts left\n")
|
99
97
|
end
|
98
|
+
|
99
|
+
def defaults
|
100
|
+
@attempts = 3 unless @attempts
|
101
|
+
@interval = 0 unless @interval
|
102
|
+
@delay = 0 unless @delay
|
103
|
+
@rescues = [] unless @rescues
|
104
|
+
@stop_when = lambda { |response| false } unless @stop_when
|
105
|
+
@log_to = NullPrinter.new unless @log_to
|
106
|
+
end
|
100
107
|
end
|
101
108
|
end
|
data/lib/try_until/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: try-until
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Markus Krogemann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|