Class: MobyUtil::Retryable
- Defined in:
- lib/tdriver/util/common/retryable.rb
Class Method Summary collapse
-
.sleep_retry_interval(time) ⇒ Object
TODO: document me.
-
.until(options = {}, &block) ⇒ Object
- Function to retry code block until timeout expires if exception raises == params options
-
Hash of options :timeout Timeout until fail.
-
.while(options = {}, &block) ⇒ Object
- Function to retry code block for x times if exception raises == params options
-
Hash of options :tries Number of tries to perform.
Class Method Details
.sleep_retry_interval(time) ⇒ Object
TODO: document me
25 26 27 28 29 |
# File 'lib/tdriver/util/common/retryable.rb', line 25 def self.sleep_retry_interval( time ) sleep( time ) end |
.until(options = {}, &block) ⇒ Object
Function to retry code block until timeout expires if exception raises
params
- options
-
Hash of options
:timeout Timeout until fail. Default is 0
:interval Timeout between retry. Default is 0
:exception Retry if given type of exception occures. Default is Exception (any error)
returns
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/tdriver/util/common/retryable.rb', line 108 def self.until( = {}, &block ) # default options .default_values( :timeout => 0, :interval => 0, :exception => Exception ) # store start time start_time = Time.now # attempt number attempt = 0 # number of block arguments _block_arity = block.arity if _block_arity > 1 _block_arity = 2 elsif _block_arity < 1 _block_arity = 0 end # last exception _exception = nil # default _arguments = [] begin case _block_arity when 1 arguments = [ attempt ] when 2 arguments = [ attempt, _exception ] end # execute block yield( *arguments ) rescue *[ :exception ] _exception = $! if (Time.now - start_time) <= [ :timeout ] && ![ *[ :unless ] ].include?( $!.class ) sleep_retry_interval( [ :interval ] ) if [ :interval ] > 0 attempt += 1 retry end # raise exception with correct exception backtrace raise $! end end |
.while(options = {}, &block) ⇒ Object
Function to retry code block for x times if exception raises
params
- options
-
Hash of options
:tries Number of tries to perform. Default is 1
:interval Timeout between retry. Default is 0
:exception Retry if given type of exception occures. Default is Exception (any error)
returns
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tdriver/util/common/retryable.rb', line 38 def self.while( = {}, &block ) # default options .default_values( :tries => 1, :interval => 0, :exception => Exception ) attempt = 1 # number of block arguments _block_arity = block.arity if _block_arity > 1 _block_arity = 2 elsif _block_arity < 1 _block_arity = 0 end # last exception _exception = nil # default _arguments = [] begin case _block_arity when 1 arguments = [ attempt ] when 2 arguments = [ attempt, _exception ] end # yield given block and pass attempt number as parameter yield( *arguments ) rescue *[ :exception ] _exception = $! if ( attempt < [ :tries ] ) && ![ *[ :unless ] ].include?( $!.class ) sleep_retry_interval( [ :interval ] ) if [ :interval ] > 0 attempt += 1 retry end # raise exception with correct exception backtrace raise $! end end |