Restart Unicorn issue (capistrano)

2019-01-21 20:52发布

问题:

I've got following settings in deploy.rb to restart my server:

namespace :deploy do
  task :restart do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2     \`cat #{unicorn_pid}\`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -    E #{rails_env} -D; fi"
  end
end

but it doesn't work. I mean that command executes (it asks me the password and gives no errors), but all changes in config files are still ignored (i.e. number of worker processes or database settings).

回答1:

Maybe this is because of the way unicorn restarts. Not every worker is restarted immediately. This is to make it possible to have zero downtime and loose no requests. If you want to see your changes for sure, try to stop and then start your application instead. I have had to do this some times. Of course you will potentially loose some request.

The following tasks is what I use for restarting, stopping, and starting my unicorn server.

desc "Zero-downtime restart of Unicorn"
task :restart, :except => { :no_release => true } do
  run "kill -s USR2 `cat #{shared_path}/pids/unicorn.pid`"
end

desc "Start unicorn"
task :start, :except => { :no_release => true } do
  run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D -E production"
end

desc "Stop unicorn"
task :stop, :except => { :no_release => true } do
  run "kill -s QUIT `cat #{shared_path}/pids/unicorn.pid`"
end

Hope this helps you.

Maybe this article is of interest.



回答2:

see here my baby~ Restarting Unicorn with USR2 doesn't seem to reload production.rb settings

Keep in mind that: your working directory in unicorn.rb should be : /your/cap/directory/current

NOT be: File.expand_path("../..", FILE)

Because the unicorn and linux soft link forking error: soft link can not work well.



回答3:

You should give capistrano-unicorn a try, that's what I currently use with the default hooks mentioned below.

Setup

Add the library to your Gemfile:

ruby group :development do gem 'capistrano-unicorn', :require => false end

And load it into your deployment script config/deploy.rb:

ruby require 'capistrano-unicorn'

Add unicorn restart task hook:

ruby after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded after 'deploy:restart', 'unicorn:restart' # app preloaded after 'deploy:restart', 'unicorn:duplicate' # before_fork hook implemented (zero downtime deployments)