Sha256: 69169cb030d3d092a903e771e46e611cb1ea176de97d5f359ac893cd75ba2f85

Contents?: true

Size: 906 Bytes

Versions: 1

Compression:

Stored size: 906 Bytes

Contents

require 'rlyeh/utils'

module Rlyeh
  module Runner
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def servers
        @servers ||= []
      end

      def run(*args, &block)
        default_options = {:signal_trap => true}.freeze
        options = default_options.merge(Rlyeh::Utils.extract_options args)
        signal_trap = options.delete(:signal_trap)

        trap(:INT) { stop } if signal_trap

        starter = proc do
          server = Server.start *args, &block
          servers << server
        end

        if EventMachine.reactor_running?
          starter.call
        else
          EventMachine.run &starter
        end
      end
      alias_method :emerge, :run

      def stop
        servers.each { |server| server.stop }
        servers.clear

        EventMachine.stop if EventMachine.reactor_running?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rlyeh-0.0.2 lib/rlyeh/runner.rb