Ruby Fleebie

Rediscovering the joy of programming

Last deployment date with Rails and Capistrano

When you are developing an app or a website for your client, it is pretty common to setup a sandbox environment where your client can test and see the application while it is still in development. One thing I find useful is to display the last deployment date on the home page.

Here is a quick and easy way to automate the process :

Step #1 (we can have lots of fun)
I will consider that you use capistrano for deploying your app. The only thing you have to do in your capistrano recipe is to “touch” a dummy file to update its modification date.

namespace :deploy do
  desc 'Restart My App'
  task :restart, :roles => :app do
    run "touch #{current_path}/last_deploy"

Step #2 (there’s so much we can do)
Now in application_controller, add a before_filter like this

before_filter :last_deploy

def last_deploy
  @last_deploy ="last_deploy").atime rescue

Step #3 (it’s just you and me)
Display the date in the layout or in the view of your choice

<%= "Last deployment : #{@last_deploy}"%>

Edit : Oh, dear readers, I just want to let you know that RubyFleebie is now on Twitter.

5 Plugins Or Gems You Could Not Live Without

Hello everyone,

For the past few months this blog has been pretty much dead. I could give you tons of excuses as for why it has been so but that’s all they would be, mere excuses! And I assure you that you would not find any of them very convincing.

I received several comments recently telling me that you liked my blog and asking me when I plan writing again and believe me, it always goes straight to my heart. For some time now I am in the expectation that most of you will give up on me and remove this blog from your list forever but for some reason… you don’t!

Since I am a bit rusty at writing blog posts, I chose the easy way : a numbered list of Rails Gems/Plugins that I like. Hmmm, I know… it’s soooooo 2007! But, interesting nonetheless. If you don’t mind, I’d like that you share in the comment section the plugins that you like the most as well. Perhaps this post could become a reference to discover great plugins. Oh btw, from now on I will use the terms plugins and gems like they were the same thing.

  1. Paperclip by Thoughtbot

    Very easy to install & configure. I use it for thumbnails generation and it works wonder.

  2. locale_routing by Perfectline

    If you are developing a multilingual app/website (with I18n), don’t miss this simple plugin. It auto inserts the locale in the url without messing with your routes.

  3. thinking-sphinx by freelancing-gods

    A fast and reliable free-text search solution for your rails apps, using the Sphinx daemon. The only thing that is really unfortunate with sphinx is that is doesn’t index new database records live out of the box. Instead you have to build the index manually or setup a cronjob at a given interval. The workaround is to use delta indexes with the delayed-job plugin but it forces you to have a rake task always running on your server. I had so much problems with the rake task getting killed for no apparent reason that I decided that live indexing was not so important. Instead I have a cronjob running every X hours that rebuild the index from scratch

  4. will_paginate by mislav

    I guess we all know and use this one but I had to put it in my list anyway. It is such a great and easy to use pagination plugin.

  5. acts_as_state_machine by rubyist
  6. This one I use all the time. Everytime an activerecord object can be in more than a single “state” (enabled, hidden, locked, whatever)… think about using it!

That’s it! Now, it’s your turn : what is your own top 5 list?

Try Monit to manage your daemons

Hello there,

I’m sorry for not updating my blog more often. Now is the time to use the best excuse in the world that will make me look like a busy businessman and you will all be impressed. Here we go : I don’t have the time!

I know a lot of you are using GOD to keep your daemons / processes alive. I really don’t want to say bad things about GOD (I can’t because I never tried it)… but I DID read some really nasty things about it and somehow it convinced me to try an alternative : Monit.

The biggest advantage with Monit is that it is written in C instead of ruby… so it doesn’t leak and it appears to be faster. It is also pretty easy to configure. Here is a very basic monit configuration file:

set daemon 60

set httpd port 2812 address localhost
     allow localhost 

check process my_process
  with pidfile /home/apps/someapp/
  start program = "/home/apps/someapp/my_process start"
  stop program = "/home/apps/someapp/my_process stop"

Then, you start the monit daemon by typing : monit

It will poll the monit configuration file every minute (this is the set daemon 60 part) and will restart ‘my_process’ whenever it is not running. Of course this is an extremely simple use case and there are a lot more configuration options you can play with. Have a look at the documentation for more info.