]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/session_cleanup.rb
Add a daemon to cleanup stale sessions.
[rails.git] / lib / daemons / session_cleanup.rb
1 #!/usr/bin/env ruby
2
3 #You might want to change this
4 #ENV["RAILS_ENV"] ||= "development"
5
6 require File.dirname(__FILE__) + "/../../config/environment"
7
8 require 'pstore'
9
10 terminated = false
11
12 session_path = ActionController::Base.session_options[:tmpdir]
13
14 def expire_session(name)
15   ActiveRecord::Base.logger.info("Expiring session #{File.basename(name)}")
16   FileUtils.rm_f(name)
17 end
18
19 while (true) do
20   Dir.foreach(session_path) do |session_name|
21     if session_name =~ /^ruby_sess\./
22       session_name = session_path + "/" + session_name
23       session = PStore.new(session_name)
24
25       session.transaction do |session|
26         session_hash = session['hash']
27
28         if session_hash
29           session_stat = File::Stat.new(session_name)
30             puts session_hash[:token]
31
32           if session_hash[:token] and User.find_by_token(session_hash[:token])
33 #            expire_session(session_name) if session_stat.mtime < 1.day.ago
34           else
35             expire_session(session_name) if session_stat.mtime < 1.hour.ago
36           end
37         else
38           expire_session(session_name)
39         end
40       end
41     end
42   end
43
44   sleep 15.minutes
45 end