diff --git a/lib/puma/cli.rb b/lib/puma/cli.rb index 09bbb7d6..4f7ca6f8 100644 --- a/lib/puma/cli.rb +++ b/lib/puma/cli.rb @@ -287,7 +287,6 @@ module Puma end def graceful_stop - @control.stop(true) if @control @runner.stop_blocked log "- Goodbye!" end @@ -428,10 +427,6 @@ module Puma setup_signals - if cont = @options[:control_url] - start_control cont - end - @status = :run @runner.run @@ -443,8 +438,10 @@ module Puma graceful_stop when :restart log "* Restarting..." - @control.stop true if @control + @runner.before_restart restart! + when :exit + # nothing end end @@ -467,44 +464,13 @@ module Puma if jruby? Signal.trap("INT") do + @status = :exit graceful_stop exit end end end - def start_control(str) - require 'puma/app/status' - - uri = URI.parse str - - app = Puma::App::Status.new self - - if token = @options[:control_auth_token] - app.auth_token = token unless token.empty? or token == :none - end - - control = Puma::Server.new app, @events - control.min_threads = 0 - control.max_threads = 1 - - case uri.scheme - when "tcp" - log "* Starting control server on #{str}" - control.add_tcp_listener uri.host, uri.port - when "unix" - log "* Starting control server on #{str}" - path = "#{uri.host}#{uri.path}" - - control.add_unix_listener path - else - error "Invalid control URI: #{str}" - end - - control.run - @control = control - end - def stop @status = :stop @runner.stop diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index ddbdda22..9861ce33 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -175,6 +175,7 @@ module Puma def stop_blocked @status = :stop if @status == :run wakeup! + @control.stop(true) if @control Process.waitall end @@ -258,6 +259,8 @@ module Puma redirect_io + start_control + @cli.write_state @master_read, @worker_write = read, @wakeup diff --git a/lib/puma/runner.rb b/lib/puma/runner.rb index f9650716..29553cb5 100644 --- a/lib/puma/runner.rb +++ b/lib/puma/runner.rb @@ -4,6 +4,7 @@ module Puma @cli = cli @options = cli.options @app = nil + @control = nil end def daemon? @@ -22,6 +23,45 @@ module Puma @cli.error str end + def before_restart + @control.stop(true) if @control + end + + def start_control + str = @options[:control_url] + return unless str + + require 'puma/app/status' + + uri = URI.parse str + + app = Puma::App::Status.new @cli + + if token = @options[:control_auth_token] + app.auth_token = token unless token.empty? or token == :none + end + + control = Puma::Server.new app, @cli.events + control.min_threads = 0 + control.max_threads = 1 + + case uri.scheme + when "tcp" + log "* Starting control server on #{str}" + control.add_tcp_listener uri.host, uri.port + when "unix" + log "* Starting control server on #{str}" + path = "#{uri.host}#{uri.path}" + + control.add_unix_listener path + else + error "Invalid control URI: #{str}" + end + + control.run + @control = control + end + def output_header(mode) min_t = @options[:min_threads] max_t = @options[:max_threads] diff --git a/lib/puma/single.rb b/lib/puma/single.rb index 5162acc6..2ba4dff2 100644 --- a/lib/puma/single.rb +++ b/lib/puma/single.rb @@ -22,6 +22,7 @@ module Puma def stop_blocked log "- Gracefully stopping, waiting for requests to finish" + @control.stop(true) if @control @server.stop(true) end @@ -68,6 +69,8 @@ module Puma @cli.write_state + start_control + @server = server = start_server unless @options[:daemon]