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

View All of Your Database Tables in Rails

Posted: May 18th, 2009 | Author: admin | Filed under: Rails | Tags: , , , | No Comments »

When working on a Ruby on Rails project, you often find yourself needing to see what the names of the columns in your tables are.  This can be done by simply looking at your migration files, but after several migrations in, this can be a confusing headache.  This is my very simple solution to seeing all of the tables listed on one page in my project.

1. Create a New View

I called mine models.html.erb.  Paste in the folling code:

<% @models.each do |model| %>
  <h3 style="margin:0;padding:0"><%=h model %></h3>
  <table width="400">
  <% model.constantize.columns.each do |col| %>
    <tr>
      <td><b><%=h col.name %></b></td><td><%=h col.sql_type %></td><td><%=h col.default.to_s %></td>
    </tr>
  <% end %>
  </table>
  <br />
<% end %>

2. Add Your Models in the Controller

Go to the controller that is part of your view and add the corresponding method.  In the method you just need to add a line telling it the names of all of your models. For example:

@models = ['User','Article','Subject']

3. Take a Look!

The resulting table will have 3 columns: the name of the column in the database, the data type, and the default value (if one exists).
viewexample


Smoother Compiz-Fusion Graphics

Posted: March 16th, 2009 | Author: admin | Filed under: Compiz, Ubuntu | No Comments »

How does it work?

Compiz has the same priority as every other application on your computer.  This means that if your computer is being bogged down by other applications, your Compiz effects will start to slow down.  Windows users may quickly be annoyed by this, because in Windows, the entire GUI is kernal code, giving it a much higher priority.  This  quick guide allows you to quickly and easily increase the priorty of Compiz.

What You Need

sudo apt-get install compizconfig-settings-manager

If you don’t already have CompizConfig Settings Manager, you are missing out on all of the fun of having Compiz.

The Magic

1. Open up your favorite text editor and copy and paste these 2 lines:

#!/bin/bash
pidof compiz.real | xargs renice -5

Save the file as nice_compiz wherever you would like.  I saved my in ~/Applications/nice_compiz

2. Make the file executable.  This can be done most simply by navigating to it in Nautilus, right click on the file and click Properties.  Under the Permissions tab check “Allow executing file as program“.Make Executable

3. Make a keyboard shortcut to nice_compiz in CompizConfig Settings Manager:

  1. Go to System -> Preferences -> CompizConfig Settings Manager.  Click the General Options button (the first one in the list), and go to the Commands tab.Select General Options
  2. Under commands, add the command
    gksudo /path/to/app/nice_compiz

    Where /path/to/app/ is the location of your text file you created earlier. Insert Command

  3. Under the Key Bindings drop down, add a keyboard shortcut for your new command.   Do so by clicking the button that says Disabled to the right of your command number.  Check enabled, then Grab Key Combination.  I chose the arbitrary shortcut Windows + =.

Your Done!

Press your new keyboard shortcut and you should see a password prompt.  After correctly entering your password, you will see smoother graphics until you restart.

To double check if it worked, open System Monitor, under Processes look at the Nice value of compiz.real.  You can try raising and lowering this value for a higher or lower priority.  The minimum value is -20, the lower the value, the better your graphics performance (but lower performance for the rest of your machine).

Know Your Linux? I originally wanted to run this script on startup, but could not find a place to put it where it would be ran as root and compiz.real would already be loaded.  If anyone figures this out, please leave a comment on how you did it.


Synchronize Tomboy Notes with Anything

Posted: November 9th, 2008 | Author: admin | Filed under: Ubuntu | Tags: , , , , , | 6 Comments »

Synchronizing with Tomboy Notes using WebDev or SSH can be confusing and tricky. Thanks to Ubuntu 8.10’s (Intrepid Ibex) fancy GVFS feature, you can now easily synchronize with just about anything: SFTP (SSH), FTP, Windows Network, WebDav. This is perfect if you already have your own personal web server.

Here are the steps:

  1. Go to Places -> Connect to Server… and connect to the server of your choice. If you will be using this to back up other things, you may find it easiest to include the folder path. Be sure to make a bookmark.
  2. Go to your GVFS folder:
    Press Alt+F2 and enter the command “nautilus ~/.gvfs
  3. Browse to the location of your backup folder.
  4. Create a new folder called “tomboy”.
  5. Right click on your Tomboy icon, and click Preferences. Go to the Synchronization tab.
  6. Select “Local Folder” as your Service. Then, copy and paste the path from nautilus from into the Folder Path. Make sure that “/tomboy” is at the end of this path.
  7. Click “Save” and you are ready to start synchronizing!

The one downside of this is that you will have to be connected to Server any time you want to synchronize. To reconnect to your server, you can always click on the bookmark under Places -> Bookmarks.