class RJR::EMAdapter
EventMachine adapater interface, ties reactor lifecycle to an instance of this class.
Attributes
reactor_thread[RW]
Run reactor in its own interally managed thread
Public Class Methods
new()
click to toggle source
EMAdapter
initializer
# File lib/rjr/util/em_adapter.rb, line 17 def initialize @em_lock = Mutex.new EventMachine.error_handler { |e| puts "EventMachine raised critical error #{e} #{e.backtrace}" # TODO dispatch to registered event handlers } end
Public Instance Methods
halt()
click to toggle source
Halt the reactor if running
@return self
# File lib/rjr/util/em_adapter.rb, line 50 def halt self.stop_event_loop if self.reactor_running? self end
join()
click to toggle source
Block until reactor thread is terminated
@return self
# File lib/rjr/util/em_adapter.rb, line 58 def join th = @em_lock.synchronize{ @reactor_thread } th.join unless th.nil? self end
method_missing(method_id, *args, &bl)
click to toggle source
Delegates everything else directly to eventmachine
(eg schedule, add_timer, add_periodic_timer, reactor_running?, stop_event_loop, etc)
# File lib/rjr/util/em_adapter.rb, line 67 def method_missing(method_id, *args, &bl) @em_lock.synchronize{ EventMachine.send method_id, *args, &bl } end
start()
click to toggle source
Start the eventmachine reactor thread if not running
# File lib/rjr/util/em_adapter.rb, line 27 def start @em_lock.synchronize{ # TODO on event of the process ending this thread will be # shutdown before a local finalizer can be run, # would be good to gracefully shut this down / wait for completion @reactor_thread = Thread.new { begin EventMachine.run rescue Exception => e # TODO option to autorestart the reactor on errors ? puts "Critical exception #{e}\n#{e.backtrace.join("\n")}" ensure @em_lock.synchronize { @reactor_thread = nil } end } unless @reactor_thread } sleep 0.01 until EventMachine.reactor_running? # XXX hack but needed self end