Permalink
Browse files

Switch puma rack handler to use Launcher

  • Loading branch information...
schneems committed Feb 4, 2016
1 parent 15447fe commit d6df5cec224a9a020dfb262a5d4c1b52d33e6df3
Showing with 20 additions and 26 deletions.
  1. +14 −22 lib/rack/handler/puma.rb
  2. +6 −4 test/test_rack_handler.rb
@@ -5,8 +5,8 @@ module Rack
module Handler
module Puma
DEFAULT_OPTIONS = {
:Host => '0.0.0.0',
:Port => 8080,
:Host => '0.0.0.0',
:Port => 8080,
:Threads => '0:16',
:Verbose => false,
:Silent => false
@@ -15,38 +15,30 @@ module Puma
def self.run(app, options = {})
options = DEFAULT_OPTIONS.merge(options)

if options[:Verbose]
if options.delete(:Verbose)
app = Rack::CommonLogger.new(app, STDOUT)
end

if options[:environment]
ENV['RACK_ENV'] = options[:environment].to_s
end

events_hander = options[:Silent] ? ::Puma::Events.strings : ::Puma::Events.stdio
server = ::Puma::Server.new(app, events_hander)
min, max = options[:Threads].split(':', 2)
options[:binds] ||= []
options[:binds] << "tcp://#{ options.delete(:Host) }:#{ options.delete(:Port) }"
options[:min_threads], options[:max_threads] = options.delete(:Threads).split(':', 2)
options[:app] = app
events = options.delete(:Silent) ? ::Puma::Events.strings : ::Puma::Events.stdio

log = events_hander.stdout

log.puts "Puma #{::Puma::Const::PUMA_VERSION} starting..."
log.puts "* Min threads: #{min}, max threads: #{max}"
log.puts "* Environment: #{ENV['RACK_ENV']}"
log.puts "* Listening on tcp://#{options[:Host]}:#{options[:Port]}"

server.add_tcp_listener options[:Host], options[:Port]
server.min_threads = min
server.max_threads = max
yield server if block_given?
launcher = ::Puma::Launcher.new(options, events: events)

yield launcher if block_given?
begin
server.run.join
launcher.run
rescue Interrupt
log.puts "* Gracefully stopping, waiting for requests to finish"
server.stop(true)
log.puts "* Goodbye!"
puts "* Gracefully stopping, waiting for requests to finish"
launcher.stop
puts "* Goodbye!"
end

end

def self.valid_options
@@ -33,14 +33,16 @@ def in_handler(app, options = {})

# Wait for server to boot
Timeout.timeout(10) do
until @server && @server.running
sleep 0.01
until @server
sleep 1
end
end
sleep 1

yield @server
ensure
@server.stop(true) if @server
thread.join if thread
@server.stop if @server
thread.join if thread
end


0 comments on commit d6df5ce

Please sign in to comment.