From d1ff8c5bdb286ae212962ec9034d6a345cf09b30 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 19 Mar 2009 13:05:54 -0700 Subject: start libifying common launcher code The daemonization logic between unicorn and unicorn_rails scripts can definitely be shared. Again: our daemonization logic is slightly non-standard since our executables are designed to run in APP_ROOT/RAILS_ROOT and not "/" like "normal" UNIX daemons. --- Manifest | 1 + bin/unicorn | 27 ++------------------------- bin/unicorn_rails | 27 ++------------------------- lib/unicorn/launcher.rb | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 50 deletions(-) create mode 100644 lib/unicorn/launcher.rb diff --git a/Manifest b/Manifest index 91b2129..729d33b 100644 --- a/Manifest +++ b/Manifest @@ -24,6 +24,7 @@ lib/unicorn/configurator.rb lib/unicorn/const.rb lib/unicorn/http_request.rb lib/unicorn/http_response.rb +lib/unicorn/launcher.rb lib/unicorn/socket.rb lib/unicorn/util.rb setup.rb diff --git a/bin/unicorn b/bin/unicorn index ebf57c3..9deb872 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -1,6 +1,5 @@ #!/home/ew/bin/ruby -$stdin.sync = $stdout.sync = $stderr.sync = true -require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX +require 'unicorn/launcher' require 'optparse' env = "development" @@ -163,27 +162,5 @@ if $DEBUG }) end -# only daemonize if we're not inheriting file descriptors from our parent -if daemonize - - $stdin.reopen("/dev/null") - unless ENV['UNICORN_FD'] - exit if fork - Process.setsid - exit if fork - end - - # We don't do a lot of standard daemonization stuff: - # * $stderr/$stderr can/will be redirected separately - # * umask is whatever was set by the parent process at startup - # and can be set in config.ru and config_file, so making it - # 0000 and potentially exposing sensitive log data can be bad - # policy. - # * Don't bother to chdir here since Unicorn is designed to - # run inside APP_ROOT. Unicorn will also re-chdir() to - # the directory it was started in when being re-executed - # to pickup code changes if the original deployment directory - # is a symlink or otherwise got replaced. -end - +Unicorn::Launcher.daemonize! if daemonize Unicorn.run(app, options) diff --git a/bin/unicorn_rails b/bin/unicorn_rails index 0deace1..4b0a34e 100755 --- a/bin/unicorn_rails +++ b/bin/unicorn_rails @@ -1,6 +1,5 @@ #!/home/ew/bin/ruby -$stdin.sync = $stdout.sync = $stderr.sync = true -require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX +require 'unicorn/launcher' require 'optparse' require 'fileutils' @@ -208,31 +207,9 @@ if $DEBUG }) end -# only daemonize if we're not inheriting file descriptors from our parent -if daemonize - options[:pid] = rails_pid - $stdin.reopen("/dev/null") - unless ENV['UNICORN_FD'] - exit if fork - Process.setsid - exit if fork - end - - # We don't do a lot of standard daemonization stuff: - # * $stderr/$stderr can/will be redirected separately - # * umask is whatever was set by the parent process at startup - # and can be set in config.ru and config_file, so making it - # 0000 and potentially exposing sensitive log data can be bad - # policy. - # * Don't bother to chdir here since Unicorn is designed to - # run inside APP_ROOT. Unicorn will also re-chdir() to - # the directory it was started in when being re-executed - # to pickup code changes if the original deployment directory - # is a symlink or otherwise got replaced. -end - # ensure Rails standard tmp paths exist %w(cache pids sessions sockets).each do |dir| FileUtils.mkdir_p("tmp/#{dir}") end +Unicorn::Launcher.daemonize! if daemonize Unicorn.run(app, options) diff --git a/lib/unicorn/launcher.rb b/lib/unicorn/launcher.rb new file mode 100644 index 0000000..8c96059 --- /dev/null +++ b/lib/unicorn/launcher.rb @@ -0,0 +1,33 @@ +$stdin.sync = $stdout.sync = $stderr.sync = true +require 'unicorn' + +class Unicorn::Launcher + + # We don't do a lot of standard daemonization stuff: + # * umask is whatever was set by the parent process at startup + # and can be set in config.ru and config_file, so making it + # 0000 and potentially exposing sensitive log data can be bad + # policy. + # * don't bother to chdir("/") here since unicorn is designed to + # run inside APP_ROOT. Unicorn will also re-chdir() to + # the directory it was started in when being re-executed + # to pickup code changes if the original deployment directory + # is a symlink or otherwise got replaced. + def self.daemonize! + $stdin.reopen("/dev/null") + + # We only start a new process group if we're not being reexecuted + # and inheriting file descriptors from our parent + unless ENV['UNICORN_FD'] + exit if fork + Process.setsid + exit if fork + + # $stderr/$stderr can/will be redirected separately in the Unicorn config + $stdout.reopen("/dev/null", "a") + $stderr.reopen("/dev/null", "a") + end + $stdin.sync = $stdout.sync = $stderr.sync = true + end + +end -- cgit v1.2.3-24-ge0c7