]> git.openstreetmap.org Git - rails.git/commitdiff
Add a daemon to cleanup stale sessions.
authorTom Hughes <tom@compton.nu>
Thu, 14 Jun 2007 23:03:41 +0000 (23:03 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 14 Jun 2007 23:03:41 +0000 (23:03 +0000)
lib/daemons/session_cleanup.rb [new file with mode: 0644]
lib/daemons/session_cleanup_ctl [new file with mode: 0644]

diff --git a/lib/daemons/session_cleanup.rb b/lib/daemons/session_cleanup.rb
new file mode 100644 (file)
index 0000000..08e2dea
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+
+#You might want to change this
+#ENV["RAILS_ENV"] ||= "development"
+
+require File.dirname(__FILE__) + "/../../config/environment"
+
+require 'pstore'
+
+terminated = false
+
+session_path = ActionController::Base.session_options[:tmpdir]
+
+def expire_session(name)
+  ActiveRecord::Base.logger.info("Expiring session #{File.basename(name)}")
+  FileUtils.rm_f(name)
+end
+
+while (true) do
+  Dir.foreach(session_path) do |session_name|
+    if session_name =~ /^ruby_sess\./
+      session_name = session_path + "/" + session_name
+      session = PStore.new(session_name)
+
+      session.transaction do |session|
+        session_hash = session['hash']
+
+        if session_hash
+          session_stat = File::Stat.new(session_name)
+            puts session_hash[:token]
+
+          if session_hash[:token] and User.find_by_token(session_hash[:token])
+#            expire_session(session_name) if session_stat.mtime < 1.day.ago
+          else
+            expire_session(session_name) if session_stat.mtime < 1.hour.ago
+          end
+        else
+          expire_session(session_name)
+        end
+      end
+    end
+  end
+
+  sleep 15.minutes
+end
diff --git a/lib/daemons/session_cleanup_ctl b/lib/daemons/session_cleanup_ctl
new file mode 100644 (file)
index 0000000..f421370
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+require 'rubygems'
+require "daemons"
+require 'yaml'
+require 'erb'
+require 'active_support'
+
+options = YAML.load(
+  ERB.new(
+  IO.read(
+  File.dirname(__FILE__) + "/../../config/daemons.yml"
+  )).result).with_indifferent_access
+options[:dir_mode] = options[:dir_mode].to_sym
+
+Daemons.run File.dirname(__FILE__) + '/session_cleanup.rb', options