blob: 21b1def36fbdf43e0dade2fcd2145e35ff2b154b (
plain)
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  | 
require 'result'
class RetryService
  Retry = Class.new(RuntimeError)
  # @param@ delays:
  # An array of delays that are used to retry after a sleep of the indicated
  # value in case of failed exceutions.
  # Once this array is exhausted the executen fails permanently
  #
  # @param@ rescue_from:
  # During execution all the excpetions from this array +plus RetryService::Retry+ are rescued from and
  # trigger just another retry after a `sleep` as indicated above.
  #
  # @param@ block:
  # This optional code is excuted before each retry, it is passed the result of the failed attempt, thus
  # an `Exception` and the number of execution already tried.
  def initialize( delays: [], rescue_from: [], &blk )
    @intervals             = delays
    @registered_exceptions = Array(rescue_from) << Retry
    @failure_callback      = blk
  end
  # @param@ blk:
  # The code to be executed it will be retried goverened by the `delay` passed into the initializer
  # as described there in case it fails with one of the predefined exceptions or `RetryService::Retry`
  #
  # Eventually it will return a `Result` object.
  def execute &blk
    result = execute_protected blk
    return result if result.ok?
    @intervals.each_with_index do | interval, retry_count |
      sleep interval
      @failure_callback.try(:call, result.value, retry_count + 1)
      result = execute_protected blk
      return result if result.ok?
    end
    result
  end
  private
  def execute_protected blk
    Result.ok(blk.())
  rescue Exception => e
    if @registered_exceptions.any?{ |re| e.is_a? re }
      Result.error(e)
    else
      raise
    end
  end
end
  |