lib/god/event_handler.rb in resurrected_god-0.14.0 vs lib/god/event_handler.rb in resurrected_god-1.0.0

- old
+ new

@@ -11,42 +11,40 @@ def self.event_system @@handler::EVENT_SYSTEM end 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 + 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 def self.register(pid, event, &block) @@actions[pid] ||= {} @@actions[pid][event] = block @@handler.register_process(pid, @@actions[pid].keys) end 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 + return unless 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 def self.call(pid, event, extra_data = {}) @@actions[pid][event].call(extra_data) if watching_pid?(pid) && @@actions[pid][event] end @@ -56,57 +54,50 @@ end 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 + @@handler.handle_events + rescue Exception => e + message = format("Unhandled exception (%{class}): %{message}\n%{backtrace}", + class: e.class, message: e.message, backtrace: e.backtrace.join("\n")) + applog(nil, :fatal, message) end end # do a real test to make sure events are working properly - @@loaded = self.operational? + @@loaded = operational? end def self.stop - @@thread.kill if @@thread + @@thread&.kill end def self.operational? com = [false] Thread.new do - begin - event_system = God::EventHandler.event_system + pid = fork do + loop { sleep(1) } + end - pid = fork do - loop { sleep(1) } - end + register(pid, :proc_exit) do + com[0] = true + end - self.register(pid, :proc_exit) do - com[0] = true - end + ::Process.kill('KILL', pid) + ::Process.waitpid(pid) - ::Process.kill('KILL', pid) - ::Process.waitpid(pid) + sleep(0.1) - sleep(0.1) - - self.deregister(pid, :proc_exit) rescue nil - rescue => e - puts e.message - puts e.backtrace.join("\n") - end + deregister(pid, :proc_exit) rescue nil + rescue => e + puts e.message + puts e.backtrace.join("\n") end.join sleep(0.1) com.first end - end end