Skip to content

Instantly share code, notes, and snippets.

@schisamo
Created May 3, 2010 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schisamo/388303 to your computer and use it in GitHub Desktop.
Save schisamo/388303 to your computer and use it in GitHub Desktop.
# Hot deploy Rails apps (rolling restart) with Capistrano, Haproxy, and cluster of Passengers
# Capistrano config
namespace :passenger do
task :disable_load_balancing, :roles => :app do
run "rm -f #{current_path}/public/http-check.txt"
end
task :enable_load_balancing, :roles => :app do
run "touch #{current_path}/public/http-check.txt"
end
task :restart, :roles => :app do
sudo "/etc/init.d/apache2 restart"
end
end
namespace :deploy do
# If there is a more Capistrano-y way to do this?
# Using roles and somehow getting rid of the ENV[HOSTS] bit would be nice
task :hot do
["assets1", "assets2"].each do |h|
puts "Updating asset server #{h}"
ENV['HOSTS'] = h
update
end
["app1", "app2", "app3"].each do |h|
puts "Updating app server #{h}"
ENV['HOSTS'] = h
passenger.disable_load_balancing
sleep 10
update
passenger.restart
passenger.enable_load_balancing
sleep 10
end
end
end
# To run...
cap deploy:hot
# Set up Haproxy to remove servers from the cluster when the http-check file is not present
backend passengers
option httpchk GET /http-check.txt
http-check disable-on-404
server app1...
server app2...
server app3...
# You could also dress up the HTTP check with some extra sanity checks...
# haproxy
option httpchk GET /http-check
# rails
map.connect 'http-check', :controller => 'http_check'
class HttpCheckController < ApplicationController
def index
if File.exists?("#{RAILS_ROOT}/public/http-check.txt")
# check database connection and other stuff here..
render :text => "OK", :layout => false
else
render :text => "404 Not Found : http-check does not exist", :layout => false, :status => 404
end
end
end
# TODO: add Nagios config - alert if an http check fails for more than X seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment