Class: God::EventHandler
- Inherits:
-
Object
- Object
- God::EventHandler
- Defined in:
- lib/god/event_handler.rb
Constant Summary collapse
- @@actions =
{}
- @@handler =
nil
- @@loaded =
false
Class Method Summary collapse
- .call(pid, event, extra_data = {}) ⇒ Object
- .deregister(pid, event = nil) ⇒ Object
- .event_system ⇒ Object
- .load ⇒ Object
- .loaded? ⇒ Boolean
- .operational? ⇒ Boolean
- .register(pid, event, &block) ⇒ Object
- .start ⇒ Object
- .watching_pid?(pid) ⇒ Boolean
Class Method Details
.call(pid, event, extra_data = {}) ⇒ Object
54 55 56 |
# File 'lib/god/event_handler.rb', line 54 def self.call(pid, event, extra_data = {}) @@actions[pid][event].call(extra_data) if watching_pid?(pid) && @@actions[pid][event] end |
.deregister(pid, event = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/god/event_handler.rb', line 41 def self.deregister(pid, event=nil) if watching_pid? pid running = ::Process.kill(0, pid.to_i) rescue false if event.nil? @@actions.delete(pid) @@handler.register_process(pid, []) if running else @@actions[pid].delete(event) @@handler.register_process(pid, @@actions[pid].keys) if running end end end |
.event_system ⇒ Object
11 12 13 |
# File 'lib/god/event_handler.rb', line 11 def self.event_system @@handler::EVENT_SYSTEM end |
.load ⇒ Object
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 |
# 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) sleep(0.1) self.deregister(pid, :proc_exit) rescue nil rescue => e puts e. 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 |
.start ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/god/event_handler.rb', line 62 def self.start Thread.new do loop do begin @@handler.handle_events rescue Exception => e = format("Unhandled exception (%s): %s\n%s", e.class, e., e.backtrace.join("\n")) applog(nil, :fatal, ) end end end # do a real test to make sure events are working properly @@loaded = self.operational? end |
.watching_pid?(pid) ⇒ Boolean
58 59 60 |
# File 'lib/god/event_handler.rb', line 58 def self.watching_pid?(pid) @@actions[pid] end |