Permalink
Browse files

Handle SIGTERM gracefully and (hopefully) correctly.

  • Loading branch information...
1 parent 3fb47a1 commit ba338fd7d2e093a0b21f6c0bb27aaf67e74c2403 Nathan Duran committed May 22, 2011
Showing with 49 additions and 41 deletions.
  1. +1 −1 khi-rack-mongrel2.gemspec
  2. +1 −1 lib/khi-rack-mongrel2.rb
  3. +12 −5 lib/mongrel2/connection.rb
  4. +35 −34 lib/rack/handler/mongrel2.rb
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
## If your rubyforge_project name is different, then edit it and comment out
## the sub! line in the Rakefile
s.name = 'khi-rack-mongrel2'
- s.version = '1.0.2'
+ s.version = '1.0.3'
s.date = '2011-05-21'
s.rubyforge_project = 'khi-rack-mongrel2'
View
@@ -10,5 +10,5 @@
module Mongrel2
JSON = Object.const_defined?('Yajl') ? ::Yajl::Parser : ::JSON
- VERSION = '1.0.2'
+ VERSION = '1.0.3'
end
View
@@ -3,6 +3,9 @@
require 'mongrel2/response'
module Mongrel2
+
+ class ConnectionDiedError < StandardError; end
+
class Connection
CTX = ZMQ::Context.new(1)
@@ -23,12 +26,16 @@ def initialize(uuid, sub, pub)
def recv
msg = nil
- ready_sockets = ZMQ.select([@reqs], nil, nil, 30)
- if !ready_sockets.nil?
- ready_sockets[0].each do | socket |
- msg = socket.recv(ZMQ::NOBLOCK)
- msg = Request.parse(msg) unless msg.nil?
+ begin
+ ready_sockets = ZMQ.select([@reqs], nil, nil, 30)
+ if !ready_sockets.nil?
+ ready_sockets[0].each do | socket |
+ msg = socket.recv(ZMQ::NOBLOCK)
+ msg = Request.parse(msg) unless msg.nil?
+ end
end
+ rescue RuntimeError => e
+ raise ConnectionDiedError
end
msg
end
@@ -20,46 +20,47 @@ def self.run(app, options = {})
running = false
end
end
+
+ begin
+ while running do
+ req = conn.recv
+ next if req.nil? || req.disconnect?
+ break if !running
- while running do
- req = conn.recv
- next if req.nil? || req.disconnect?
- break if !running
-
- script_name = ENV['RACK_RELATIVE_URL_ROOT'] || req.headers['PATTERN'].split('(', 2).first.gsub(/\/$/, '')
- env = {
- 'rack.version' => Rack::VERSION,
- 'rack.url_scheme' => 'http', # Only HTTP for now
- 'rack.input' => StringIO.new(req.body),
- 'rack.errors' => $stderr,
- 'rack.multithread' => true,
- 'rack.multiprocess' => true,
- 'rack.run_once' => false,
- 'mongrel2.pattern' => req.headers['PATTERN'],
- 'REQUEST_METHOD' => req.headers['METHOD'],
- 'SCRIPT_NAME' => script_name,
- 'PATH_INFO' => req.headers['PATH'].gsub(script_name, ''),
- 'QUERY_STRING' => req.headers['QUERY'] || ''
- }
+ script_name = ENV['RACK_RELATIVE_URL_ROOT'] || req.headers['PATTERN'].split('(', 2).first.gsub(/\/$/, '')
+ env = { 'rack.version' => Rack::VERSION,
+ 'rack.url_scheme' => 'http', # Only HTTP for now
+ 'rack.input' => StringIO.new(req.body),
+ 'rack.errors' => $stderr,
+ 'rack.multithread' => true,
+ 'rack.multiprocess' => true,
+ 'rack.run_once' => false,
+ 'mongrel2.pattern' => req.headers['PATTERN'],
+ 'REQUEST_METHOD' => req.headers['METHOD'],
+ 'SCRIPT_NAME' => script_name,
+ 'PATH_INFO' => req.headers['PATH'].gsub(script_name, ''),
+ 'QUERY_STRING' => req.headers['QUERY'] || '' }
- env['SERVER_NAME'], env['SERVER_PORT'] = req.headers['host'].split(':', 2)
- req.headers.each do |key, val|
- unless key =~ /content_(type|length)/i
- key = "HTTP_#{key.upcase.gsub('-', '_')}"
+ env['SERVER_NAME'], env['SERVER_PORT'] = req.headers['host'].split(':', 2)
+ req.headers.each do |key, val|
+ unless key =~ /content_(type|length)/i
+ key = "HTTP_#{key.upcase.gsub('-', '_')}"
+ end
+ env[key] = val
end
- env[key] = val
- end
- status, headers, rack_response = app.call(env)
- body = ''
- rack_response.each { |b| body << b }
- conn.reply(req, body, status, headers)
+ status, headers, rack_response = app.call(env)
+ body = ''
+ rack_response.each { |b| body << b }
+ conn.reply(req, body, status, headers)
+ end
+ rescue ::Mongrel2::ConnectionDiedError => e
+ conn.close
+ exit
+ return
end
-
- conn.close
- Process.exit!
end #def self.run
end #class Mongrel2
end #module Handler
-end #module Rack
+end #module Rack

0 comments on commit ba338fd

Please sign in to comment.