diff options
author | Eric Wong <normalperson@yhbt.net> | 2009-12-26 17:04:57 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2009-12-26 17:15:11 -0800 |
commit | 5eea32764571b721cd1a89cf9ebfa853c621ac9e (patch) | |
tree | 7aae0814fe23d06945b92ee9158b3f02bbd37b4b /lib/unicorn | |
parent | 643994afcdbd4121c0d131a87052cdd17d40e54f (diff) | |
download | unicorn-5eea32764571b721cd1a89cf9ebfa853c621ac9e.tar.gz |
This behavior change also means our grandparent (launched from a controlling terminal or script) will wait until the master process is ready before returning. Thanks to IƱaki Baz Castillo for the initial implementations and inspiration.
Diffstat (limited to 'lib/unicorn')
-rw-r--r-- | lib/unicorn/launcher.rb | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/lib/unicorn/launcher.rb b/lib/unicorn/launcher.rb index 1229b84..2d6ad97 100644 --- a/lib/unicorn/launcher.rb +++ b/lib/unicorn/launcher.rb @@ -19,21 +19,42 @@ class Unicorn::Launcher # 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! + def self.daemonize!(options = {}) $stdin.reopen("/dev/null") + $stdin.sync = true # may not do anything... # 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 + # grandparent - reads pipe, exits when master is ready + # \_ parent - exits immediately ASAP + # \_ unicorn master - writes to pipe when ready - # $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" + rd, wr = IO.pipe + grandparent = $$ + if fork + wr.close # grandparent does not write + else + rd.close # unicorn master does not read + Process.setsid + exit if fork # parent dies now + end + + if grandparent == $$ + # this will block until HttpServer#join runs (or it dies) + master_pid = rd.sysread(16).to_i + exit!(1) unless master_pid > 1 + exit 0 + else # unicorn master process + options[:ready_pipe] = wr + # $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" + + # returns so Unicorn.run can happen + end end - $stdin.sync = $stdout.sync = $stderr.sync = true end end |