Module: LogStash::Retryable
- Defined in:
- lib/logstash/util/retryable.rb
Instance Method Summary collapse
-
#retryable(options = {}, &block) ⇒ Object
execute retryable code block.
Instance Method Details
#retryable(options = {}, &block) ⇒ Object
execute retryable code block
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/logstash/util/retryable.rb', line 11 def retryable( = {}, &block) = { :tries => 1, :rescue => Exception, :on_retry => nil, :base_sleep => 1, :max_sleep => 1, }.merge() rescue_classes = Array([:rescue]) max_sleep_retry = Math.log2([:max_sleep] / [:base_sleep]) retry_count = 0 begin return yield(retry_count) rescue *rescue_classes => e raise e if [:tries] > 0 && retry_count >= [:tries] [:on_retry].call(retry_count + 1, e) if [:on_retry] # dont compute and maybe overflow exponent on too big a retry count seconds = retry_count < max_sleep_retry ? [:base_sleep] * (2 ** retry_count) : [:max_sleep] sleep(seconds) retry_count += 1 retry end end |