unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* Problem restarting Unicorn
@ 2012-04-05  7:21 Andrew Stewart
  2012-04-05 11:34 ` Michael Guterl
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Stewart @ 2012-04-05  7:21 UTC (permalink / raw)
  To: mongrel-unicorn

Hello,

I seem to be confused about how to restart Unicorn when deploying updates to a Rails app via Capistrano.

I just had a problem where a bundled gem wasn't being seen by the Unicorn processes, even though app code I introduced at the same time was being served by the Unicorn processes.  I had restarted Unicorn numerous times using `bundle exec cap deploy:restart` (see the code below), which I thought was the right way to do zero-downtime restarts, but my problem was only solved by doing a hard restart of Unicorn with `bundle exec cap deploy:hard_restart`.

With each "normal" restart I tailed the stderr log to check everything was ok; everything appeared fine, with gems refreshed etc.

Please could somebody explain what I'm doing wrong with my normal restarts?

config/deploy.rb:

    set :unicorn_config, "#{current_path}/config/unicorn.rb"
    set :unicorn_pid,    "#{shared_path}/pids/unicorn.pid"

    namespace :deploy do
      task :start, :roles => :app, :except => { :no_release => true } do
        run "cd #{current_path} && #{sudo_workaround} bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
      end
      task :stop, :roles => :app, :except => { :no_release => true } do
        run "#{sudo_workaround} kill -s QUIT `cat #{unicorn_pid}`"
      end
      task :hard_stop, :roles => :app, :except => { :no_release => true } do
        run "#{sudo_workaround} kill `cat #{unicorn_pid}`"
      end
      task :restart, :roles => :app, :except => { :no_release => true } do
        run "#{sudo_workaround} kill -s USR2 `cat #{unicorn_pid}`"
      end
      task :hard_restart, :roles => :app, :except => { :no_release => true } do
        hard_stop
        start
      end
    end
    
    # Works around Ubuntu changing the PATH when you use sudo.
    # sudo's path can find common commands like echo but not much else.
    def sudo_workaround
      "sudo env PATH=$PATH"
    end


config/unicorn.rb:

    APP_PATH = '/var/www/apps/sparkle'
    worker_processes 4
    user 'rails', 'rails'
    working_directory "#{APP_PATH}/current"
    listen "/tmp/unicorn_sparkle.sock", :backlog => 64
    timeout 30
    pid "#{APP_PATH}/shared/pids/unicorn.pid"
    stderr_path "#{APP_PATH}/shared/log/unicorn.stderr.log"
    stdout_path "#{APP_PATH}/shared/log/unicorn.stdout.log"
    preload_app true
    GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
    
    before_fork do |server, worker|
      defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
    
      old_pid = "#{server.config[:pid]}.oldbin"
      if old_pid != server.pid
        begin
          sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
          Process.kill(sig, File.read(old_pid).to_i)
        rescue Errno::ENOENT, Errno::ESRCH
        end
      end
    end
    
    after_fork do |server, worker|
      defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
    end

This is all with Unicorn 4.2.0 and Rails 3.0.11.

Many thanks in advance,

Andy Stewart
----
http://airbladesoftware.com

_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

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

* Re: Problem restarting Unicorn
  2012-04-05  7:21 Problem restarting Unicorn Andrew Stewart
@ 2012-04-05 11:34 ` Michael Guterl
  2012-04-10  8:52   ` Andrew Stewart
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Guterl @ 2012-04-05 11:34 UTC (permalink / raw)
  To: unicorn list

