Replace the old session cleanup daemon with a new script to be run from
authorTom Hughes <tom@compton.nu>
Sat, 25 Aug 2007 09:53:07 +0000 (09:53 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 25 Aug 2007 09:53:07 +0000 (09:53 +0000)
cron periodically.

lib/daemons/session_cleanup.rb [deleted file]
lib/daemons/session_cleanup_ctl [deleted file]
script/cleanup [new file with mode: 0755]

diff --git a/lib/daemons/session_cleanup.rb b/lib/daemons/session_cleanup.rb
deleted file mode 100755 (executable)
index 08e2dea..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-#!/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
deleted file mode 100755 (executable)
index f421370..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/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
diff --git a/script/cleanup b/script/cleanup
new file mode 100755 (executable)
index 0000000..2708770
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../config/environment'
+
+Session.find(:all, :conditions => ["updated_at < ?", 1.week.ago]).each do |session|
+  begin
+    if session[:user] and User.find(session[:user])
+      session.destroy if session.updated_at < 1.month.ago
+    else
+      session.destroy
+    end
+  rescue Exception => ex
+    puts "Invalid session #{session.session_id}: #{ex.to_s}"
+    session.destroy
+  end
+end
+
+UserToken.delete_all("expiry < NOW()")