Class: Rack::Handler::Mongrel2

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/handler/mongrel2.rb

Class Method Summary collapse

Class Method Details

.run(app, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/handler/mongrel2.rb', line 9

def run(app, options = {})
  options = {
    :recv => ENV['RACK_MONGREL2_RECV'] || 'tcp://127.0.0.1:9997',
    :send => ENV['RACK_MONGREL2_SEND'] || 'tcp://127.0.0.1:9996',
    :uuid => ENV['RACK_MONGREL2_UUID'],
    :chroot => ENV['RACK_MONGREL2_CHROOT'] || '.'
  }.merge(options)

  raise ArgumentError.new('Must specify an :uuid or set RACK_MONGREL2_UUID') if options[:uuid].nil?

  conn = nil

  EM.run do
    conn = ::Mongrel2::Connection.new(options[:uuid], options[:recv], options[:send], options[:chroot], app)
    
    # This doesn't work at all until zmq fixes their shit (in 2.1.x I think), but trap it now anyway.
    %w(INT TERM KILL).each do |sig|
      trap(sig) do
        conn.close
        EM.stop
      end
    end
  end
ensure
  conn.close if conn.respond_to?(:close)
end