1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Move more logic out of CLI

Focus on removing @options. Delegate all actions to launcher.
This commit is contained in:
schneems 2016-02-03 14:06:00 -06:00
parent 182c836869
commit 6ca4c4ca0a
4 changed files with 160 additions and 123 deletions

View file

@ -50,7 +50,6 @@ module Puma
@config = nil @config = nil
setup_options setup_options
generate_restart_data
@binder = Binder.new(@events) @binder = Binder.new(@events)
@binder.import_from_env @binder.import_from_env
@ -67,12 +66,22 @@ module Puma
@launcher.events = self.events @launcher.events = self.events
@launcher.config = self.config @launcher.config = self.config
@launcher.binder = self.binder @launcher.binder = self.binder
@launcher.argv = @argv
@launcher.setup(@options) @launcher.setup(@options)
end end
## BACKWARDS COMPAT FOR TESTS ## BACKWARDS COMPAT FOR TESTS
def error(str)
@launcher.error(str)
end
def debug(str)
@launcher.debug(str)
end
def delete_pidfile def delete_pidfile
@launcher.delete_pidfile @launcher.delete_pidfile
end end
@ -121,29 +130,17 @@ module Puma
# The Events object used to output information. # The Events object used to output information.
attr_reader :events attr_reader :events
# Delegate +error+ to +@events+
#
def error(str)
@events.error str
end
def debug(str)
@events.log "- #{str}" if @options[:debug]
end
def clustered? def clustered?
# remove eventually @launcher.clustered?
@options[:workers] > 0
end end
def jruby? def jruby?
# remove eventually Puma.jruby?
IS_JRUBY
end end
def windows? def windows?
# remove eventually Puma.windows?
RUBY_PLATFORM =~ /mswin32|ming32/
end end
<<<<<<< HEAD <<<<<<< HEAD
@ -195,63 +192,30 @@ module Puma
======= =======
>>>>>>> Initial Seperation of CLI and Server Launcher work >>>>>>> Initial Seperation of CLI and Server Launcher work
def jruby_daemon_start def jruby_daemon_start
require 'puma/jruby_restart' @launcher.jruby_daemon_start
JRubyRestart.daemon_start(@restart_dir, restart_args)
end end
def restart! def restart!
@options[:on_restart].each do |block| @launcher.restart!
block.call self
end
if jruby?
close_binder_listeners
require 'puma/jruby_restart'
JRubyRestart.chdir_exec(@restart_dir, restart_args)
elsif windows?
close_binder_listeners
argv = restart_args
Dir.chdir(@restart_dir)
argv += [redirects] if RUBY_VERSION >= '1.9'
Kernel.exec(*argv)
else
redirects = {:close_others => true}
@binder.listeners.each_with_index do |(l, io), i|
ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
redirects[io.to_i] = io.to_i
end
argv = restart_args
Dir.chdir(@restart_dir)
argv += [redirects] if RUBY_VERSION >= '1.9'
Kernel.exec(*argv)
end
end end
# Parse the options, load the rackup, start the server and wait # Parse the options, load the rackup, start the server and wait
# for it to finish. # for it to finish.
# #
def run def run
@runner = @launcher.runner
@launcher.run @launcher.run
end end
def reload_worker_directory def reload_worker_directory
@runner.reload_worker_directory if @runner.respond_to?(:reload_worker_directory) @launcher.reload_worker_directory
end end
def phased_restart def phased_restart
unless @runner.respond_to?(:phased_restart) and @runner.phased_restart @launcher.phased_restart
log "* phased-restart called but not available, restarting normally."
return restart
end
true
end end
def redirect_io def redirect_io
@runner.redirect_io @launcher.redirect_io
end end
def stats def stats
@ -259,8 +223,7 @@ module Puma
end end
def halt def halt
@status = :halt @launcher.halt
@runner.halt
end end
private private
@ -269,15 +232,6 @@ module Puma
raise UnsupportedOption raise UnsupportedOption
end end
def restart_args
cmd = @options[:restart_cmd]
if cmd
cmd.split(' ') + @original_argv
else
@restart_argv
end
end
# Build the OptionParser object to handle the available options. # Build the OptionParser object to handle the available options.
# #
@ -412,55 +366,5 @@ module Puma
end end
end end
end end
def generate_restart_data
# Use the same trick as unicorn, namely favor PWD because
# it will contain an unresolved symlink, useful for when
# the pwd is /data/releases/current.
if dir = ENV['PWD']
s_env = File.stat(dir)
s_pwd = File.stat(Dir.pwd)
if s_env.ino == s_pwd.ino and (jruby? or s_env.dev == s_pwd.dev)
@restart_dir = dir
@options[:worker_directory] = dir
end
end
@restart_dir ||= Dir.pwd
@original_argv = @argv.dup
require 'rubygems'
# if $0 is a file in the current directory, then restart
# it the same, otherwise add -S on there because it was
# picked up in PATH.
#
if File.exist?($0)
arg0 = [Gem.ruby, $0]
else
arg0 = [Gem.ruby, "-S", $0]
end
# Detect and reinject -Ilib from the command line
lib = File.expand_path "lib"
arg0[1,0] = ["-I", lib] if $:[0] == lib
if defined? Puma::WILD_ARGS
@restart_argv = arg0 + Puma::WILD_ARGS + @original_argv
else
@restart_argv = arg0 + @original_argv
end
end
def close_binder_listeners
@binder.listeners.each do |l, io|
io.close
uri = URI.parse(l)
next unless uri.scheme == 'unix'
File.unlink("#{uri.host}#{uri.path}")
end
end
end end
end end

View file

@ -1,4 +1,11 @@
module Puma module Puma
IS_JRUBY = defined?(JRUBY_VERSION) IS_JRUBY = defined?(JRUBY_VERSION)
end
def self.jruby?
IS_JRUBY
end
def self.windows?
RUBY_PLATFORM =~ /mswin32|ming32/
end
end

View file

@ -34,6 +34,16 @@ module Puma
@events @events
end end
# Delegate +error+ to +@events+
#
def error(str)
@events.error str
end
def debug(str)
@events.log "- #{str}" if @options[:debug]
end
def write_state def write_state
write_pid write_pid
@ -74,11 +84,14 @@ module Puma
end end
end end
attr_accessor :options, :binder, :config, :events attr_accessor :options, :binder, :config, :events, :argv
## THIS STUFF IS NEEDED FOR RUNNER ## THIS STUFF IS NEEDED FOR RUNNER
def setup(options) def setup(options)
@options = options @options = options
generate_restart_data
parse_options parse_options
dir = @options[:directory] dir = @options[:directory]
@ -101,7 +114,6 @@ module Puma
end end
attr_accessor :runner attr_accessor :runner
def stop def stop
@ -139,13 +151,11 @@ module Puma
end end
def jruby? def jruby?
# remove eventually Puma.jruby?
IS_JRUBY
end end
def windows? def windows?
# remove eventually Puma.windows?
RUBY_PLATFORM =~ /mswin32|ming32/
end end
@ -176,6 +186,71 @@ module Puma
end end
end end
def redirect_io
@runner.redirect_io
end
def phased_restart
unless @runner.respond_to?(:phased_restart) and @runner.phased_restart
log "* phased-restart called but not available, restarting normally."
return restart
end
true
end
private
def restart_args
cmd = @options[:restart_cmd]
if cmd
cmd.split(' ') + @original_argv
else
@restart_argv
end
end
public
def jruby_daemon_start
require 'puma/jruby_restart'
JRubyRestart.daemon_start(@restart_dir, restart_args)
end
def reload_worker_directory
@launcher.reload_worker_directory
end
def restart!
@options[:on_restart].each do |block|
block.call self
end
if jruby?
close_binder_listeners
require 'puma/jruby_restart'
JRubyRestart.chdir_exec(@restart_dir, restart_args)
elsif windows?
close_binder_listeners
argv = restart_args
Dir.chdir(@restart_dir)
argv += [redirects] if RUBY_VERSION >= '1.9'
Kernel.exec(*argv)
else
redirects = {:close_others => true}
@binder.listeners.each_with_index do |(l, io), i|
ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
redirects[io.to_i] = io.to_i
end
argv = restart_args
Dir.chdir(@restart_dir)
argv += [redirects] if RUBY_VERSION >= '1.9'
Kernel.exec(*argv)
end
end
private private
def unsupported(str) def unsupported(str)
@events.error(str) @events.error(str)
@ -243,6 +318,57 @@ module Puma
@options[:prune_bundler] && clustered? && !@options[:preload_app] @options[:prune_bundler] && clustered? && !@options[:preload_app]
end end
def close_binder_listeners
@binder.listeners.each do |l, io|
io.close
uri = URI.parse(l)
next unless uri.scheme == 'unix'
File.unlink("#{uri.host}#{uri.path}")
end
end
def generate_restart_data
# Use the same trick as unicorn, namely favor PWD because
# it will contain an unresolved symlink, useful for when
# the pwd is /data/releases/current.
if dir = ENV['PWD']
s_env = File.stat(dir)
s_pwd = File.stat(Dir.pwd)
if s_env.ino == s_pwd.ino and (jruby? or s_env.dev == s_pwd.dev)
@restart_dir = dir
@options[:worker_directory] = dir
end
end
@restart_dir ||= Dir.pwd
@original_argv = @argv.nil? ? "puma" : @argv.dup
require 'rubygems'
# if $0 is a file in the current directory, then restart
# it the same, otherwise add -S on there because it was
# picked up in PATH.
#
if File.exist?($0)
arg0 = [Gem.ruby, $0]
else
arg0 = [Gem.ruby, "-S", $0]
end
# Detect and reinject -Ilib from the command line
lib = File.expand_path "lib"
arg0[1,0] = ["-I", lib] if $:[0] == lib
if defined? Puma::WILD_ARGS
@restart_argv = arg0 + Puma::WILD_ARGS + @original_argv
else
@restart_argv = arg0 + @original_argv
end
end
def setup_signals def setup_signals
begin begin
Signal.trap "SIGUSR2" do Signal.trap "SIGUSR2" do

View file

@ -17,7 +17,7 @@ class TestCLI < Test::Unit::TestCase
@wait, @ready = IO.pipe @wait, @ready = IO.pipe
@events = Events.strings @events = Puma::Events.strings
@events.on_booted { @ready << "!" } @events.on_booted { @ready << "!" }
end end