Skip to content

Commit

Permalink
refactor configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
chulkilee committed Mar 15, 2015
1 parent 67fa78e commit c8abca0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 123 deletions.
99 changes: 48 additions & 51 deletions lib/puma/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def debug(str)
@events.log "- #{str}" if @options[:debug]
end

def clustered?
@options[:workers] > 0
end

def prune_bundler?
@options[:prune_bundler] && clustered? && !@options[:preload_app]
end

def jruby?
IS_JRUBY
end
Expand All @@ -88,10 +96,8 @@ def windows?
RUBY_PLATFORM =~ /mswin32|ming32/
end

def unsupported(str, cond=true)
return unless cond
@events.error str
raise UnsupportedOption
def env
@options[:environment] || ENV['RACK_ENV'] || 'development'
end

def write_state
Expand Down Expand Up @@ -129,59 +135,17 @@ def write_pid
end
end

def env
# Try the user option first, then the environment variable,
# finally default to development
@options[:environment] || ENV['RACK_ENV'] || 'development'
end

def delete_pidfile
path = @options[:pidfile]
File.unlink(path) if path && File.exist?(path)
end

# :nodoc:
def parse_options
@parser.parse! @argv

@options[:rackup] = @argv.shift if @argv.last

find_config

@config = Puma::Configuration.new @options

# Advertise the Configuration
Puma.cli_config = @config

@config.load

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 clustered?
@options[:workers] > 0
end

def graceful_stop
@runner.stop_blocked
log "=== puma shutdown: #{Time.now} ==="
log "- Goodbye!"
end

def restart_args
if cmd = @options[:restart_cmd]
cmd.split(' ') + @original_argv
else
@restart_argv
end
end

def jruby_daemon_start
require 'puma/jruby_restart'
JRubyRestart.daemon_start(@restart_dir, restart_args)
Expand Down Expand Up @@ -218,10 +182,6 @@ def restart!
end
end

def prune_bundler?
@options[:prune_bundler] && clustered? && !@options[:preload_app]
end

# Parse the options, load the rackup, start the server and wait
# for it to finish.
#
Expand Down Expand Up @@ -311,6 +271,20 @@ def title
buffer
end

def unsupported(str)
@events.error(str)
raise UnsupportedOption
end

def restart_args
cmd = @options[:restart_cmd]
if cmd
cmd.split(' ') + @original_argv
else
@restart_argv
end
end

def set_process_title
Process.respond_to?(:setproctitle) ? Process.setproctitle(title) : $0 = title
end
Expand Down Expand Up @@ -556,6 +530,29 @@ def close_binder_listeners
end
end

def parse_options
@parser.parse! @argv

@options[:rackup] = @argv.shift if @argv.last

find_config

@config = Puma::Configuration.new @options

# Advertise the Configuration
Puma.cli_config = @config

@config.load

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 prune_bundler
return unless defined?(Bundler)
puma = Bundler.rubygems.loaded_specs("puma")
Expand All @@ -577,7 +574,7 @@ def prune_bundler
Bundler.with_clean_env do
ENV['GEM_HOME'] = home
wild = File.expand_path(File.join(puma_lib_dir, "../bin/puma-wild"))
args = [Gem.ruby, wild, '-I', dirs.join(":"), deps.join(',')] + @original_argv
args = [Gem.ruby, wild, '-I', dirs.join(':'), deps.join(',')] + @original_argv
Kernel.exec(*args)
end
end
Expand Down
122 changes: 59 additions & 63 deletions lib/puma/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,9 @@ def initialize_copy(other)
def load
DSL.load(@options, @options[:config_file])

# Rakeup default option support
if host = @options[:Host]
port = @options[:Port] || DefaultTCPPort

@options[:binds] << "tcp://#{host}:#{port}"
end

if @options[:binds].empty?
@options[:binds] << "tcp://#{DefaultTCPHost}:#{DefaultTCPPort}"
end

if @options[:control_url] == "auto"
path = Configuration.temp_path
@options[:control_url] = "unix://#{path}"
@options[:control_url_temp] = path
end

unless @options[:control_auth_token]
setup_random_token
end

unless @options[:tag]
@options[:tag] = infer_tag
end

@options[:binds].uniq!
end

def infer_tag
File.basename Dir.getwd
setup_binds
setup_control
@options[:tag] ||= infer_tag
end

# Injects the Configuration object into the env
Expand Down Expand Up @@ -90,40 +63,73 @@ def rackup
# the rackup file, and set @app.
#
def app
app = @options[:app]
found = options[:app] || load_rackup

unless app
unless File.exist?(rackup)
raise "Missing rackup file '#{rackup}'"
end
if @options[:mode] == :tcp
require 'puma/tcp_logger'

logger = @options[:logger] || STDOUT
return TCPLogger.new(logger, found, @options[:quiet])
end

