Friday, June 22, 2007

Before going live in production with rails app...

Here are some steps that I found important before going live with my first rails application out there. Of course, there are others, but here we go:
  • Timeout old sessions
  • Send notifications on application errors
  • Roll log files
Timeout old sessions
I am using the activerecord store for my session data and hence, I need to delete some "timed out" session rows in the "sessions" table. This is easily done using this cronjob line:

00 02 * * * bash -c "cd $RAILS_APP_DIR && script/runner -e production \"CGI::Session::ActiveRecordStore::Session.delete_all(['updated_at < ?', 12.hours.ago])\""

If you are using the file based store (default) you can use a find with some -ctime parameters or something.

Send notifications on application errors
To be able to be notified by email when and application error occurs, I simply installed and configured the exception_notification plugin.

ruby script/plugin install exception_notification

Inserted in ApplicationController:

include ExceptionNotifiable

Edited config/environment.rb:

ExceptionNotifier.exception_recipients = %w(
ExceptionNotifier.sender_address = %("Application Error" )
ExceptionNotifier.email_prefix = "[MYAPP ERROR] "

Roll log files
It seems to be the advice not to let the log implementation in rails log the files, if one has multiple mongrel instances (they share the log files). So, I opt for a rails-external solution. Luckily, on linux we have something called logrotate.

In my linux setup /etc/logrotate.conf already contained the line "include /etc/logrotate.d", which makes adding more log rotation as easy as adding a file to the logrotate.d dir. I added a file shown below, which is fine for my setup:

/var/opt/rails/myapp/log/*.log {
  rotate 21

Shit, that was easy. Gotta love deploying on linux :-)


Anonymous said...

did you have to do anything about restarting the mongrel clusters some tutorials seem to mention forcing a restart?

Per Olesen said...

Nope. No restarts needed here.

Tech Per said...

Come to think if it. If you mean restarts due to problems with mongrel, then yes, I am having such problems.

I have just blogged about a mongrel surveillance and automatic restart on crash script I have done.