From 4accf4449390c649bce0b1c9d84314d65fd41f8e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 10 Jun 2010 08:47:10 +0000 Subject: respect "working_directory" wrt config.ru Since we added support for the "working_directory" parameter, it often became unclear where/when certain paths would be bound. There are some extremely nasty dependencies and ordering issues when doing this. It's all pretty fragile, but works for now and we even have a full integration test to keep it working. I plan on cleaning this up 2.x.x to be less offensive to look at (Rainbows! and Zbatery are a bit tied to this at the moment). Thanks to Pierre Baillet for reporting this. ref: http://mid.gmane.org/AANLkTimKb7JARr_69nfVrJLvMZH3Gvs1o_KwZFLKfuxy@mail.gmail.com --- lib/unicorn.rb | 7 +++--- lib/unicorn/configurator.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++ lib/unicorn/launcher.rb | 5 ++-- 3 files changed, 62 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/unicorn.rb b/lib/unicorn.rb index c026236..a7b0646 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -33,11 +33,10 @@ module Unicorn # Unicorn config). The returned lambda will be called when it is # time to build the app. def builder(ru, opts) - if ru =~ /\.ru\z/ - # parse embedded command-line options in config.ru comments - /^#\\(.*)/ =~ File.read(ru) and opts.parse!($1.split(/\s+/)) - end + # allow Configurator to parse cli switches embedded in the ru file + Unicorn::Configurator::RACKUP.update(:file => ru, :optparse => opts) + # always called after config file parsing, may be called after forking lambda do || inner_app = case ru when /\.ru$/ diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb index e4305c2..0716e64 100644 --- a/lib/unicorn/configurator.rb +++ b/lib/unicorn/configurator.rb @@ -13,6 +13,12 @@ module Unicorn # nginx is also available at # http://unicorn.bogomips.org/examples/nginx.conf class Configurator < Struct.new(:set, :config_file, :after_reload) + # :stopdoc: + # used to stash stuff for deferred processing of cli options in + # config.ru after "working_directory" is bound. Do not rely on + # this being around later on... + RACKUP = {} + # :startdoc: # Default settings for Unicorn DEFAULTS = { @@ -51,6 +57,8 @@ module Unicorn def reload #:nodoc: instance_eval(File.read(config_file), config_file) if config_file + parse_rackup_file + # working_directory binds immediately (easier error checking that way), # now ensure any paths we changed are correctly set. [ :pid, :stderr_path, :stdout_path ].each do |var| @@ -66,6 +74,9 @@ module Unicorn def commit!(server, options = {}) #:nodoc: skip = options[:skip] || [] + if ready_pipe = RACKUP.delete(:ready_pipe) + server.ready_pipe = ready_pipe + end set.each do |key, value| value == :unset and next skip.include?(key) and next @@ -411,5 +422,50 @@ module Unicorn set[var] = my_proc end + # this is called _after_ working_directory is bound. This only + # parses the embedded switches in .ru files + # (for "rackup" compatibility) + def parse_rackup_file # :nodoc: + ru = RACKUP[:file] or return # we only return here in unit tests + + # :rails means use (old) Rails autodetect + if ru == :rails + File.readable?('config.ru') or return + ru = 'config.ru' + end + + File.readable?(ru) or + raise ArgumentError, "rackup file (#{ru}) not readable" + + # it could be a .rb file, too, we don't parse those manually + ru =~ /\.ru\z/ or return + + /^#\\(.*)/ =~ File.read(ru) or return + warn "ru cli opts: #{$1}" + RACKUP[:optparse].parse!($1.split(/\s+/)) + + # XXX ugly as hell, WILL FIX in 2.x (along with Rainbows!/Zbatery) + host, port, set_listener, options, daemonize = + eval("[ host, port, set_listener, options, daemonize ]", + TOPLEVEL_BINDING) + + warn [ :host, :port, :set_listener, :options, :daemonize ].inspect + warn [ ru, host, port, set_listener, options, daemonize ].inspect + # XXX duplicate code from bin/unicorn{,_rails} + set[:listeners] << "#{host}:#{port}" if set_listener + + if daemonize + # unicorn_rails wants a default pid path, (not plain 'unicorn') + if ru == :rails + spid = set[:pid] + pid('tmp/pids/unicorn.pid') if spid.nil? || spid == :unset + end + unless RACKUP[:daemonized] + Unicorn::Launcher.daemonize!(options) + RACKUP[:ready_pipe] = options.delete(:ready_pipe) + end + end + end + end end diff --git a/lib/unicorn/launcher.rb b/lib/unicorn/launcher.rb index 5ab04c7..7f3ffa6 100644 --- a/lib/unicorn/launcher.rb +++ b/lib/unicorn/launcher.rb @@ -56,8 +56,9 @@ class Unicorn::Launcher end end # $stderr/$stderr can/will be redirected separately in the Unicorn config - Unicorn::Configurator::DEFAULTS[:stderr_path] = "/dev/null" - Unicorn::Configurator::DEFAULTS[:stdout_path] = "/dev/null" + Unicorn::Configurator::DEFAULTS[:stderr_path] ||= "/dev/null" + Unicorn::Configurator::DEFAULTS[:stdout_path] ||= "/dev/null" + Unicorn::Configurator::RACKUP[:daemonized] = true end end -- cgit v1.2.3-24-ge0c7