On Thu, Apr 5, 2012 at 3:21 AM, Andrew Stewart
<boss@airbladesoftware.com> wrote:
> Hello,
>
> I seem to be confused about how to restart Unicorn when deploying updates to a Rails app via Capistrano.
>
> I just had a problem where a bundled gem wasn't being seen by the Unicorn processes, even though app code I introduced at the same time was being served by the Unicorn processes.  I had restarted Unicorn numerous times using `bundle exec cap deploy:restart` (see the code below), which I thought was the right way to do zero-downtime restarts, but my problem was only solved by doing a hard restart of Unicorn with `bundle exec cap deploy:hard_restart`.
>
> With each "normal" restart I tailed the stderr log to check everything was ok; everything appeared fine, with gems refreshed etc.
>
> Please could somebody explain what I'm doing wrong with my normal restarts?
>
> config/deploy.rb:
>
>    set :unicorn_config, "#{current_path}/config/unicorn.rb"
>    set :unicorn_pid,    "#{shared_path}/pids/unicorn.pid"
>
>    namespace :deploy do
>      task :start, :roles => :app, :except => { :no_release => true } do
>        run "cd #{current_path} && #{sudo_workaround} bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
>      end
>      task :stop, :roles => :app, :except => { :no_release => true } do
>        run "#{sudo_workaround} kill -s QUIT `cat #{unicorn_pid}`"
>      end
>      task :hard_stop, :roles => :app, :except => { :no_release => true } do
>        run "#{sudo_workaround} kill `cat #{unicorn_pid}`"
>      end
>      task :restart, :roles => :app, :except => { :no_release => true } do
>        run "#{sudo_workaround} kill -s USR2 `cat #{unicorn_pid}`"
>      end
>      task :hard_restart, :roles => :app, :except => { :no_release => true } do
>        hard_stop
>        start
>      end
>    end
>
>    # Works around Ubuntu changing the PATH when you use sudo.
>    # sudo's path can find common commands like echo but not much else.
>    def sudo_workaround
>      "sudo env PATH=$PATH"
>    end
>
>
> config/unicorn.rb:
>
>    APP_PATH = '/var/www/apps/sparkle'
>    worker_processes 4
>    user 'rails', 'rails'
>    working_directory "#{APP_PATH}/current"
>    listen "/tmp/unicorn_sparkle.sock", :backlog => 64
>    timeout 30
>    pid "#{APP_PATH}/shared/pids/unicorn.pid"
>    stderr_path "#{APP_PATH}/shared/log/unicorn.stderr.log"
>    stdout_path "#{APP_PATH}/shared/log/unicorn.stdout.log"
>    preload_app true
>    GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
>
>    before_fork do |server, worker|
>      defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
>
>      old_pid = "#{server.config[:pid]}.oldbin"
>      if old_pid != server.pid
>        begin
>          sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
>          Process.kill(sig, File.read(old_pid).to_i)
>        rescue Errno::ENOENT, Errno::ESRCH
>        end
>      end
>    end
>
>    after_fork do |server, worker|
>      defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
>    end
>
> This is all with Unicorn 4.2.0 and Rails 3.0.11.
>
> Many thanks in advance,
>

You probably want to check out the Sandbox documentation here:
http://unicorn.bogomips.org/Sandbox.html

The section that I think is most relevant is related to setting the START_CTX:

Unicorn::HttpServer::START_CTX[0] = "/some/path/to/bin/unicorn"

Hope that helps.

Best,
Michael Guterl
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

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

* Re: Problem restarting Unicorn
  2012-04-05 11:34 ` Michael Guterl
@ 2012-04-10  8:52   ` Andrew Stewart
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Stewart @ 2012-04-10  8:52 UTC (permalink / raw)
  To: unicorn list


On 5 Apr 2012, at 13:34, Michael Guterl wrote:
> On Thu, Apr 5, 2012 at 3:21 AM, Andrew Stewart <boss@airbladesoftware.com> wrote:
>> I just had a problem where a bundled gem wasn't being seen by the Unicorn processes, even though app code I introduced at the same time was being served by the Unicorn processes.  I had restarted Unicorn numerous times using `bundle exec cap deploy:restart` (see the code below), which I thought was the right way to do zero-downtime restarts, but my problem was only solved by doing a hard restart of Unicorn with `bundle exec cap deploy:hard_restart`.
> 
> You probably want to check out the Sandbox documentation here:
> http://unicorn.bogomips.org/Sandbox.html

Thanks for the pointer to the Sandbox documentation, the one page on the Unicorn website I hadn't read.  The section on BUNDLE_GEMFILE for Capistrano users was what I needed.

Yours,
Andy Stewart
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

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

end of thread, other threads:[~2012-04-10  8:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-05  7:21 Problem restarting Unicorn Andrew Stewart
2012-04-05 11:34 ` Michael Guterl
2012-04-10  8:52   ` Andrew Stewart

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).