Skip to content

Instantly share code, notes, and snippets.

@mikewadhera
Created August 18, 2009 21:02
Show Gist options
  • Save mikewadhera/169966 to your computer and use it in GitHub Desktop.
Save mikewadhera/169966 to your computer and use it in GitHub Desktop.
deploy:rolling_restart
namespace :deploy do
desc "Restarts each app role one at a time, removing from load balancing during restart"
serial_task self, :rolling_restart, :roles => :app do
lb = fetch(:load_balancer)
lb_removal = lambda { `elb-deregister-instances-from-lb #{lb} --instances #{@instance_id}` }
lb_addition = lambda { `elb-register-instances-with-lb #{lb} --instances #{@instance_id}` }
watch = "rails"
port = 8080
heartbeat = "/heartbeat"
n_heartbeat = 3
# Capture EC2 InstanceID by querying meta-data web service on remote instance
run "curl -s http://169.254.169.254/2007-01-19/meta-data/instance-id" do |_,_,out|
@instance_id = out
end
logger.info "Removing #{@instance_id} from #{lb} load balancer..."
lb_removal.call
logger.info "Removed #{@instance_id} from #{lb} load balancer."
logger.info "Restarting process..."
sudo %(god restart #{watch}; sleep 2) # sleep a bit to let process fully reap (restart is async)
logger.info "Waiting for port #{port} to come back up..."
run %(while true; do echo 'waiting for port...' ; if netstat -tan | grep -q :::#{port}; then echo '#{port} up' && exit 0; fi; sleep 5; done)
logger.info "Sending #{n_heartbeat} heartbeat checks to #{heartbeat}..."
run %(for i in {1..#{n_heartbeat}}; do echo -n Attempting $i of #{n_heartbeat} requests to #{heartbeat}... && curl -s http://localhost:#{port}#{heartbeat} && echo; done)
logger.info "Heartbeat checks successful, restart complete."
logger.info "Adding #{@instance_id} to #{lb} load balancer..."
lb_addition.call
logger.info "Added #{@instance_id} to #{lb} load balancer."
end
desc "Custom restart behavior"
task :restart, :roles => :app, :except => { :no_restart => true } do
rolling_restart
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment