hide kqueue tests from linux
[god.git] / lib / god / conditions / degrading_lambda.rb
blobd53fbb49639c1f3ff99f8cbc6c4de7d78bf0761d
1 module God
2   module Conditions
3     
4     # This condition degrades its interval by a factor of two for 3 tries before failing
5     class DegradingLambda < PollCondition
6       attr_accessor :lambda
7       
8       def initialize
9         super
10         @tries = 0
11       end
12       
13       def valid?
14         valid = true
15         valid &= complain("You must specify the 'lambda' attribute for :degrading_lambda") if self.lambda.nil?
16         valid
17       end
19       def test
20         puts "Calling test. Interval at #{self.interval}"
21         @original_interval ||= self.interval
22         unless pass?
23           return true if @tries == 2
24           self.interval = self.interval / 2.0
25           @tries += 1
26         else
27           @tries = 0
28           self.interval = @original_interval
29         end
30         false
31       end
32       
33       private
34         
35         def pass?
36           begin
37             Timeout::timeout(@interval) {
38               self.lambda.call()
39             }
40           rescue Timeout::Error
41             false
42           end
43         end
44     end
46   end
47 end