Skip to content

Instantly share code, notes, and snippets.

@kares
Forked from yaroslav/sessions.rake
Created April 14, 2011 07:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kares/919069 to your computer and use it in GitHub Desktop.
Save kares/919069 to your computer and use it in GitHub Desktop.
rake task for cleaning up ActiveRecord session store table from expired sessions
namespace :db do
namespace :sessions do
desc "Clean up expired Active Record sessions (updated before ENV['UPDATED_AT'], defaults to 1 month ago)."
task :expire => :environment do
time = Time.parse( ENV['UPDATED_AT'] || 1.month.ago.to_s(:db) )
say "cleaning up expired sessions (older than #{time}) ..."
session = ActiveRecord::SessionStore::Session
rows = session.delete_all ["updated_at < ?", time]
say "deleted #{rows} session row(s) - there are #{session.count} session row(s) left"
end
end
def say(msg)
puts msg
end
end
@adamwaite
Copy link

Thanks for this. What's that say thing about though! Ha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment