mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Remove config from CLI
Don't store @cli_options in Launcher (there are too many "options" hashes and their roles are confusing, let's make them more obvious, the arguments passed to cli_options are parsed and turned into @options).
This commit is contained in:
parent
ffae12c40f
commit
256e886a45
2 changed files with 64 additions and 121 deletions
|
@ -47,8 +47,6 @@ module Puma
|
||||||
@status = nil
|
@status = nil
|
||||||
@runner = nil
|
@runner = nil
|
||||||
|
|
||||||
@config = nil
|
|
||||||
|
|
||||||
setup_options
|
setup_options
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -59,11 +57,6 @@ module Puma
|
||||||
end
|
end
|
||||||
|
|
||||||
@launcher = Puma::Launcher.new(@cli_options, events: @events, argv: @argv)
|
@launcher = Puma::Launcher.new(@cli_options, events: @events, argv: @argv)
|
||||||
|
|
||||||
@launcher.config = self.config
|
|
||||||
|
|
||||||
@launcher.setup(@options)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
## BACKWARDS COMPAT FOR TESTS
|
## BACKWARDS COMPAT FOR TESTS
|
||||||
|
@ -118,10 +111,14 @@ module Puma
|
||||||
end
|
end
|
||||||
|
|
||||||
# The Configuration object used.
|
# The Configuration object used.
|
||||||
attr_reader :config
|
def config
|
||||||
|
@launcher.config
|
||||||
|
end
|
||||||
|
|
||||||
# The Hash of options used to configure puma.
|
# The Hash of options used to configure puma.
|
||||||
attr_reader :options
|
def options
|
||||||
|
@launcher.options
|
||||||
|
end
|
||||||
|
|
||||||
# The Events object used to output information.
|
# The Events object used to output information.
|
||||||
attr_reader :events
|
attr_reader :events
|
||||||
|
@ -139,54 +136,10 @@ module Puma
|
||||||
Puma.windows?
|
Puma.windows?
|
||||||
end
|
end
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
def env
|
def env
|
||||||
@options[:environment] || @cli_options[:environment] || ENV['RACK_ENV'] || 'development'
|
@launcher.env
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_state
|
|
||||||
write_pid
|
|
||||||
|
|
||||||
path = @options[:state]
|
|
||||||
return unless path
|
|
||||||
|
|
||||||
state = { 'pid' => Process.pid }
|
|
||||||
cfg = @config.dup
|
|
||||||
|
|
||||||
KEYS_NOT_TO_PERSIST_IN_STATE.each { |k| cfg.options.delete(k) }
|
|
||||||
state['config'] = cfg
|
|
||||||
|
|
||||||
require 'yaml'
|
|
||||||
File.open(path, 'w') { |f| f.write state.to_yaml }
|
|
||||||
end
|
|
||||||
|
|
||||||
# If configured, write the pid of the current process out
|
|
||||||
# to a file.
|
|
||||||
#
|
|
||||||
def write_pid
|
|
||||||
path = @options[:pidfile]
|
|
||||||
return unless path
|
|
||||||
|
|
||||||
File.open(path, 'w') { |f| f.puts Process.pid }
|
|
||||||
cur = Process.pid
|
|
||||||
at_exit do
|
|
||||||
delete_pidfile if cur == Process.pid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete_pidfile
|
|
||||||
path = @options[:pidfile]
|
|
||||||
File.unlink(path) if path && File.exist?(path)
|
|
||||||
end
|
|
||||||
|
|
||||||
def graceful_stop
|
|
||||||
@runner.stop_blocked
|
|
||||||
log "=== puma shutdown: #{Time.now} ==="
|
|
||||||
log "- Goodbye!"
|
|
||||||
end
|
|
||||||
|
|
||||||
=======
|
|
||||||
>>>>>>> Initial Seperation of CLI and Server Launcher work
|
|
||||||
def jruby_daemon_start
|
def jruby_daemon_start
|
||||||
@launcher.jruby_daemon_start
|
@launcher.jruby_daemon_start
|
||||||
end
|
end
|
||||||
|
@ -233,7 +186,6 @@ module Puma
|
||||||
|
|
||||||
def setup_options
|
def setup_options
|
||||||
@cli_options = {}
|
@cli_options = {}
|
||||||
@options = {}
|
|
||||||
|
|
||||||
@parser = OptionParser.new do |o|
|
@parser = OptionParser.new do |o|
|
||||||
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
|
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
|
||||||
|
|
|
@ -2,18 +2,68 @@ require 'puma/binder'
|
||||||
|
|
||||||
module Puma
|
module Puma
|
||||||
class Launcher
|
class Launcher
|
||||||
|
|
||||||
def initialize(cli_options = {}, launcher_options = {})
|
def initialize(cli_options = {}, launcher_options = {})
|
||||||
@cli_options = cli_options
|
|
||||||
@runner = nil
|
@runner = nil
|
||||||
@events = launcher_options[:events] or raise "must provide :events key"
|
@events = launcher_options[:events] or raise "must provide :events key"
|
||||||
@argv = launcher_options[:argv] || "puma"
|
@argv = launcher_options[:argv] || "puma"
|
||||||
@original_argv = @argv.dup
|
@original_argv = @argv.dup
|
||||||
|
@config = nil
|
||||||
|
|
||||||
@binder = Binder.new(@events)
|
@binder = Binder.new(@events)
|
||||||
@binder.import_from_env
|
@binder.import_from_env
|
||||||
|
|
||||||
|
# Final internal representation of options is stored here
|
||||||
|
# cli_options will be parsed
|
||||||
|
@options = {}
|
||||||
|
generate_restart_data
|
||||||
|
|
||||||
|
@env = @options[:environment] || ENV['RACK_ENV'] || 'development'
|
||||||
|
|
||||||
|
if cli_options[:config_file] == '-'
|
||||||
|
cli_options[:config_file] = nil
|
||||||
|
else
|
||||||
|
cli_options[:config_file] ||= %W(config/puma/#{@env}.rb config/puma.rb).find { |f| File.exist?(f) }
|
||||||
|
end
|
||||||
|
|
||||||
|
@config = Puma::Configuration.new(cli_options)
|
||||||
|
|
||||||
|
# Advertise the Configuration
|
||||||
|
Puma.cli_config = @config
|
||||||
|
|
||||||
|
@config.load
|
||||||
|
|
||||||
|
@options = @config.options
|
||||||
|
|
||||||
|
if clustered? && (jruby? || windows?)
|
||||||
|
unsupported 'worker mode not supported on JRuby or Windows'
|
||||||
|
end
|
||||||
|
|
||||||
|
if @options[:daemon] && windows?
|
||||||
|
unsupported 'daemon mode not supported on Windows'
|
||||||
|
end
|
||||||
|
|
||||||
|
dir = @options[:directory]
|
||||||
|
Dir.chdir(dir) if dir
|
||||||
|
|
||||||
|
prune_bundler if prune_bundler?
|
||||||
|
|
||||||
|
@env = @options[:environment] if @options[:environment]
|
||||||
|
set_rack_environment
|
||||||
|
|
||||||
|
if clustered?
|
||||||
|
@events.formatter = Events::PidFormatter.new
|
||||||
|
@options[:logger] = @events
|
||||||
|
|
||||||
|
@runner = Cluster.new(self)
|
||||||
|
else
|
||||||
|
@runner = Single.new(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
@status = :run
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :binder, :events
|
attr_reader :binder, :events, :config, :options
|
||||||
|
|
||||||
## THIS STUFF IS NEEDED FOR RUNNER
|
## THIS STUFF IS NEEDED FOR RUNNER
|
||||||
|
|
||||||
|
@ -23,10 +73,6 @@ module Puma
|
||||||
@events.log str
|
@events.log str
|
||||||
end
|
end
|
||||||
|
|
||||||
def config
|
|
||||||
@config
|
|
||||||
end
|
|
||||||
|
|
||||||
def stats
|
def stats
|
||||||
@runner.stats
|
@runner.stats
|
||||||
end
|
end
|
||||||
|
@ -93,33 +139,6 @@ module Puma
|
||||||
attr_accessor :options, :binder, :config
|
attr_accessor :options, :binder, :config
|
||||||
## THIS STUFF IS NEEDED FOR RUNNER
|
## THIS STUFF IS NEEDED FOR RUNNER
|
||||||
|
|
||||||
|
|
||||||
def setup(options)
|
|
||||||
@options = options
|
|
||||||
generate_restart_data
|
|
||||||
|
|
||||||
parse_options
|
|
||||||
|
|
||||||
dir = @options[:directory]
|
|
||||||
Dir.chdir(dir) if dir
|
|
||||||
|
|
||||||
prune_bundler if prune_bundler?
|
|
||||||
|
|
||||||
set_rack_environment
|
|
||||||
|
|
||||||
if clustered?
|
|
||||||
@events.formatter = Events::PidFormatter.new
|
|
||||||
@options[:logger] = @events
|
|
||||||
|
|
||||||
@runner = Cluster.new(self)
|
|
||||||
else
|
|
||||||
@runner = Single.new(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
@status = :run
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
attr_accessor :runner
|
attr_accessor :runner
|
||||||
|
|
||||||
def stop
|
def stop
|
||||||
|
@ -258,40 +277,15 @@ module Puma
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def parse_options
|
||||||
|
# backwards compat with CLI (private) interface
|
||||||
|
end
|
||||||
|
|
||||||
def unsupported(str)
|
def unsupported(str)
|
||||||
@events.error(str)
|
@events.error(str)
|
||||||
raise UnsupportedOption
|
raise UnsupportedOption
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options
|
|
||||||
find_config
|
|
||||||
|
|
||||||
@config = Puma::Configuration.new @cli_options
|
|
||||||
|
|
||||||
# Advertise the Configuration
|
|
||||||
Puma.cli_config = @config
|
|
||||||
|
|
||||||
@config.load
|
|
||||||
|
|
||||||
@options = @config.options
|
|
||||||
|
|
||||||
if clustered? && (jruby? || windows?)
|
|
||||||
unsupported 'worker mode not supported on JRuby or Windows'
|
|
||||||
end
|
|
||||||
|
|
||||||
if @options[:daemon] && windows?
|
|
||||||
unsupported 'daemon mode not supported on Windows'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_config
|
|
||||||
if @cli_options[:config_file] == '-'
|
|
||||||
@cli_options[:config_file] = nil
|
|
||||||
else
|
|
||||||
@cli_options[:config_file] ||= %W(config/puma/#{env}.rb config/puma.rb).find { |f| File.exist?(f) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def graceful_stop
|
def graceful_stop
|
||||||
@runner.stop_blocked
|
@runner.stop_blocked
|
||||||
log "=== puma shutdown: #{Time.now} ==="
|
log "=== puma shutdown: #{Time.now} ==="
|
||||||
|
@ -314,10 +308,7 @@ module Puma
|
||||||
end
|
end
|
||||||
|
|
||||||
def env
|
def env
|
||||||
@options[:environment] ||
|
@env
|
||||||
@cli_options[:environment] ||
|
|
||||||
ENV['RACK_ENV'] ||
|
|
||||||
'development'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def prune_bundler?
|
def prune_bundler?
|
||||||
|
|
Loading…
Add table
Reference in a new issue