How to automate tasks using Ruby or Rails?

I’ve yet to get the cron job to consistently execute for the flickr and delicious caching. I’ve tried several different things, but nothing seems to work. So I’ve started seeking alternatives. There are three different methods listed, but it’s not obvious to me how they might work. If I were to setup a scheduler, would I then “start” it when Rails initializes in environment.rb? Of all three options, I’m at a loss as to which one I should pursue.

Right now I’m calling refresh_api_cache via a “refresh” link that appears upon login:

1
2
3
4
5
6
7
8
9
10
def refresh_api_cache
  reload_cache(ENV['RAILS_ENV']) and redirect_to :back
end

private

def reload_cache(env)
  command = "ruby #{RAILS_ROOT}/script/custom/api_cache.rb"
  env =~ /development|test/ ? system command : exec command
end

That’s not terribly inconvenient, but it requires two things to happen (tag something in delicious or flickr and then come here and click “refresh”). I want this to be automated. Any tips on scheduling in Ruby/Rails?

Comments

01

Ilya Grigorik on Fri Apr 27 at 06:56PM

Ryan, to answer your question.. If you use BackgrounDRB, it will load your worker when Rails is first initialized. To take it a step further, you can also provide a ‘schedule’, which the worker can follow (ex. refresh cache every x minutes). Take a look at the BackgrounDRB RDoc, there are a lot of interesting features in there which I never discussed in my post.

Have something to say?
Please rewrite the image text Are You Human? Hint: Are You Human? Formatting Tips

or

© 2009 Ryan Heath | Site Management A Ruby on Rails production.

Get in Touch