Forum: Ruby on Rails Session Sweeping

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-28 18:05
(Received via mailing list)

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

Bob Silva
5ce55767684e0cd1727db2b8b8975640?d=identicon&s=25 Steve Longdo (Guest)
on 2006-02-28 19:01
(Received via mailing list)
More elegant than the rake task?

rake purge_sessions_table     # Drop and recreate the session table
faster than 'DELETE * FROM sessions')

Ff43001ac5fe9805aa6ca2e89d3b7b5d?d=identicon&s=25 Jake Janovetz (janovetz)
on 2006-02-28 19:07
Steve Longdo wrote:
> More elegant than the rake task?
> rake purge_sessions_table     # Drop and recreate the session table
> (much
> faster than 'DELETE * FROM sessions')
> -Steve

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

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.

Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-28 20:23
(Received via mailing list)
Here's my trick.
Actually make a session model in your app and leave it almost completly

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

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

Now, in order to run this (either through cron, windows scheduler,
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
time or session length to whatever you want. It has worked great for me.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 subimage interactive (Guest)
on 2006-02-28 20:54
(Received via mailing list)
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'
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-02-28 21:09
(Received via mailing list)
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 Mornini
This topic is locked and can not be replied to.