Running something periodically.

Jonas Pfenniger zimbatm at oree.ch
Thu Apr 26 09:11:48 EDT 2007


2007/4/25, Jonathan Stott <jonathan.stott at gmail.com>:
> Suppose, for a moment, that I have a Camping app. It looks at
> something, like an RSS feed of upcoming events, and displays them if
> they meet a specified criterion.  I look at the index page, it fetches
> the feed items and then displays them. Yay.  Maybe it also writes them
> to a database.
>
> Which is great, so long as I keep looking at the page every so often.
> But what do I do while I'm sleeping? or on holiday? Who will look at
> the page to fetch the feeds then?
>
> So, is there some way I can make Camping perform the fetch operation
> regularly, all internal to Camping? I know I could set up a cron job
> to grab the page via curl every hour or whatever, but can I do it just
> in Camping?

You can make use of the create method to initialize your thread. Just
make sure that you store the thread's presence outside of the app's
module to avoid problems when reloading..

module YourApp
  def self.create
    ::YOURAPP_FETCH_THREAD ||= Thread.new { ... }
  end
end

PS : I wouldn't use an external scheduler if the database is sqlite..
I had lots of problems with locking.

-- 
Cheers,
  zimbatm


More information about the Camping-list mailing list