5 # directory to hold conditions and their corresponding metric
6 # {condition => metric}
7 attr_accessor :directory
12 def self.attach(condition, metric)
13 self.directory[condition] = metric
17 Timer.get.schedule(condition, 0)
18 when EventCondition, TriggerCondition
23 def self.detach(condition)
24 self.directory.delete(condition)
28 Timer.get.unschedule(condition)
29 when EventCondition, TriggerCondition
34 def self.trigger(condition)
37 self.handle_poll(condition)
38 when EventCondition, TriggerCondition
39 self.handle_event(condition)
43 def self.handle_poll(condition)
46 metric = self.directory[condition]
48 # it's possible that the timer will trigger an event before it can be cleared
49 # by an exiting metric, in which case it should be ignored
53 watch.mutex.synchronize do
55 result = condition.test
58 msg = watch.name + ' ' + condition.class.name + " [#{result}] " + self.dest_desc(metric, condition)
60 LOG.log(watch, :info, msg)
67 if result && condition.transition
72 metric.destination && metric.destination[result]
75 # transition or reschedule
80 rescue EventRegistrationFailedError
81 msg = watch.name + ' Event registration failed, moving back to previous state'
83 LOG.log(watch, :info, msg)
90 Timer.get.schedule(condition)
95 message = format("Unhandled exception (%s): %s\n%s",
96 e.class, e.message, e.backtrace.join("\n"))
103 def self.handle_event(condition)
105 metric = self.directory[condition]
110 watch.mutex.synchronize do
111 msg = watch.name + ' ' + condition.class.name + " [true] " + self.dest_desc(metric, condition)
113 LOG.log(watch, :info, msg)
115 # get the destination
117 if condition.transition
122 metric.destination && metric.destination[true]
135 def self.dest_desc(metric, condition)
136 if metric.destination
137 metric.destination.inspect
139 if condition.transition
140 {true => condition.transition}.inspect