Sha256: c4f94da8e3d3cfa40b4363185500bfb17927f44921c4d0b53f2c968f456d62b9

Contents?: true

Size: 1.33 KB

Versions: 44

Compression:

Stored size: 1.33 KB

Contents

module DaemonKit

  # 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 EM

    class << self

      # 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.
      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

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

  end
end

Version data entries

44 entries across 44 versions & 7 rubygems

Version Path
daemon-kit-0.1.8 lib/daemon_kit/em.rb
daemon-kit-0.1.8rc3 lib/daemon_kit/em.rb
textgoeshere-daemon-kit-0.1.8.31 lib/daemon_kit/em.rb
textgoeshere-daemon-kit-0.1.8rc3 lib/daemon_kit/em.rb
daemon-kit-0.1.8rc2 lib/daemon_kit/em.rb
daemon-kit-0.1.8rc1 lib/daemon_kit/em.rb
apphunkd-0.13.0 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.12.2 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.12.1 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.12.0 vendor/daemon_kit/lib/daemon_kit/em.rb
daemon-kit-0.1.8pre lib/daemon_kit/em.rb
apphunkd-0.11.1 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.11.0 vendor/daemon_kit/lib/daemon_kit/em.rb
daemon-kit-0.1.7.12 lib/daemon_kit/em.rb
daemon-kit-0.1.7.11 lib/daemon_kit/em.rb
apphunkd-0.10.1 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.10.0 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.9.2 vendor/daemon_kit/lib/daemon_kit/em.rb
apphunkd-0.9.1 vendor/daemon-kit/lib/daemon_kit/em.rb
apphunkd-0.9.0 vendor/daemon-kit/lib/daemon_kit/em.rb