From ecbff236982f0ac94555171f86cee217929a978a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 23 Feb 2009 16:10:02 -0800 Subject: Allow overriding :directory or via -C/--directory This allows Unicorn to be constantly started in symlink paths such as the ones Capistrano creates (e.g. "/u/apps/$app/current") --- bin/unicorn | 8 ++++++-- lib/unicorn.rb | 9 ++++++++- lib/unicorn/configurator.rb | 5 +++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/bin/unicorn b/bin/unicorn index f682311..dd4cc64 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -78,6 +78,10 @@ opts = OptionParser.new("", 24, ' ') do |opts| listeners << address end + opts.on("-C", "--directory PATH", "run in this directory") do |d| + options[:directory] = d + end + opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f| options[:config_file] = File.expand_path(f) end @@ -159,11 +163,11 @@ if daemonize exit if fork end - Dir.chdir("/") + Dir.chdir("/") # setting options[:directory] will override this later on File.umask(0000) STDIN.reopen("/dev/null") - # we can redirect these again in the Unicorn after_fork hook + # we can redirect these again in the Unicorn {before,after}_fork hooks STDOUT.reopen("/dev/null", "a") STDERR.reopen("/dev/null", "a") end diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 5b12fc8..aad4c3d 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -125,6 +125,13 @@ module Unicorn @pid = path end + # sets the path for running the master and worker process, useful for + # running and reexecuting from a symlinked path like Capistrano allows + def directory=(path) + Dir.chdir(path) if path + @directory = path + end + # 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. @@ -316,7 +323,7 @@ module Unicorn ENV.replace(@start_ctx[:environ]) ENV['UNICORN_FD'] = @listeners.map { |sock| sock.fileno }.join(',') File.umask(@start_ctx[:umask]) - Dir.chdir(@start_ctx[:cwd]) + Dir.chdir(@cwd || @start_ctx[:cwd]) cmd = [ @start_ctx[:zero] ] + @start_ctx[:argv] logger.info "executing #{cmd.inspect} (in #{Dir.pwd})" exec(*cmd) diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb index 9457480..ebdcba6 100644 --- a/lib/unicorn/configurator.rb +++ b/lib/unicorn/configurator.rb @@ -20,6 +20,7 @@ module Unicorn :before_fork => lambda { |server, worker_nr| server.logger.info("worker=#{worker_nr} spawning...") }, + :directory => nil, :pid => nil, :backlog => 1024, } @@ -137,6 +138,10 @@ module Unicorn @set[:pid] = path end + def directory(path) + @set[:directory] = path ? File.expand_path(path) : nil + end + private def set_hook(var, my_proc) #:nodoc: -- cgit v1.2.3-24-ge0c7