unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* RAILS_ROOT and USR2 restarts
@ 2009-10-26 15:04 Ian Leitch
  2009-10-26 16:01 ` Eric Wong
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Leitch @ 2009-10-26 15:04 UTC (permalink / raw)
  To: mongrel-unicorn

Hi,

I've just started using Unicorn in production behind Nginx with a
Rails app, for the most part its been a smooth transition apart from
this one issue.
When I do an in place restart (USR2), the restart process works but
the workers load my old Rails release, not the new one. I'm guessing
that because the master process is currently in the old release
directory, when I signal USR2 it's just reloading inside the current
directory. I'm deploying with Capistrano, so app/current is a symlink
to a specific release. I've tried changing to the new release
directory in before_work but that made no difference. Is there a
callback that fires before the master preloads the app?

worker_processes 6
preload_app true
timeout 60
listen "/tmp/unicorn.production.sock"
pid "/tmp/unicorn.production.pid"

before_fork do |server, worker|
  Dir.chdir("/var/www/apps/systino_production/current")
  old_pid = "/tmp/unicorn.#{RAILS_ENV}.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

after_fork do |server, worker|
  ActiveRecord::Base.establish_connection
end


I also have a little launcher script (I'm using Solaris - SMF doesn't
let me provide a custom restart routine, so I have to bypass it and
use this script):


#!/usr/bin/ruby
$rails_env = ARGV[0]
$pid_file = "/tmp/unicorn.#{$rails_env}.pid"
def restart(pid)
  system("kill -USR2 #{pid}")
end
def start
  system("/opt/local/bin/unicorn_rails -c config/unicorn.rb -E
#{$rails_env} -D")
end
if File.exists?($pid_file)
  pid = File.read($pid_file).strip.to_i
  begin
    Process.kill(0, pid)
  rescue Errno::ESRCH
    # Not running
    start
  else
    restart(pid)
  end
else
  start
end


At the end of the deploy I execute:


task :restart_unicorn do
  run "cd #{current_path}; ruby config/unicorn_launcher.rb #{rails_env}"
end
_______________________________________________
mongrel-unicorn mailing list
mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: RAILS_ROOT and USR2 restarts
  2009-10-26 15:04 RAILS_ROOT and USR2 restarts Ian Leitch
@ 2009-10-26 16:01 ` Eric Wong
  2009-10-26 16:13   ` Ian Leitch
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Wong @ 2009-10-26 16:01 UTC (permalink / raw)
  To: unicorn list

Ian Leitch <port001@gmail.com> wrote:
> Hi,
> 
> I've just started using Unicorn in production behind Nginx with a
> Rails app, for the most part its been a smooth transition apart from
> this one issue.
> When I do an in place restart (USR2), the restart process works but
> the workers load my old Rails release, not the new one. I'm guessing
> that because the master process is currently in the old release
> directory, when I signal USR2 it's just reloading inside the current
> directory. I'm deploying with Capistrano, so app/current is a symlink
> to a specific release. I've tried changing to the new release
> directory in before_work but that made no difference. Is there a
> callback that fires before the master preloads the app?

Hi Ian,

Unicorn chdirs to the directory returned by `/bin/sh -c pwd` when it was
originally started.  The output of `pwd` *should* be symlink-aware, but
then Solaris /bin/sh is weird.  Can you confirm that it's broken?

You can put the following in your config (outside of the hooks):

  app_root = "/var/www/apps/systino_production/current"
  Dir.chdir(Unicorn::HttpServer::START_CTX[:cwd] = app_root)

I've been pondering adding a "working_directory" directive to
Configurator, too.  However the START_CTX hash is considered a stable
interface and I recently documented it in
http://unicorn.bogomips.org/Unicorn.html

START_CTX even allows you to switch between different installation paths
for Unicorn[1] and alter command line options that were originally
passed.  Lots of rope there :>

[1] for different Ruby installs/versions,
    or even swap in Rainbows! or vice versa

-- 
Eric Wong

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: RAILS_ROOT and USR2 restarts
  2009-10-26 16:01 ` Eric Wong
@ 2009-10-26 16:13   ` Ian Leitch
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Leitch @ 2009-10-26 16:13 UTC (permalink / raw)
  To: unicorn list

Apologies for the noise, I discovered START_CTX[:cwd] and that fixed it.

Solaris sh is indeed the culprit:

$ cd /var/www/apps/production/current
(admin@production:/var/www/apps/production/current)
$ sh -c pwd
/var/www/apps/production/releases/20091026160154

Thanks for creating Unicorn!


2009/10/26 Eric Wong <normalperson@yhbt.net>:
> Ian Leitch <port001@gmail.com> wrote:
>> Hi,
>>
>> I've just started using Unicorn in production behind Nginx with a
>> Rails app, for the most part its been a smooth transition apart from
>> this one issue.
>> When I do an in place restart (USR2), the restart process works but
>> the workers load my old Rails release, not the new one. I'm guessing
>> that because the master process is currently in the old release
>> directory, when I signal USR2 it's just reloading inside the current
>> directory. I'm deploying with Capistrano, so app/current is a symlink
>> to a specific release. I've tried changing to the new release
>> directory in before_work but that made no difference. Is there a
>> callback that fires before the master preloads the app?
>
> Hi Ian,
>
> Unicorn chdirs to the directory returned by `/bin/sh -c pwd` when it was
> originally started.  The output of `pwd` *should* be symlink-aware, but
> then Solaris /bin/sh is weird.  Can you confirm that it's broken?
>
> You can put the following in your config (outside of the hooks):
>
>  app_root = "/var/www/apps/systino_production/current"
>  Dir.chdir(Unicorn::HttpServer::START_CTX[:cwd] = app_root)
>
> I've been pondering adding a "working_directory" directive to
> Configurator, too.  However the START_CTX hash is considered a stable
> interface and I recently documented it in
> http://unicorn.bogomips.org/Unicorn.html
>
> START_CTX even allows you to switch between different installation paths
> for Unicorn[1] and alter command line options that were originally
> passed.  Lots of rope there :>
>
> [1] for different Ruby installs/versions,
>    or even swap in Rainbows! or vice versa
>
> --
> Eric Wong
> _______________________________________________
> mongrel-unicorn mailing list
> mongrel-unicorn@rubyforge.org
> http://rubyforge.org/mailman/listinfo/mongrel-unicorn
>
_______________________________________________
mongrel-unicorn mailing list
mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-10-26 16:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-26 15:04 RAILS_ROOT and USR2 restarts Ian Leitch
2009-10-26 16:01 ` Eric Wong
2009-10-26 16:13   ` Ian Leitch

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).