Class: God::Hub

Inherits:
Object
  • Object
show all
Defined in:
lib/god/hub.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.directoryObject

directory to hold conditions and their corresponding metric

key: condition
val: metric


8
9
10
# File 'lib/god/hub.rb', line 8

def directory
  @directory
end

Class Method Details

.attach(condition, metric) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/god/hub.rb', line 13

def self.attach(condition, metric)
  # add the condition to the directory
  self.directory[condition] = metric
  
  # schedule poll condition
  # register event condition
  if condition.kind_of?(PollCondition)
    Timer.get.schedule(condition, 0)
  else
    condition.register
  end
end

.detach(condition) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/god/hub.rb', line 26

def self.detach(condition)
  # remove the condition from the directory
  self.directory.delete(condition)
  
  # unschedule any pending polls
  Timer.get.unschedule(condition)
  
  # deregister event condition
  if condition.kind_of?(EventCondition)
    condition.deregister
  end
end

.handle_event(condition) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/god/hub.rb', line 84

def self.handle_event(condition)
  Thread.new do
    metric = self.directory[condition]
    watch = metric.watch
    
    watch.mutex.synchronize do
      msg = watch.name + ' ' + condition.class.name + " [true] " + metric.destination.inspect
      Syslog.debug(msg)
      puts msg
      
      dest = metric.destination[true]
      watch.move(dest)
    end
  end
end

.handle_poll(condition) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/god/hub.rb', line 47

def self.handle_poll(condition)
  Thread.new do
    begin
      metric = self.directory[condition]
      
      # it's possible that the timer will trigger an event before it can be cleared
      # by an exiting metric, in which case it should be ignored
      return if metric.nil?
      
      watch = metric.watch
    
      watch.mutex.synchronize do
        result = condition.test
      
        msg = watch.name + ' ' + condition.class.name + " [#{result}] " + metric.destination.inspect
        Syslog.debug(msg)
        puts msg
      
        condition.after
      
        dest = metric.destination[result]
        if dest
          watch.move(dest)
        else
          # reschedule
          Timer.get.schedule(condition)
        end
      end
    rescue => e
      message = format("Unhandled exception (%s): %s\n%s",
                       e.class, e.message, e.backtrace.join("\n"))
      Syslog.crit message
      abort message
    end
  end
end

.trigger(condition) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/god/hub.rb', line 39

def self.trigger(condition)
  if condition.kind_of?(PollCondition)
    self.handle_poll(condition)
  elsif condition.kind_of?(EventCondition)
    self.handle_event(condition)
  end
end