Forum: Ruby on Rails Session Sweeping

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bob S. (Guest)
on 2006-02-28 19:05
(Received via mailing list)
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/
Steve L. (Guest)
on 2006-02-28 20:01
(Received via mailing list)
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
Jake J. (Guest)
on 2006-02-28 20:07
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
Nick S. (Guest)
on 2006-02-28 21:23
(Received via mailing list)
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.
subimage interactive (Guest)
on 2006-02-28 21: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'
cron.
Tom M. (Guest)
on 2006-02-28 22: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 M.
This topic is locked and can not be replied to.