5 # directory to hold conditions and their corresponding metric
8 attr_accessor :directory
13 def self.attach(condition, metric)
14 # add the condition to the directory
15 self.directory[condition] = metric
17 # schedule poll condition
18 # register event condition
19 if condition.kind_of?(PollCondition)
20 Timer.get.schedule(condition, 0)
26 def self.detach(condition)
27 # remove the condition from the directory
28 self.directory.delete(condition)
30 # unschedule any pending polls
31 Timer.get.unschedule(condition)
33 # deregister event condition
34 if condition.kind_of?(EventCondition)
39 def self.trigger(condition)
40 if condition.kind_of?(PollCondition)
41 self.handle_poll(condition)
42 elsif condition.kind_of?(EventCondition)
43 self.handle_event(condition)
47 def self.handle_poll(condition)
50 metric = self.directory[condition]
52 # it's possible that the timer will trigger an event before it can be cleared
53 # by an exiting metric, in which case it should be ignored
58 watch.mutex.synchronize do
60 result = condition.test
63 msg = watch.name + ' ' + condition.class.name + " [#{result}] " + metric.destination.inspect
65 LOG.log(watch, :info, msg)
72 if result && condition.transition
77 metric.destination[result]
80 # transition or reschedule
85 rescue EventRegistrationFailedError
86 msg = watch.name + ' Event registration failed, moving back to previous state'
88 LOG.log(watch, :info, msg)
95 Timer.get.schedule(condition)
99 message = format("Unhandled exception (%s): %s\n%s",
100 e.class, e.message, e.backtrace.join("\n"))
107 def self.handle_event(condition)
109 metric = self.directory[condition]
112 watch.mutex.synchronize do
113 msg = watch.name + ' ' + condition.class.name + " [true] " + metric.destination.inspect
115 LOG.log(watch, :info, msg)
117 dest = metric.destination[true]