Class: Retries
- Inherits:
-
Object
- Object
- Retries
- Defined in:
- lib/robustserver.rb
Overview
Description
Counts retries ot something. If the retries are to often in a short time, you shouldn’t retry again.
Examples
Strings aren’t Integers and 2*“Text” will raise TypeError.
retries = Retry.new 5, 1 begin array_of_ints_and_some_strings.each do |i| puts 2*i end rescue TypeError retries.retry? and retry raise $! end
Retry.new( 10, 30).run( ConnectionLost) do try_to_connect_to_db try_query end
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#max ⇒ Object
Returns the value of attribute max.
-
#range ⇒ Object
Returns the value of attribute range.
Instance Method Summary collapse
-
#initialize(max = nil, range = nil) ⇒ Retries
constructor
max: How many retries in range-time are allowed maximal.
-
#retry? ⇒ Boolean
Counts retries on every call.
-
#run(ex = nil, &e) ⇒ Object
Automatical retrieing on raised exceptions in block.
Constructor Details
#initialize(max = nil, range = nil) ⇒ Retries
max: How many retries in range-time are allowed maximal. range: In which time-range are these retries are allowed
65 66 67 |
# File 'lib/robustserver.rb', line 65 def initialize max = nil, range = nil @max, @range, @count, @last = max || 10, range || 10, 0, Time.now end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
61 62 63 |
# File 'lib/robustserver.rb', line 61 def count @count end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
61 62 63 |
# File 'lib/robustserver.rb', line 61 def last @last end |
#max ⇒ Object
Returns the value of attribute max.
60 61 62 |
# File 'lib/robustserver.rb', line 60 def max @max end |
#range ⇒ Object
Returns the value of attribute range.
60 61 62 |
# File 'lib/robustserver.rb', line 60 def range @range end |
Instance Method Details
#retry? ⇒ Boolean
Counts retries on every call. If these retries are to often - max times in range - it will return false else true. Now you can say: “I give up, to many retries, it seems it doesn’t work.”
73 74 75 76 77 |
# File 'lib/robustserver.rb', line 73 def retry? @count = @last + @range > Time.now ? @count + 1 : 1 @last = Time.now @count < @max end |
#run(ex = nil, &e) ⇒ Object
Automatical retrieing on raised exceptions in block. ex: Your expected Esception you will rescue. Default: Object, so realy everything.
Example: Retries.new( 10, 30).run ArgumentError do something_do_which_could_raise_exception ArgumentError end
This will retry maximal 10 times in 30 seconds to Call this block. But only rescues ArgumentError! Every other Error it will ignore and throws Exception. No retry.
87 88 89 90 91 92 93 |
# File 'lib/robustserver.rb', line 87 def run ex = nil, &e ex ||= Object begin e.call *args rescue ex retries.retry? and retry end end |