#!/usr/bin/ruby # Clears out old sessions of applications on Rails Machine. # This script is intended to be called by cron on your Rails Machine. # # Andrew Stewart, AirBlade Software Ltd (boss at airbladesoftware dot com) # Feedback gratefully received. # # Version 0.1 require 'rubygems' require 'logger' require 'yaml' # Configure the script here. You won't need to change anything # unless you have modified your Rails Machine setup. APPS_PATH = '/var/www/apps' # Path to all your apps EXCLUSIONS = %w( rails ) # Any directories or apps to exclude HISTORY = 7 # How far back we keep sessions (days) LOG_FILE = '/home/deploy/sessions.log' # Where to log activity # The body of the script follows. log = Logger.new(LOG_FILE, 5, 10*1024) log.level = Logger::DEBUG log.info 'Starting session maintenance' Dir["#{APPS_PATH}/*"].each do |entry| app = File.basename entry next if File.file?(entry) || EXCLUSIONS.include?(app) config = YAML.load_file(File.join(entry, 'current', 'config', 'database.yml'))['production'] database, username, password = config['database'], config['username'], config['password'] db_session_cmd = "mysql -u #{username} -p#{password} -D #{database} -e 'DELETE FROM sessions WHERE datediff(now(), updated_at) > #{HISTORY}' " log.debug "Executing: #{db_session_cmd}" `#{db_session_cmd}` log.info "Cleared out #{HISTORY} days of sessions from #{app}" end log.info 'Finished session maintenance'