lib/include/class_knjappserver_cleaner.rb in knjappserver-0.0.15 vs lib/include/class_knjappserver_cleaner.rb in knjappserver-0.0.16

- old
+ new

@@ -11,11 +11,12 @@ loop do sleep time if @config.has_key?(:restart_when_used_memory) and !@should_restart mbs_used = (Knj::Php.memory_get_usage / 1024) / 1024 - #STDOUT.print "Used: #{mbs_used}\n" + STDOUT.print "Restart when over #{@config[:restart_when_used_memory]}mb\n" if @config[:debug] + STDOUT.print "Used: #{mbs_used}mb\n" if @config[:debug] if mbs_used.to_i >= @config[:restart_when_used_memory].to_i STDOUT.print "Memory is over #{@config[:restart_when_used_memory]} - restarting.\n" @should_restart = true end @@ -23,11 +24,11 @@ if @should_restart and !@should_restart_done and !@should_restart_runnning begin @should_restart_runnning = true - #When we begin to restart it should go as fast as possible - so start by flushing out any emails waiting... + #When we begin to restart it should go as fast as possible - so start by flushing out any emails waiting so it goes faster the last time... STDOUT.print "Flushing mails.\n" self.mail_flush #Lets try to find a time where no thread is working within the next 30 seconds. If we cant - we interrupt after 10 seconds and restart the server. begin @@ -87,22 +88,28 @@ self.timeout(:time => 300) do STDOUT.print "Cleaning sessions on appserver.\n" if @config[:debug] self.paused_exec do + session_not_ids = [] time_check = Time.now.to_i - 300 - @sessions.each do |ip, ip_sessions| - ip_sessions.each do |session_hash, session_data| - if session_data[:time_lastused].to_i <= time_check - session_data[:dbobj].flush - @ob.unset(session_data[:dbobj]) - session_data[:hash].clear - ip_sessions.delete(session_hash) - session_data.clear - end - end + newsessions = {} + @sessions.each do |session_hash, session_data| + session_data[:dbobj].flush - @sessions.delete(ip) if ip_sessions.empty? + if session_data[:time_lastused].to_i > time_check + newsessions[session_hash] = session_data + session_not_ids << session_data[:dbobj].id + end + end + + @sessions = newsessions + + STDOUT.print "Delete sessions...\n" if @config[:debug] + @ob.list(:Session, {"id_not" => session_not_ids, "date_lastused_below" => (Time.now - 5356800)}) do |session| + @ob.delete(session) + @sessions.delete(session[:idhash]) + STDOUT.print "Deleted session: #{session.id}\n" if @config[:debug] end end end end end \ No newline at end of file