« Plugging a Team City Security Hole with a Little Obfuscation | Main | How to Entice People to Vote »

Rails Housekeeping

Listen to this articleListen to this article

Since moving from lighttpd+FasCGI to Apache 2.2+mongrel our production rails application has been rock solid—one unexplained ruby core-dump notwithstanding.

To keep everything humming along, we run a few cron jobs which I thought I'd share.

The first is to ensure the application starts on boot. There is an rc-script to do this but I never bothered to get it running on FreeBSD. Instead, we use the @reboot keyword built into vixie-cron:

cd ~/www/production/current; mongrel_rails cluster::stop; mongrel_rails cluster::start

Next, session expiration. Even though plenty have argued against them in favour of memcached, we've found file-system sessions to be just fine for our, relatively low traffic, application. To keep timeout sessions after one hour—with a margin of error of an extra hour—we run an hourly cron job to delete session files that haven't been updated since it last ran:

cd ~/www/production/current; find tmp/sessions -name 'ruby_sess.*' -amin +60 -exec rm -rf {} \;

Next, to keep log file sizes manageable, we run a cron job once a day to rotate the log files using logrotate, followed by a re-cycle of the mongrel cluster:

cd ~/www/production/current; logrotate -s log/logrotate.status config/logrotate.conf; mongrel_rails cluster::restart

And here's config/logrotate.conf:

"log/*.log" {
  compress
  daily
  delaycompress
  missingok
  notifempty
  rotate 7
}

And finally, just because, we run another daily cron job to vacuum the PostgreSQL database:

cd ~/www/production/current; psql cjp_production -c 'vacuum full'

Comments

logrotate also has a postrotate option. I use it to run /etc/init.d/mongrel_cluster restart (included in the mongrel_cluster gem).

Cool! I'll have to try it out.

Hi Simon,

Why do you need to restart the mongrel cluster after rotating the log file? Does logrotate not leave behind empty log files for mongrel to keep adding to? Or does mongrel keep a file handle open to the old ones?

I found mongrel wouldn't write to the new file; presumably because it had a file handle to the old one?

Craig and Simon --

You don't have to restart your Mongrel cluster if you use copytruncate in your config. copytruncate copies (rather than moves) your log files and clears them out (rather than create fresh ones). I prefer using copytruncate and restarting the cluster only on deploy or when something goes wrong.

See http://wiki.rubyonrails.org/rails/pages/DeploymentTips for more information

Thanks for the post!

Post a comment