app, options = Rack::Builder.parse_file rackup
@options.merge! options
if !@options[:quiet] and @options[:environment] == "development"
logger = @options[:logger] || STDOUT
found = Rack::CommonLogger.new(found, logger)
end

config_ru_binds = []
ConfigMiddleware.new(self, found)
end

options.each do |key,val|
if key.to_s[0,4] == "bind"
config_ru_binds << val
end
end
def self.temp_path
require 'tmpdir'

@options[:binds] = config_ru_binds unless config_ru_binds.empty?
t = (Time.now.to_f * 1000).to_i
"#{Dir.tmpdir}/puma-status-#{t}-#{$$}"
end

private

def infer_tag
File.basename(Dir.getwd)
end

def load_rackup
raise "Missing rackup file '#{rackup}'" unless File.exist?(rackup)

rack_app, rack_options = Rack::Builder.parse_file(rackup)
@options.merge!(rack_options)

config_ru_binds = rack_options.each_with_object([]) do |(k, v), b|
b << v if k.start_with?('bind')
end
@options[:binds] = config_ru_binds unless config_ru_binds.empty?

if @options[:mode] == :tcp
require 'puma/tcp_logger'
rack_app
end

logger = @options[:logger] || STDOUT
return TCPLogger.new(logger, app, @options[:quiet])
def setup_binds
# Rakeup default option support
host = @options[:Host]
if host
port = @options[:Port] || DefaultTCPPort
@options[:binds] << "tcp://#{host}:#{port}"
end

if !@options[:quiet] and @options[:environment] == "development"
logger = @options[:logger] || STDOUT
app = Rack::CommonLogger.new(app, logger)
if @options[:binds].empty?
@options[:binds] << "tcp://#{DefaultTCPHost}:#{DefaultTCPPort}"
end

@options[:binds].uniq!
end

def setup_control
if @options[:control_url] == 'auto'
path = Configuration.temp_path
@options[:control_url] = "unix://#{path}"
@options[:control_url_temp] = path
end

return ConfigMiddleware.new(self, app)
setup_random_token unless @options[:control_auth_token]
end

def setup_random_token
Expand All @@ -139,9 +145,7 @@ def setup_random_token
if defined? OpenSSL::Random
bytes = OpenSSL::Random.random_bytes(count)
elsif File.exist?("/dev/urandom")
File.open("/dev/urandom") do |f|
bytes = f.read(count)
end
File.open('/dev/urandom') { |f| bytes = f.read(count) }
end

if bytes
Expand All @@ -153,13 +157,5 @@ def setup_random_token

@options[:control_auth_token] = token
end

def self.temp_path
require 'tmpdir'

t = (Time.now.to_f * 1000).to_i
"#{Dir.tmpdir}/puma-status-#{t}-#{$$}"
end

end
end
18 changes: 9 additions & 9 deletions test/test_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def teardown

def test_pid_file
cli = Puma::CLI.new ["--pidfile", @tmp_path]
cli.parse_options
cli.send(:parse_options)
cli.write_pid

assert_equal File.read(@tmp_path).strip.to_i, Process.pid
Expand All @@ -48,7 +48,7 @@ def test_control_for_tcp
"--control-token", "",
"test/lobster.ru"], @events

cli.parse_options
cli.send(:parse_options)

thread_exception = nil
t = Thread.new do
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_control
"--control", url,
"--control-token", "",
"test/lobster.ru"], @events
cli.parse_options
cli.send(:parse_options)

t = Thread.new { cli.run }

Expand All @@ -102,7 +102,7 @@ def test_control_stop
"--control", url,
"--control-token", "",
"test/lobster.ru"], @events
cli.parse_options
cli.send(:parse_options)

t = Thread.new { cli.run }

Expand All @@ -120,7 +120,7 @@ def test_control_stop
def test_tmp_control
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"]
cli.parse_options
cli.send(:parse_options)
cli.write_state

data = YAML.load File.read(@tmp_path)
Expand All @@ -138,7 +138,7 @@ def test_tmp_control
def test_state
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
cli.parse_options
cli.send(:parse_options)
cli.write_state

data = YAML.load File.read(@tmp_path)
Expand All @@ -149,13 +149,13 @@ def test_state

def test_load_path
cli = Puma::CLI.new ["--include", 'foo/bar']
cli.parse_options
cli.send(:parse_options)

assert_equal 'foo/bar', $LOAD_PATH[0]
$LOAD_PATH.shift

cli = Puma::CLI.new ["--include", 'foo/bar:baz/qux']
cli.parse_options
cli.send(:parse_options)

assert_equal 'foo/bar', $LOAD_PATH[0]
$LOAD_PATH.shift
Expand All @@ -165,7 +165,7 @@ def test_load_path

def test_environment
cli = Puma::CLI.new ["--environment", @environment]
cli.parse_options
cli.send(:parse_options)
cli.send(:set_rack_environment)

assert_equal ENV['RACK_ENV'], @environment
Expand Down

0 comments on commit c8abca0

Please sign in to comment.