Class: God::EventHandler

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

Constant Summary collapse

@@actions =
{}
@@handler =
nil
@@loaded =
false

Class Method Summary collapse

Class Method Details

.call(pid, event, extra_data = {}) ⇒ Object


50
51
52
# File 'lib/god/event_handler.rb', line 50

def self.call(pid, event, extra_data = {})
  @@actions[pid][event].call(extra_data) if watching_pid?(pid) && @@actions[pid][event]
end

.deregister(pid, event) ⇒ Object


41
42
43
44
45
46
47
48
# File 'lib/god/event_handler.rb', line 41

def self.deregister(pid, event)
  if watching_pid? pid
    running = ::Process.kill(0, pid.to_i) rescue false
    @@actions[pid].delete(event)
    @@handler.register_process(pid, @@actions[pid].keys) if running
    @@actions.delete(pid) if @@actions[pid].empty?
  end
end

.event_systemObject


11
12
13
# File 'lib/god/event_handler.rb', line 11

def self.event_system
  @@handler::EVENT_SYSTEM
end

.loadObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/god/event_handler.rb', line 15

def self.load
  begin
    case RUBY_PLATFORM
    when /darwin/i, /bsd/i
      require 'god/event_handlers/kqueue_handler'
      @@handler = KQueueHandler
    when /linux/i
      require 'god/event_handlers/netlink_handler'
      @@handler = NetlinkHandler
    else
      raise NotImplementedError, "Platform not supported for EventHandler"
    end
    @@loaded = true
  rescue Exception
    require 'god/event_handlers/dummy_handler'
    @@handler = DummyHandler
    @@loaded = false
  end
end

.loaded?Boolean


7
8
9
# File 'lib/god/event_handler.rb', line 7

def self.loaded?
  @@loaded
end

.operational?Boolean


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/god/event_handler.rb', line 79

def self.operational?
  com = [false]

  Thread.new do
    begin
      event_system = God::EventHandler.event_system

      pid = fork do
        loop { sleep(1) }
      end

      self.register(pid, :proc_exit) do
        com[0] = true
      end

      ::Process.kill('KILL', pid)
      ::Process.waitpid(pid)

      sleep(0.1)

      self.deregister(pid, :proc_exit) rescue nil
    rescue => e
      puts e.message
      puts e.backtrace.join("\n")
    end
  end.join

  sleep(0.1)

  com.first
end

.register(pid, event, &block) ⇒ Object


35
36
37
38
39
# File 'lib/god/event_handler.rb', line 35

def self.register(pid, event, &block)
  @@actions[pid] ||= {}
  @@actions[pid][event] = block
  @@handler.register_process(pid, @@actions[pid].keys)
end

.startObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/god/event_handler.rb', line 58

def self.start
  @@thread = Thread.new do
    loop do
      begin
        @@handler.handle_events
      rescue Exception => e
        message = format("Unhandled exception (%s): %s\n%s",
                         e.class, e.message, e.backtrace.join("\n"))
        applog(nil, :fatal, message)
      end
    end
  end

  # do a real test to make sure events are working properly
  @@loaded = self.operational?
end

.stopObject


75
76
77
# File 'lib/god/event_handler.rb', line 75

def self.stop
  @@thread.kill if @@thread
end

.watching_pid?(pid) ⇒ Boolean


54
55
56
# File 'lib/god/event_handler.rb', line 54

def self.watching_pid?(pid)
  @@actions[pid]
end