From 22bcfb1240fb3b03613944d8517c100650ab9ad2 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sat, 25 Aug 2007 09:53:07 +0000 Subject: [PATCH] Replace the old session cleanup daemon with a new script to be run from cron periodically. --- lib/daemons/session_cleanup.rb | 45 --------------------------------- lib/daemons/session_cleanup_ctl | 15 ----------- script/cleanup | 18 +++++++++++++ 3 files changed, 18 insertions(+), 60 deletions(-) delete mode 100755 lib/daemons/session_cleanup.rb delete mode 100755 lib/daemons/session_cleanup_ctl create mode 100755 script/cleanup diff --git a/lib/daemons/session_cleanup.rb b/lib/daemons/session_cleanup.rb deleted file mode 100755 index 08e2deae5..000000000 --- a/lib/daemons/session_cleanup.rb +++ /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 index f421370fd..000000000 --- a/lib/daemons/session_cleanup_ctl +++ /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 index 000000000..270877096 --- /dev/null +++ b/script/cleanup @@ -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()") -- 2.39.5