Originally uploaded by Lucee.

In my previous post, I’ve re-written my old Java JMX client program in JRuby with Jetty.

In other area of my old Java program, I chose the data-push model, calling a set of JMX commands periodically and publishing data onto JMS, because I did not want them called too much frequently by accident. It has worked well, but there are a few things I wanted to change like, 1) It can’t run another instance for redundancy, 2) it’s not simple to get data because of JMS.

So, this time I’ve chosen the data-pull model with embedded HTTP servlet container jetty, implementing JSON webservice.

I also chose data caching without database backend in the JBoss webapp that subscribes to the JMX data on JMS. I admit I made a wrong decision on that. The data became stale from time to time and the JBoss server needed to restart to resolve the problem.

This time, as you would expect, I’ve chosen MySQL since I am thinking to rewrite the webapp in Rails.

The point of this post is how I gather data for a Rails application.

What I need is to gather data every minute. There are a few ways to run background jobs in Rails, but there is a better and simpler way to do that, which is running Rake tasks using Active Records from Cron. There is a great tutorial on that from Rails Envy.

Each task takes 15-20 secs to complete. Anything could go wrong and more than one task would run at once, which would make the problem worse. How can I prevent this? Lockrun comes handy here. Lockrun serves as a protective wrapper and it insures that another instance of the same command is not already running. My co-worker Phil has been using this for his applications for sometime now.

Related Posts: