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

543 lines
12 KiB
Ruby
Raw Normal View History

2011-09-27 12:23:03 -04:00
require 'optparse'
2011-09-27 13:53:45 -04:00
require 'uri'
require 'puma/server'
2011-09-27 12:23:03 -04:00
require 'puma/const'
2011-12-07 16:42:53 -05:00
require 'puma/configuration'
require 'puma/binder'
2012-08-10 22:35:47 -04:00
require 'puma/detect'
require 'puma/daemon_ext'
2013-02-05 01:31:40 -05:00
require 'puma/util'
require 'puma/single'
require 'puma/cluster'
2011-09-27 12:23:03 -04:00
require 'rack/commonlogger'
2012-01-13 19:29:50 -05:00
require 'rack/utils'
2011-09-27 12:23:03 -04:00
module Puma
2011-12-01 18:23:14 -05:00
# Handles invoke a Puma::Server in a command line style.
#
2011-09-27 12:23:03 -04:00
class CLI
2011-12-01 18:23:14 -05:00
# Create a new CLI object using +argv+ as the command line
# arguments.
#
# +stdout+ and +stderr+ can be set to IO-like objects which
# this object will report status on.
#
def initialize(argv, events=Events.stdio)
2012-08-01 13:11:27 -04:00
@debug = false
2011-09-27 12:23:03 -04:00
@argv = argv
@events = events
2011-09-27 17:33:17 -04:00
@status = nil
@runner = nil
@config = nil
ENV['NEWRELIC_DISPATCHER'] ||= "puma"
2011-09-27 12:23:03 -04:00
setup_options
generate_restart_data
@binder = Binder.new(@events)
@binder.import_from_env
end
# The Binder object containing the sockets bound to.
attr_reader :binder
# The Configuration object used.
attr_reader :config
# The Hash of options used to configure puma.
attr_reader :options
2013-02-05 19:47:18 -05:00
# The Events object used to output information.
attr_reader :events
2011-09-27 12:23:03 -04:00
2012-07-02 18:09:35 -04:00
# Delegate +log+ to +@events+
2011-12-01 18:23:14 -05:00
#
def log(str)
2012-07-02 18:09:35 -04:00
@events.log str
end
2012-07-02 18:09:35 -04:00
# Delegate +error+ to +@events+
2011-12-01 18:23:14 -05:00
#
def error(str)
2012-07-02 18:09:35 -04:00
@events.error str
end
2012-08-01 13:11:27 -04:00
def debug(str)
if @options[:debug]
2012-08-01 13:11:27 -04:00
@events.log "- #{str}"
end
end
def jruby?
IS_JRUBY
end
def windows?
RUBY_PLATFORM =~ /mswin32|ming32/
end
def unsupported(str, cond=true)
return unless cond
@events.error str
raise UnsupportedOption
end
2011-12-01 18:23:14 -05:00
# Build the OptionParser object to handle the available options.
#
2011-09-27 12:23:03 -04:00
def setup_options
2011-09-27 13:53:45 -04:00
@options = {
2011-09-27 17:33:17 -04:00
:min_threads => 0,
:max_threads => 16,
:quiet => false,
:debug => false,
2012-08-05 15:33:21 -04:00
:binds => [],
:workers => 0,
:daemon => false,
:worker_boot => []
2011-09-27 13:53:45 -04:00
}
2011-09-27 12:23:03 -04:00
2011-09-27 13:53:45 -04:00
@parser = OptionParser.new do |o|
2012-08-01 13:11:27 -04:00
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
@options[:binds] << arg
end
o.on "-C", "--config PATH", "Load PATH as a config file" do |arg|
@options[:config_file] = arg
end
2012-09-25 12:59:07 -04:00
o.on "--control URL", "The bind url to use for the control server",
"Use 'auto' to use temp unix server" do |arg|
if arg
@options[:control_url] = arg
elsif jruby?
unsupported "No default url available on JRuby"
end
end
o.on "--control-token TOKEN",
"The token to use as authentication for the control server" do |arg|
@options[:control_auth_token] = arg
end
o.on "-d", "--daemon", "Daemonize the server into the background" do
@options[:daemon] = true
@options[:quiet] = true
end
2012-09-25 12:59:07 -04:00
o.on "--debug", "Log lowlevel debugging information" do
@options[:debug] = true
end
o.on "--dir DIR", "Change to DIR before starting" do |d|
@options[:directory] = d.to_s
@options[:worker_directory] = d.to_s
end
2012-09-25 12:59:07 -04:00
o.on "-e", "--environment ENVIRONMENT",
"The environment to run the Rack app on (default development)" do |arg|
@options[:environment] = arg
end
o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
$LOAD_PATH.unshift(*arg.split(':'))
end
2013-07-16 16:35:19 -04:00
o.on "-p", "--port PORT", "Define the TCP port to bind to",
2012-01-13 18:41:22 -05:00
"Use -b for more advanced options" do |arg|
2012-02-22 13:19:03 -05:00
@options[:binds] << "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
2012-01-13 18:41:22 -05:00
end
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
@options[:pidfile] = arg
end
o.on "--preload", "Preload the app. Cluster mode only" do
@options[:preload_app] = true
end
o.on "-q", "--quiet", "Quiet down the output" do
@options[:quiet] = true
end
2013-02-05 19:47:09 -05:00
o.on "-R", "--restart-cmd CMD",
2012-09-25 12:59:07 -04:00
"The puma command to run during a hot restart",
"Default: inferred" do |cmd|
@options[:restart_cmd] = cmd
end
o.on "-S", "--state PATH", "Where to store the state details" do |arg|
@options[:state] = arg
end
2011-09-27 17:33:17 -04:00
o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
min, max = arg.split(":")
if max
@options[:min_threads] = min
@options[:max_threads] = max
2011-09-27 17:33:17 -04:00
else
@options[:min_threads] = 0
@options[:max_threads] = arg
2011-09-27 17:33:17 -04:00
end
2011-09-27 13:53:45 -04:00
end
o.on "--tcp-mode", "Run the app in raw TCP mode instead of HTTP mode" do
@options[:mode] = :tcp
end
2013-02-08 21:23:30 -05:00
o.on "-V", "--version", "Print the version information" do
puts "puma version #{Puma::Const::VERSION}"
exit 1
end
2012-08-05 15:33:21 -04:00
o.on "-w", "--workers COUNT",
"Activate cluster mode: How many worker processes to create" do |arg|
@options[:workers] = arg.to_i
end
2011-09-27 12:23:03 -04:00
end
2011-09-27 13:53:45 -04:00
@parser.banner = "puma <options> <rackup file>"
2011-09-27 12:23:03 -04:00
2011-09-27 13:53:45 -04:00
@parser.on_tail "-h", "--help", "Show help" do
log @parser
2011-09-27 12:23:03 -04:00
exit 1
end
end
def write_state
write_pid
require 'yaml'
if path = @options[:state]
state = { "pid" => Process.pid }
cfg = @config.dup
[ :logger, :worker_boot, :on_restart ].each { |o| cfg.options.delete o }
state["config"] = cfg
File.open(path, "w") do |f|
f.write state.to_yaml
end
end
end
2011-12-01 18:23:14 -05:00
# If configured, write the pid of the current process out
# to a file.
#
2011-11-22 00:15:40 -05:00
def write_pid
if path = @options[:pidfile]
File.open(path, "w") do |f|
f.puts Process.pid
end
cur = Process.pid
at_exit do
if cur == Process.pid
delete_pidfile
end
end
2011-11-22 00:15:40 -05:00
end
end
def set_rack_environment
2012-07-19 19:38:16 -04:00
# Try the user option first, then the environment variable,
# finally default to development
env = @options[:environment] ||
ENV['RACK_ENV'] ||
'development'
@options[:environment] = env
ENV['RACK_ENV'] = env
end
def delete_pidfile
if path = @options[:pidfile]
2014-01-25 17:50:40 -05:00
File.unlink path if File.exist? path
end
end
def find_config
if cfg = @options[:config_file]
# Allow - to disable config finding
if cfg == "-"
@options[:config_file] = nil
return
end
return
end
pos = []
if env = (@options[:environment] || ENV['RACK_ENV'])
pos << "config/puma/#{env}.rb"
end
pos << "config/puma.rb"
2014-01-25 17:50:40 -05:00
@options[:config_file] = pos.find { |f| File.exist? f }
end
2011-12-01 18:23:14 -05:00
# :nodoc:
2011-11-22 00:15:40 -05:00
def parse_options
2011-09-27 13:53:45 -04:00
@parser.parse! @argv
2011-12-07 16:42:53 -05:00
if @argv.last
@options[:rackup] = @argv.shift
end
find_config
@config = Puma::Configuration.new @options
# Advertise the Configuration
Puma.cli_config = @config
@config.load
2013-07-06 00:13:29 -04:00
if clustered?
unsupported "worker mode not supported on JRuby or Windows",
jruby? || windows?
end
2013-07-05 20:09:18 -04:00
if @options[:daemon] and windows?
unsupported "daemon mode not supported on Windows"
end
2013-07-06 00:13:29 -04:00
end
2013-07-05 20:09:18 -04:00
2013-07-06 00:13:29 -04:00
def clustered?
@options[:workers] > 0
2011-11-22 00:15:40 -05:00
end
def graceful_stop
@runner.stop_blocked
log "- Goodbye!"
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 s_env.dev == s_pwd.dev
@restart_dir = dir
@options[:worker_directory] = dir
end
end
@restart_dir ||= Dir.pwd
@original_argv = ARGV.dup
if defined? Rubinius::OS_ARGV
@restart_argv = Rubinius::OS_ARGV
else
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.
#
2014-01-25 17:50:40 -05:00
if File.exist?($0)
arg0 = [Gem.ruby, $0]
else
arg0 = [Gem.ruby, "-S", $0]
end
2012-10-13 13:15:59 -04:00
# Detect and reinject -Ilib from the command line
lib = File.expand_path "lib"
arg0[1,0] = ["-I", lib] if $:[0] == lib
@restart_argv = arg0 + ARGV
2012-10-13 13:15:59 -04:00
end
end
2012-10-13 13:15:59 -04:00
def restart_args
if cmd = @options[:restart_cmd]
cmd.split(' ') + @original_argv
else
@restart_argv
2012-10-13 13:15:59 -04:00
end
end
2013-07-05 19:53:52 -04:00
def jruby_daemon_start
require 'puma/jruby_restart'
JRubyRestart.daemon_start(@restart_dir, restart_args)
end
def restart!
@options[:on_restart].each do |block|
block.call self
end
if jruby?
@binder.listeners.each_with_index do |(str,io),i|
io.close
# We have to unlink a unix socket path that's not being used
uri = URI.parse str
if uri.scheme == "unix"
path = "#{uri.host}#{uri.path}"
File.unlink path
end
end
require 'puma/jruby_restart'
JRubyRestart.chdir_exec(@restart_dir, restart_args)
elsif windows?
@binder.listeners.each_with_index do |(str,io),i|
io.close
# We have to unlink a unix socket path that's not being used
uri = URI.parse str
if uri.scheme == "unix"
path = "#{uri.host}#{uri.path}"
File.unlink path
end
end
argv = restart_args
Dir.chdir @restart_dir
argv += [redirects] unless 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] unless RUBY_VERSION < '1.9'
Kernel.exec(*argv)
end
end
2013-07-06 00:13:29 -04:00
2011-12-01 18:23:14 -05:00
# Parse the options, load the rackup, start the server and wait
# for it to finish.
#
2011-11-22 00:15:40 -05:00
def run
begin
parse_options
rescue UnsupportedOption
exit 1
end
2011-09-27 12:23:03 -04:00
if dir = @options[:directory]
Dir.chdir dir
end
2013-07-06 00:13:29 -04:00
set_rack_environment
2013-07-06 00:13:29 -04:00
if clustered?
2012-08-05 15:33:21 -04:00
@events = PidEvents.new STDOUT, STDERR
@options[:logger] = @events
2011-09-27 12:23:03 -04:00
@runner = Cluster.new(self)
2012-08-05 15:33:21 -04:00
else
@runner = Single.new(self)
2012-08-05 15:33:21 -04:00
end
2012-10-13 13:15:59 -04:00
setup_signals
@status = :run
@runner.run
case @status
when :halt
log "* Stopping immediately!"
when :run, :stop
graceful_stop
when :restart
log "* Restarting..."
@runner.before_restart
restart!
when :exit
# nothing
end
end
def setup_signals
2012-06-21 12:01:18 -04:00
begin
Signal.trap "SIGUSR2" do
restart
2012-06-21 12:01:18 -04:00
end
rescue Exception
log "*** SIGUSR2 not implemented, signal based restart unavailable!"
end
begin
Signal.trap "SIGUSR1" do
phased_restart
end
rescue Exception
log "*** SIGUSR1 not implemented, signal based restart unavailable!"
end
2012-06-21 12:01:18 -04:00
begin
Signal.trap "SIGTERM" do
2013-07-06 00:13:29 -04:00
stop
2012-06-21 12:01:18 -04:00
end
rescue Exception
log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
end
if jruby?
Signal.trap("INT") do
@status = :exit
graceful_stop
exit
end
end
2011-09-27 12:23:03 -04:00
end
def stop
@status = :stop
@runner.stop
2012-08-05 15:33:21 -04:00
end
def restart
@status = :restart
@runner.restart
2013-02-05 22:09:40 -05:00
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
2012-08-05 15:33:21 -04:00
end
def stats
@runner.stats
2012-08-05 15:33:21 -04:00
end
def halt
@status = :halt
@runner.halt
end
2011-09-27 12:23:03 -04:00
end
end