more diagnostics
[god.git] / lib / god / metric.rb
blobb6a66ad1d63b7551e6d3d17fb29419d018fa50a7
1 module God
2   
3   class Metric
4     attr_accessor :watch, :destination, :conditions
5     
6     def initialize(watch, destination = nil)
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       # test generic and specific validity
29       unless Condition.valid?(c) && c.valid?
30         abort "Exiting on invalid condition"
31       end
32       
33       # inherit interval from watch if no poll condition specific interval was set
34       if c.kind_of?(PollCondition) && !c.interval
35         if self.watch.interval
36           c.interval = self.watch.interval
37         else
38           abort "No interval set for Condition '#{c.class.name}' in Watch '#{self.watch.name}', and no default Watch interval from which to inherit"
39         end
40       end
41       
42       # remember
43       self.conditions << c
44     end
45     
46     def enable
47       self.conditions.each do |c|
48         Hub.attach(c, self)
49       end
50     end
51     
52     def disable
53       self.conditions.each do |c|
54         Hub.detach(c)
55       end
56     end
57   end
58   
59 end