SessionCleanup is a routine to clear out stale sessions from within a
A Rails application creates a new session for every connection. Over
time, this leads to the accumulation of hundreds or thousands of stale
session files or session records (if you’re using a database). These
sessions are often cleaned out manually or by the use of a cron job.
Neither of these approaches is ideal. Each of them requires an agency
external to the application. Neither of them handles the problem of data
– work files, images, etc. – that are associated with the session but
external to it.
SessionCleanup solves both of these problems. It is called from within a
Rails application, anywhere an extra second or two won’t matter – as
part of a login method, for example. If needed, it will automatically
call a block within the application with the session hash of each
session it deletes. This block can clean up any additional state
associated with the session.
SessionCleanup has no way of knowing whether a session is in use or not.
It deletes sessions based solely on their age. If your application is
one in which users normally keep the same session for many hours or
days, SessionCleanup is not for you!
SessionCleanup presently only handles sessions stored in the file system
(CGI::Session::PStore) or in the database
(CGI::Session::ActiveRecordStore). I would welcome contributed code for
the other possible cases!
You can get SessionCleanup here:
I wrote SessionCleanup to help with a case where I have work files
associated with sessions, and because I didn’t want to have to use a
cron job to maintain my Rails apps. Maybe it’ll be useful to someone
Please email me with any problems, suggestions, or improvements.
Routines to handle other forms of session storage would be particularly