Keep your crashing Rails app always running.

Posted: October 6th, 2010 | Author: admin | Filed under: Uncategorized | No Comments »

I realize that this title seems to discredit my programming abilities.  However, sometimes you have an issue that is not easily explained and takes several outages to determine the root cause of.  This was happening to a low traffic site of mine.  Because it was not getting tons of traffic, the site would some times be down for days before we knew about it.  To help with this problem, I put this simple script in my Rails root directory:

#!/bin/sh

if [ `ps aux | grep ruby | grep -v grep | wc -l` -eq 0 ]; then
        cd ~/rails_apps/oliver
        echo "`date` : app is down, starting it up" >> log/rails_persist.log
        rm log/*.pid
        /usr/bin/ruby /usr/bin/mongrel_rails start -p 12002 -d -e production -P log/mongrel.pid
fi

Save this as a file called rails_persist.sh in your root Rails app directory.  The conditional checks to see if there is any Ruby process running on the server.  If you have multiple Rails apps on your system, you may want to add another | grep to this conditional.

Note that this command to start Ruby was taken directly from the method that I found cPanel was using.  You can change out this last line with whatever works for you.  Every time the app is down when this script is running, it starts it back up and logs the time in the file log/rails_persist.log.

Finally the last part is to add the following to your crontab:

* * * * *    sh /home/my_name/rails_apps/oliver/rails_persist.sh

This will tell the cron service to run the script every minute.  If you are using cPanel, you can add this in the cPanel console under Advanced > Cron Jobs.


Verify file type in Ruby

Posted: February 27th, 2010 | Author: admin | Filed under: Uncategorized | Tags: , , , , , , | No Comments »

I was unable to find anything quite like this in Google, so I had to make one.  When validating a file type, you don’t want to resort to telling your user a file is the wrong format when they think is.  If for some reason a user’s file doesn’t contain an extension, you should check the mime type as a last resort.  Linux and Mac users may often not use file extensions.  Note that if you are doing this for security reasons, you should check both the extension and mime-type instead of one or the other.

Here was my solution for checking to see if a file was a document for a Recruiting web site.  Note that you will need MIME::Types which is included by default in Ruby on Rails.

def is_document?(file)
  extensions = %w( pages pdf doc docx rtf txt odt )
  # used http://filext.com/ for mime types
  mimetypes = %w( application/pdf application/x-pdf application/acrobat applications/vnd.pdf text/pdf text/x-pdf
                  application/doc appl/text application/vnd.msword application/vnd.ms-word application/winword application/word application/x-msw6 application/x-msword
                  application/vnd.openxmlformats-officedocument.wordprocessingml.document
                  application/rtf application/x-rtf text/rtf text/richtext application/msword application/x-soffice
                  text/plain application/txt browser/internal text/anytext widetext/plain widetext/paragraph
                  application/vnd.oasis.opendocument.text application/x-vnd.oasis.opendocument.text
                  application/x-iwork-pages-sffpages )
  if file.include?('.')
    return extensions.include? file[ file.rindex('.')+1, file.length ]
  else
    return mimetypes.include? MIME::Types.of( file ).to_s
  end
end