Cleaning up session files.

Hi, it seems like session files never get erased? I've made a small
monkeypath for environment.rb to clean up old session every time a new
session is created. Do you know of any nicer place to put this code?
Without having to add cron-jobs or other os-dependent solutions?

Does it exists any trigger or similar that get executed every N'th
request to the rails application that instead can be used to cleanup
the session directory?

Have a nice day,
Tobias

class PStore
  def initialize_with_cleanup(session)
    unless File::exist?(session)
      expire_time = Time.now - 3600 # 1 hour
      Dir["#{ActionController::Base.session_options[:tmpdir]}/
#{ActionController::Base.session_options[:prefix]}*"].each do |sess|
        (File.delete(sess) if File.ctime(sess) < expire_time) rescue
nil
      end
    end
    initialize_without_cleanup(session)
  end
  alias_method_chain :initialize, :cleanup
end

Hi, it seems like session files never get erased? I've made a small
monkeypath for environment.rb to clean up old session every time a new
session is created. Do you know of any nicer place to put this code?
Without having to add cron-jobs or other os-dependent solutions?

Isn't this going to hurt performance by scouring the sessions folder
and looking at the modification time of every file there, for each
and every request ?

Does it exists any trigger or similar that get executed every N'th
request to the rails application that instead can be used to cleanup
the session directory?

not that I know of.

cookie based sessions are nice in that you don't need to worry about
this sort of stuff.

Fred

You can create a very simple cron job using find and looking at the
creation or modification time of the session files to remove them.

Tobias Nurmiranta wrote:

> Hi, it seems like session files never get erased? I've made a small
> monkeypath for environment.rb to clean up old session every time a new
> session is created. Do you know of any nicer place to put this code?
> Without having to add cron-jobs or other os-dependent solutions?

Isn't this going to hurt performance by scouring the sessions folder
and looking at the modification time of every file there, for each
and every request ?

Its not every request, only when a new session is created. Which can
be quite often as well.

> Does it exists any trigger or similar that get executed every N'th
> request to the rails application that instead can be used to cleanup
> the session directory?

not that I know of.

cookie based sessions are nice in that you don't need to worry about
this sort of stuff.

But this is only available in rails edge?

Hi Tobias

I think there is a Rake Task to clear the tmp directory

rake tmp:sessions:clear

I saw it in Aptana RadRails RakeTasks Tab

HTH

Hi

I think there is a Rake Task to clear the tmp directory

rake tmp:sessions:clear

It will simply remove all session files. If you need to delete expired
files only you can check this task:
http://www.taknado.com/2007/7/25/rake-task-to-clear-expired-session-files

Tobias Nurmiranta wrote:

Hi, it seems like session files never get erased? I've made a small
monkeypath for environment.rb to clean up old session every time a new
session is created. Do you know of any nicer place to put this code?
Without having to add cron-jobs or other os-dependent solutions?

Does it exists any trigger or similar that get executed every N'th
request to the rails application that instead can be used to cleanup
the session directory?

Beware, the file based session store was never meant for production. Use ActiveRecordStore or one of the other alternatives.