Skip to content

Instantly share code, notes, and snippets.

@yaroslav
Created November 2, 2008 19:16
Show Gist options
  • Save yaroslav/21733 to your computer and use it in GitHub Desktop.
Save yaroslav/21733 to your computer and use it in GitHub Desktop.
Rake task for Ruby on Rails for cleaning up ActiveRecord session store table from expired sessions. A bit lame (talk about logger.info not working in production) but still.
namespace :db do
namespace :sessions do
desc "Clean up expired Active Record sessions (updated before ENV['EXPIRE_AT'], defaults to 1 month ago)."
task :expire => :environment do
Rails.logger.info "Cleaning up expired sessions..."
puts "Cleaning up expired sessions..."
time = ENV['EXPIRE_AT'] || 1.month.ago.to_s(:db)
rows = CGI::Session::ActiveRecordStore::Session.delete_all ["updated_at < ?", time]
Rails.logger.info "Expired sessions cleanup: #{rows} session row(s) deleted."
puts "Expired sessions cleanup: #{rows} session row(s) deleted."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment