Pidfiles and cwd?

Eric Wong normalperson at yhbt.net
Sat Sep 5 00:28:06 EDT 2009


Chris Wanstrath <chris at ozmm.org> wrote:
> Yikes. Let me try that again.
> 
> Hi,
> 
> Thanks for unicorn!

Hi Chris, no problem!

> Two questions:
> 
> A) Is there a reason `unicorn` allows you to specify the pidfile's
> location but `unicorn_rails` does not?

`unicorn` was designed to mimic `rackup` in terms of command line
options and `unicorn_rails` was designed to mimic `script/server` in
Rails.

I really didn't know what I was doing with the command-line options for
this, so I decided to steal from others :)

For long-running servers, I'm not a fan of command-line options in
general because they're easy to forget, so `unicorn` only supports it
because `rackup` does it (so the embedded CLI options in config.ru can
be shared).

For `unicorn_rails`, --daemonize already sets a default PID path in
RAILS_ROOT/tmp/pids/unicorn.pid whereas `script/server` chooses
RAILS_ROOT/tmp/pids/server.pid.  Since Rails values "convention over
configuration",  I figured I might as well hard code it...

Additionally, the "-P" parameter used by unicorn_rails and script/server
is used to set RAILS_RELATIVE_URL_ROOT so it conflicts with the short
option used by rackup/unicorn.

> B) Is there a reason `unicorn_rails` must start in the app root and
> doesn't allow it as a config option?

Since the config file is just Ruby, you can just Dir.chdir inside it.
And since the chdir is done when the config file is evaluated, the
chdir can be done across restarts/reloads (you can point it to a
symlinked directory) to pick up new code/releases.

If you do that, I would initially start Unicorn in "/" or some other
directory that won't get deleted so you'll be safe for upgrades.

If you managed to forget that, you can set the following in your
Unicorn config:

  Unicorn::HttpServer::START_CTX[:cwd] = "/"

And then HUP the process before doing the USR2+QUIT to reexec.
Subsequent Unicorns will always start in "/" and then you can
Dir.chdir to wherever you run your app.

Hopefully that makes sense, one thing I've been trying to avoid with the
configuration is having too many ways to do the same thing.

-- 
Eric Wong


More information about the mongrel-unicorn mailing list