Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

Pruning Old Sessions

2006-03-21   [ 0 comments ]

So here it is the update to tell you how to remove old sessions from your database. How did those sessions get into your database in the first place? For the answer to that question check out my previous post entitled "Initialized! good, Authorized? Great!".

Now that we all know that Ruby on Rails does not clear old sessions for us (surprising since it does just about everything else you would ever want it to do), we have to take care of the session overload. I have went over a week without clearing my sessions, and on this small site those records started to pile up. So how did I take care of it?

I decided that staying away from some seperate script and cron job would ultimately prove the best solution. By keeping all the session management inside my RoR application I prepare it for any move to any other system. Besides, clearing the sessions is quite easy. Below is the SQL statement necessary to remove old sessions:

DELETE FROM sessions WHERE now() - updated_at > 3600;

Simple enough. The number 3600 is of course the time in seconds (in this case 2hrs). So when should we run this statement? - every 30 minutes, hour, 2 hours? I would suggest for the sake of simplicity, and for strict adherence to your session policy:

Sessions should be cleared each time the controller is called, before any session is checked for or created.

Makes sense, we certainly don't want sessions being accepted after our set timelimit for the sake of good policy. So what must we do to make this happen?

Ruby on Rails makes it easy to call a function before any other action is taken. So I decided to take my SQL call and put into a function called "kill_sessions" (sounds serious!). Then at the top my controller that is worried about sessions I placed the instruction:

before_filter :kill_sessions

The only problem I ran into was trying to get my simple SQL statement to just execute. I tried the basic "execute()" command I found in the Rails API. However I couldn't get it to work. Luckily I stumbled across this requirement while googling the problem:

To use the execute method you must write it like so:
ActiveRecord::Base.connection.execute()

A bit annoying if you ask me. Perhaps I'll find an easier way of doing it later, but hey, thats what learning a new language / framework is all about. Learn as you go. =)

:author => "Charles Abbott"
Converting to Ruby on Rails
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"