mindsifter

sifting through the thoughts in my mind
Copyright Michael D'Auria © 2008

Categories

Bookmarks

Clearing of Session Data January 26th, 2007

So, you have all these sessions and need to clear them out but are clueless how to do it? It’s actually really simple.

You can do it this way:

  class SessionCleaner
    def self.remove_stale_sessions
      CGI::Session::ActiveRecordStore::Session.
        destroy_all( ['updated_on < ?', 20.minutes.ago] ) 
    end
  end

Then just call via cron:

  */10 * * * * ruby /full/path/to/script/runner 
     -e production "SessionCleaner.remove_stale_sessions" 

Above was found via: http://www.realityforge.org/articles/2006/03/01/removing-stale-rails-sessions

Or you can do it all one 1 line:


  */5 * * * *  ruby /full/path/to/script/runner -e 
    production 'CGI::Session::ActiveRecordStore::Session.destroy_all
    ( ["updated_at < ?",20.minutes.ago ] )' 2>&1

Both will clear out sessions that are 20 minutes old every 10 minutes. Both will work, but they have to load up Rails every time it runs. I came up with the following solution to avoid loading up Rails thus alleviating some server load:

  require 'rubygems'
  require 'mysql'

  db = Mysql.new("localhost", "db_user", "db_user_pw", "db")
  query = db.prepare("DELETE FROM `sessions` WHERE 
    updated_at < NOW() - INTERVAL 20 MINUTE")
  query.execute
  puts "Removed #{query.affected_rows} Sessions\n" 
  db.close

Please note that I assume that you have a MySQL database ,have the mysql gem installed and that you are storing your sessions in a table named ‘sessions’.


Enjoy!


Stay tuned for a more flexible version that will read in your config/database.yml for your db settings so you don’t have to put them in the script itself.

Sorry, comments are closed for this article.