Class: DaemonKit::EM

Inherits:
Object show all
Defined in:
lib/daemon_kit/em.rb

Overview

EventMachine forms a critical part of the daemon-kit toolset, and especially of daemon process developers.

This class abstracts away the difficulties of managing multiple libraries that all utilize the event reactor.

Class Method Summary collapse

Class Method Details

.run(&block) ⇒ Object

Start a reactor, just like classical EM.run. If the block is provided, the method will block and call the provided block argument inside the running reactor. If the block argument is not provided the reactor will be started in a separate thread and the program will continue to run after the method. All the signal traps are configured to shutdown the reactor when the daemon exists.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/daemon_kit/em.rb', line 19

def run(&block)
  if ::EM.reactor_running?
    DaemonKit.logger.warn "EventMachine reactor already running"
    block.call if block_given?

  else
    if block_given?
      ::EM.run { block.call }
    else
      Thread.main[:_dk_reactor] = Thread.new { EM.run {} }
      DaemonKit.trap( 'INT' ) { DaemonKit::EM.stop  }
      DaemonKit.trap( 'TERM' ) { DaemonKit::EM.stop }
    end
  end
end

.stopObject

Stop the reactor



36
37
38
39
# File 'lib/daemon_kit/em.rb', line 36

def stop
  ::EM.stop_event_loop if ::EM.reactor_running?
  Thread.main[:_dk_reactor].join
end