user1104783
user1104783

Reputation: 155

Rails polling RSS every 6 hours

I am seeking advice on how to poll RSS with a given interval such as 6 hours.

The following code works, it reads and parses the feed and adds it to the database. It only adds new feeds. Here is the class method:

def self.update_from_feed(feed_url)
  feed = Feedzirra::Feed.fetch_and_parse("http://feeds.feedburner.com/PaulDixExplainsNothing") #Example feed
  feed.entries.each do |entry|
    unless exists? :guid => entry.id
      create!(
        :name         => entry.title,
        :url          => entry.url,
        :published_at => entry.published,
        :guid         => entry.id
      )
    end
  end
end

How do I run this entire class method lets say, every 6 hours? I'm new to Ruby (and Rails), so any help would be appreciated with an example. I want to avoid running an external cron job if possible. I want it to run every 6 hours within the code if that makes sense. Thanks

Upvotes: 0

Views: 484

Answers (1)

Srdjan Pejic
Srdjan Pejic

Reputation: 8202

No, it doesn't make sense to not use cron for this. This is what cron is for, it's in the name of the program.

If you don't like the cron syntax, that's cool, there's a gem called whenever, https://github.com/javan/whenever that gives a nice Ruby syntax for generating a cron job and a command that lets you do that.

However, for the love of god, do not try to invent a new way of doing this, unless you're adding some killer features. Use cron, move on.

Upvotes: 4

Related Questions