Syndication IconNew article alerts are available via Atom. Hide this message

Offline tasks the easy way

There's been quite a lot of chat recently about various job scheduling systems and process managers for offlining expensive tasks on the LRUG list. BackgrounDRb, Beanstalk, Starling, BackgroundJob and other similar solutions have been discussed. These systems can be useful, but most of the time they're just adding unnecessary complexity.

One instance where I feel these solutions are unnecessary is where you need to strip data from an external service in a way that's totally disconnected from the HTTP request-response cycle.

Say you want to pull the most recent article from this blog every 15 minutes and create a file that could then be served statically to your visitors. A naive implementation of that functionality would look something like this:

require 'net/http'
require 'hpricot'

barking_iguana = URI.parse('http://barkingiguana.com/')
loop do
  articles = Hpricot(Net::HTTP.get(barking_iguana))
  title = (articles / "div.article a[@rel=bookmark] text()").first
  link = (articles / "div.article a[@rel=bookmark]").first['href']

  # Of course, this should have a real file path in it.
  File.open("/.../.../.../barking_iguana.ssi", "w+") do |f|
    f.write("#{title}: #{link}")
    f.flush
  end

  sleep 900 # 15 minutes
end

Doesn't that look nice? No screwing around with complex tools to handle the scheduling - just run it and it'll go forever.

"Ah," I hear you say, "but what if it crashes?" Well, in the unlikely event that such a simple script does crash I'd have something like God monitoring the processes so it would be restarted. You've got something monitoring your processes anyway (right?) so it should be pretty simple to add another process to that list.

Love me!

If you've found this article useful I'd appreciate recommendations at Working With Rails.

Related articles

Leave feedback...

Commenting is closed for this article.

About the boy

A picture of Craig in grayscale

Hi, I'm Craig and I'm a Ruby coder. I live, work and play in London. I like scaling applications and eating yoghurt. Sometimes I climb rocks. Most of the time I climb back down.

You can contact me by email, MSN or Jabber. My address on all of these is craig@xeriom.net.

Code Licence

You can use any of the code on this blog in any way you want. It's totally public domain. You don't even need to attribute it to me, although it would be nice if you did. Just don't sue me.

Friends and colleagues

Other Reading

I Work With Rails

Recommend Me

My Travels

I go places. Do you go places too? Let's meet up!.