From ae47e5afc5674c13bdbff3afc887a1505f15bd4f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 29 Mar 2009 22:22:11 -0700 Subject: Fix default listener setup Combining command-line and config file options in a reasonable manner has and always will be a painful experience. --- bin/unicorn | 11 +++++------ bin/unicorn_rails | 10 +++++----- lib/unicorn.rb | 7 +++++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bin/unicorn b/bin/unicorn index 9deb872..da77198 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -6,8 +6,8 @@ env = "development" daemonize = false listeners = [] options = { :listeners => listeners } -host = Unicorn::Const::DEFAULT_HOST -port = Unicorn::Const::DEFAULT_PORT +host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT +set_listener = false opts = OptionParser.new("", 24, ' ') do |opts| opts.banner = "Usage: #{File.basename($0)} " \ @@ -46,11 +46,13 @@ opts = OptionParser.new("", 24, ' ') do |opts| opts.on("-o", "--host HOST", "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h| host = h + set_listener = true end opts.on("-p", "--port PORT", "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p| port = p.to_i + set_listener = true end opts.on("-E", "--env ENVIRONMENT", @@ -148,10 +150,7 @@ else inner_app end -if listeners.empty? - listener = "#{host}:#{port}" - listeners << listener -end +listeners << "#{host}:#{port}" if set_listener if $DEBUG pp({ diff --git a/bin/unicorn_rails b/bin/unicorn_rails index fae6f4b..172d572 100755 --- a/bin/unicorn_rails +++ b/bin/unicorn_rails @@ -10,7 +10,8 @@ daemonize = false static = true listeners = [] options = { :listeners => listeners } -host, port = Unicorn::Const::DEFAULT_HOST, 3000 +host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT +set_listener = false ENV['RAILS_ENV'] ||= "development" map_path = ENV['RAILS_RELATIVE_URL_ROOT'] @@ -50,10 +51,12 @@ opts = OptionParser.new("", 24, ' ') do |opts| opts.on("-o", "--host HOST", "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h| host = h + set_listener = true end opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p| port = p.to_i + set_listener = true end opts.on("-E", "--env ENVIRONMENT", @@ -187,10 +190,7 @@ app = lambda do || end.to_app end -if listeners.empty? - listener = "#{host}:#{port}" - listeners << listener -end +listeners << "#{host}:#{port}" if set_listener if $DEBUG pp({ diff --git a/lib/unicorn.rb b/lib/unicorn.rb index f99b433..7ffc8a5 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -86,6 +86,9 @@ module Unicorn # share the same OS-level file descriptor as the higher-level *Server # objects; we need to prevent Socket objects from being garbage-collected config_listeners -= listener_names + if config_listeners.empty? && @listeners.empty? + config_listeners << Unicorn::Const::DEFAULT_LISTEN + end config_listeners.each { |addr| listen(addr) } raise ArgumentError, "no listeners" if @listeners.empty? self.pid = @config[:pid] @@ -133,10 +136,10 @@ module Unicorn # add a given address to the +listeners+ set, idempotently # Allows workers to add a private, per-process listener via the # @after_fork hook. Very useful for debugging and testing. - def listen(address) + def listen(address, opt = {}.merge(@listener_opts[address] || {})) return if String === address && listener_names.include?(address) - if io = bind_listen(address, @listener_opts[address] || {}) + if io = bind_listen(address, opt) if Socket == io.class @io_purgatory << io io = server_cast(io) -- cgit v1.2.3-24-ge0c7