Skip to content

Instantly share code, notes, and snippets.

@jamiew
Created May 6, 2011 16:11
Show Gist options
  • Save jamiew/959256 to your computer and use it in GitHub Desktop.
Save jamiew/959256 to your computer and use it in GitHub Desktop.
Some handy Capistrano tasks for handling memcached. Assumes memcached runs locally on your app servers, YMMV
# This goes in config/deploy.rb
namespace :memcached do
desc "Flush memcached"
task :flush, :roles => [:app] do
run("cd #{current_release} && RAILS_ENV=#{rails_env} /usr/bin/rake memcached:flush")
end
desc "Flush memcached if there are any pending migrations (hook this before db:migrate)"
task :flush_if_pending_migrations, :roles => [:app] do
output = capture("cd #{current_release} && RAILS_ENV=#{rails_env} /usr/bin/rake db:pending_migration_count"
)
count = /(\d+) pending migrations/.match(output)
if count[0] && count[0].to_i > 0
puts "#{count[0].to_i} migrations will be run! Installing memcached:flush hook"
after "deploy:migrate", "memcached:flush"
end
end
end
# This goes in lib/tasks/memcached.rake
require 'socket'
namespace :memcached do
desc "Flush memcached (running on localhost port 11211)"
task :flush do
socket = TCPSocket.new('127.0.0.1', 11211)
socket.write("flush_all\r\n")
result = socket.recv()
if result == 'OK'
puts "memcached flush."
else
STDERR.puts "Error flushing memcached: #{result}"
end
socket.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment