File: attempt.rb

package info (click to toggle)
ruby-tins 1.32.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,248 kB
  • sloc: ruby: 6,659; makefile: 3
file content (122 lines) | stat: -rw-r--r-- 3,728 bytes parent folder | download
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module Tins
  module Attempt
    # Attempts code in block *attempts* times, sleeping according to *sleep*
    # between attempts and catching the exception(s) in *exception_class*.
    #
    # *sleep* is either a Proc returning a floating point number for duration
    # as seconds or a Numeric >= 0 or < 0. In the former case this is the
    # duration directly, in the latter case -*sleep* is the total number of
    # seconds that is slept before giving up, and every attempt is retried
    # after a exponentially increasing duration of seconds.
    #
    # Iff *reraise* is true the caught exception is reraised after running out
    # of attempts.
    def attempt(opts = {}, &block)
      sleep           = nil
      exception_class = StandardError
      prev_exception  = nil
      if Numeric === opts
        attempts = opts
      else
        attempts        = opts[:attempts] || 1
        attempts >= 1 or raise ArgumentError, 'at least one attempt is required'
        exception_class = opts[:exception_class] if opts.key?(:exception_class)
        sleep           = interpret_sleep(opts[:sleep], attempts)
        reraise         = opts[:reraise]
      end
      return if attempts <= 0
      count = 0
      if exception_class.nil?
        begin
          count += 1
          if block.call(count, prev_exception)
            return true
          elsif count < attempts
            sleep_duration(sleep, count)
          end
        end until count == attempts
        false
      else
        begin
          count += 1
          block.call(count, prev_exception)
          true
        rescue *exception_class
          if count < attempts
            prev_exception = $!
            sleep_duration(sleep, count)
            retry
          end
          case reraise
          when Proc
            reraise.($!)
          when Exception.class
            raise reraise, "reraised: #{$!.message}"
          when true
            raise $!, "reraised: #{$!.message}"
          else
            false
          end
        end
      end
    end

    private

    def sleep_duration(duration, count)
      case duration
      when Numeric
        sleep duration
      when Proc
        sleep duration.call(count)
      end
    end

    def compute_duration_base(sleep, attempts)
      x1, x2  = 1, sleep
      attempts <= sleep or raise ArgumentError,
        "need less or equal number of attempts than sleep duration #{sleep}"
      x1 >= x2 and raise ArgumentError, "invalid sleep argument: #{sleep.inspect}"
      function = -> x { (0...attempts).inject { |s, i| s + x ** i } - sleep }
      f, fmid = function[x1], function[x2]
      f * fmid >= 0 and raise ArgumentError, "invalid sleep argument: #{sleep.inspect}"
      n       = 1 << 16
      epsilon = 1E-16
      root = if f < 0
               dx = x2 - x1
               x1
             else
               dx = x1 - x2
               x2
             end
      n.times do
        fmid = function[xmid = root + (dx *= 0.5)]
        fmid < 0 and root = xmid
        dx.abs < epsilon or fmid == 0 and return root
      end
      raise ArgumentError, "too many iterations (#{n})"
      result
    end

    def interpret_sleep(sleep, attempts)
      case sleep
      when nil
      when Numeric
        if sleep < 0
          if attempts > 2
            sleep = -sleep
            duration_base = compute_duration_base sleep, attempts
            sleep = lambda { |i| duration_base ** i }
          else
            raise ArgumentError, "require > 2 attempts for negative sleep value"
          end
        end
        sleep
      when Proc
        sleep
      else
        raise TypeError, "require Proc or Numeric sleep argument"
      end
    end
  end
end