fix process#alive? to not raise on no such file
[god.git] / lib / god / metric.rb
blob07c7d3ebf27ab2e5ccae2bde7976c849a9aa24fa
1 module God
2   
3   class Metric
4     attr_accessor :watch, :destination, :conditions
5     
6     def initialize(watch, destination)
7       self.watch = watch
8       self.destination = destination
9       self.conditions = []
10     end
11     
12     # Instantiate a Condition of type +kind+ and pass it into the optional
13     # block. Attributes of the condition must be set in the config file
14     def condition(kind)
15       # create the condition
16       begin
17         c = Condition.generate(kind, self.watch)
18       rescue NoSuchConditionError => e
19         abort e.message
20       end
21       
22       # send to block so config can set attributes
23       yield(c) if block_given?
24       
25       # call prepare on the condition
26       c.prepare
27       
28       # abort if the Condition is invalid, the Condition will have printed
29       # out its own error messages by now
30       unless c.valid?
31         abort "Exiting on invalid condition"
32       end
33       
34       # inherit interval from watch if no poll condition specific interval was set
35       if c.kind_of?(PollCondition) && !c.interval
36         if self.watch.interval
37           c.interval = self.watch.interval
38         else
39           abort "No interval set for Condition '#{c.class.name}' in Watch '#{self.watch.name}', and no default Watch interval from which to inherit"
40         end
41       end
42       
43       # remember
44       self.conditions << c
45     end
46     
47     def enable
48       self.conditions.each do |c|
49         Hub.attach(c, self)
50       end
51     end
52     
53     def disable
54       self.conditions.each do |c|
55         Hub.detach(c)
56       end
57     end
58   end
59   
60 end