How to restart god after deploying a Rails app with Capistrano
If you use the God gem to manage your Ruby on Rails application server (Unicorn, for example), you will have to restart the application server using God after deploying a new version.
I found a solution that polls the application’s tmp directory for restart.txt (like Phusion Passenger does) from within the god configuration and restarts the app if the file is touched. However, it didn’t work well for me because the ps syntax varies on different systems and the method is not very reliable (it triggered when I didn’t want it to, and vice versa).
So I thought about it and now I think that the best solution to restart an app via god is to do (surprise):
god restart YOUR-APP
And it should be done exactly if capistrano deploys, and not by polling.
To allow this command to be run by your deploying user, install sudo and make an entry into sudoers. It may look like this:
thedeployinguser ALL=(root) NOPASSWD: /usr/local/bin/god restart YourRailsServer
Then, all you need to do in config/deploy.rb is to make it look like this:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "ln -nfs #{shared_path}/sockets #{release_path}/tmp/sockets"
sudo "god restart YourRailsServer"
end
end
(I need the socket symlink for the Unicorn UNIX sockets.)