DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Method For A Staggered Restart Of A Rails Site Running Fcgid And Capistrano

11.13.2008
| 4081 views |
  • submit to reddit
        killing the dispatch processes too fast was triggering too many rails boots and making one of our sites thrash.  This bit of bash can be used to kill them softly. (with it's song...)

def dispatch_killer apache_user, seconds_between_kills=1
  run <<-CMD 
    pids=`ps -U #{apache_user} -o pid,command | grep #{current_path} | awk '{print $1;}'`;\
    for pid in $pids ;do \
      #{ sudo :as => remote_user } -u #{apache_user} kill $pid; \
      sleep #{seconds_between_kills}; \
    done
  CMD
end

a recipie could use this method like this:
namespace :deploy do
  task :restart, :roles => :app do
      dispatch_killer 'www', 10 # as www issue kills 10 seconds apart
  end
end

we came up with 10 seconds by timing the boots of script/console. no more thrashing!
(props to James at http://www.titled.com for the idea)