Session Sweeping

Hi,

Anyone got some slick ideas on how to sweep an AR session store? I have
a
few ideas how I can do it, just want to see if there is an elegant
solution
that someone is already using.

Bob S.

http://www.railtie.net/

More elegant than the rake task?

rake purge_sessions_table # Drop and recreate the session table
(much
faster than ‘DELETE * FROM sessions’)

-Steve
http://www.stevelongdo.com

Steve L. wrote:

More elegant than the rake task?

rake purge_sessions_table # Drop and recreate the session table
(much
faster than ‘DELETE * FROM sessions’)

-Steve
http://www.stevelongdo.com

That’s not a particularly nice way to do it for users that would prefer
to keep their session data for a little while.

Isn’t it better to do a nightly sweep of sessions that are, say, 3 weeks
old?

I keep login information (cookie) for up to 2 weeks, so I’ll probably
just delete sessions after 16 days. But I’ll do it nightly to avoid a
huge build-up. I have an SQL script that runs nightly to perform
backups. I will probably just put the delete command in there unless
there is some compelling reason to have Ruby do it.

Jake

Here’s my trick.
Actually make a session model in your app and leave it almost completly
empty.

Add a simple method to the session model called sweep or whatever.

def self.sweep
self.destroy_all(:coditions => [“updated_at < ?”, 30.minutes.ago]
end

Now, in order to run this (either through cron, windows scheduler,
whatever)
your command, from the rails app directory:
ruby script/runner “Session.sweep”

And there ya go, that should do it for ya. Of course you can change the
run
time or session length to whatever you want. It has worked great for me.

Cron is the way to go…I have a cron job that runs hourly on each of my
rails sites.

It does different things based on the site, but one of those things is
deleting sessions older than a few hours.

Maybe look into RailsCron as well. I just launch mine from good ol’
cron.

The great things about RailsCron is that if you ever need to move
your application, your
cron job will move with you as well…


– Tom M.