software, business, and fun
cambodia

Category — Development

how to cleanup stale activerecord session objects

rails 1.2 app was using a db backed session store and was kind of “neglected”:


cupid_production=> select count(*) from sessions;
count
--------
380754
(1 row)

http://www.realityforge.org/articles/2006/03/01/removing-stale-rails-sessions
http://robsanheim.com/2007/04/
http://groups.google.com/group/melbourne-ruby/browse_thread/thread/c5eecde3fb6a0b08

destroy_all is un-needed since we don’t have callbacks on the session table. use delete_all instead.

save into lib/tasks as session_cleanup.rake


desc "Clear database-stored sessions older than a day old"
task :session_cleanup => :environment do
CGI::Session::ActiveRecordStore::Session.delete_all ["updated_at < ?", 1.day.ago ]
end

setup to run daily via rake/cron


30 1 * * * cd /var/www/appname/current && rake session_cleanup RAILS_ENV=production

August 10, 2007   No Comments

mongrel issues with logrotate/ruby

i’m digging mongrel but it does not work with ruby Logger default rotation. requires logrotate to do the work.

added /etc/logrotate.d/PROJECT so logrotate will run it via cron.
rotates out apaches logs weekly, and all mongrel/rails logs weekly.


compress
missingok
weekly
rotate 52

"/var/www/PROJECT/current/log/access.log" "/var/www/PROJECT/current/log/error.log" {
olddir old
#delaycompress
notifempty
create 644 root root
sharedscripts
postrotate
if [ -f /var/run/apache2.pid ]; then
/etc/init.d/apache2 restart > /dev/null
fi
endscript
}

“/var/www/PROJECT/current/log/production.log” “/var/www/PROJECT/current/log/mongrel.log” “/var/www/PROJECT/current/log/statistics/*.log” {
olddir old
sharedscripts
postrotate
mongrel_rails cluster::restart -C /var/www/PROJECT/current/config/mongrel_cluster.yml
endscript
}

less than ideal if apache ran lots of other sites, could not restart apache so greedily. would need to run as a seperate logrotate task that manages all access.log/error.log across each application.

works for now with apache running just for this rails app.

http://www.redhillconsulting.com.au/blogs/simon/archives/000370.html

http://www.bjhess.com/bjhessblog/2007/08/01/rolling-your-rails-logs-with-logrotate

http://mongrel.rubyforge.org/faq.html

August 10, 2007   No Comments