File: degrading_lambda.rb

package info (click to toggle)
ruby-god 0.13.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 832 kB
  • sloc: ruby: 6,641; ansic: 237; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,146 bytes parent folder | download | duplicates (4)
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
module God
  module Conditions

    # This condition degrades its interval by a factor of two for 3 tries before failing
    class DegradingLambda < PollCondition
      attr_accessor :lambda

      def initialize
        super
        @tries = 0
      end

      def valid?
        valid = true
        valid &= complain("Attribute 'lambda' must be specified", self) if self.lambda.nil?
        valid
      end

      def test
        puts "Calling test. Interval at #{self.interval}"
        @original_interval ||= self.interval
        unless pass?
          if @tries == 2
            self.info = "lambda condition was satisfied"
            return true
          end
          self.interval = self.interval / 2.0
          @tries += 1
        else
          @tries = 0
          self.interval = @original_interval
        end

        self.info = "lambda condition was not satisfied"
        false
      end

      private

        def pass?
          begin
            Timeout::timeout(@interval) {
              self.lambda.call()
            }
          rescue Timeout::Error
            false
          end
        end
    end

  end
end