Module: Garcon::Retry
Overview
Class methods that are added when you include Garcon::Retry
Instance Method Summary collapse
-
#patiently(wait = 8, delay = 0.1) {|Proc| ... } ⇒ Proc
Similar to ‘#poll`, `#patiently` also executes an arbitrary code block.
-
#poll(wait = 8, delay = 0.1) {|Proc| ... } ⇒ Proc
‘#poll` is a method for knowing when something is ready.
-
#retrier(opts = {}) {|Proc| ... } ⇒ Proc
Runs a code block, and retries it when an exception occurs.
Instance Method Details
#patiently(wait = 8, delay = 0.1) {|Proc| ... } ⇒ Proc
Similar to ‘#poll`, `#patiently` also executes an arbitrary code block. If the passed block runs without raising an error, execution proceeds normally. If an error is raised, the block is rerun after a brief delay, until the block can be run without exceptions. If exceptions continue to raise, `#patiently` gives up after a bit (default 8 seconds) by re-raising the most recent exception raised by the block.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/garcon/utility/retry.rb', line 220 def patiently(wait = 8, delay = 0.1) try_until = Time.now + wait failure = nil while Time.now < try_until do begin return yield rescue Exception => e failure = e sleep delay end end failure ? (raise failure) : (raise TimeoutError) end |
#poll(wait = 8, delay = 0.1) {|Proc| ... } ⇒ Proc
‘#poll` is a method for knowing when something is ready. When your block yields true, execution continues. When your block yields false, poll keeps trying until it gives up and raises an error.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/garcon/utility/retry.rb', line 175 def poll(wait = 8, delay = 0.1) try_until = Time.now + wait while Time.now < try_until do result = yield return result if result sleep delay end raise TimeoutError end |
#retrier(opts = {}) {|Proc| ... } ⇒ Proc
Runs a code block, and retries it when an exception occurs. It is configured using four optional parameters ‘:tries`, `:on`, `:sleep`, `:match`, `:ensure` and runs the passed block. Should an exception occur, it’ll retry for (n-1) times. Should the number of retries be reached without success, the last exception will be raised.
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 |
# File 'lib/garcon/utility/retry.rb', line 111 def retrier(opts = {}, &_block) tries = opts.fetch(:tries, 4) wait = opts.fetch(:sleep, 1) on = opts.fetch(:on, StandardError) match = opts.fetch(:match, /.*/) insure = opts.fetch(:ensure, Proc.new {}) retries = 0 retry_exception = nil begin yield retries, retry_exception rescue *[on] => e raise unless e. =~ match raise if retries + 1 >= tries begin sleep wait.respond_to?(:call) ? wait.call(retries) : wait rescue *[on] end retries += 1 retry_exception = exception retry ensure insure.call(retries) end end |