Question

I have two models Company and News How to make news downloaded every 30 minutes? I've tried through gem 'whenever', nothing happens

class Company < ActiveRecord::Base
  after_save :load_news 

  def load_news
    last_entry = self.news.last
    if last_entry.nil?
      feed = Feedzirra::Feed.fetch_and_parse(self.url)
    else
      feed = Feedzirra::Feed.fetch_and_parse(self.url,
        :if_modified_since => last_entry.published_at.to_time)
    end

    Company.add_entries(feed.entries, self.id)
  end

  def self.update_all_feeds(urls)
    Feedzirra::Feed.fetch_and_parse(urls,
      :on_success => lambda { |url, feed|
        rss = Comapny.select("companies.id").where(:url => url).first
        Company.add_entries(feed.entries, rss.id)
      }
    )
  end

end

class News < ActiveRecord::Base
  attr_accessible :content, :guid, :name, :published_at, :summary, :url, :company_id
end
Was it helpful?

Solution

whenever scpript

every 15.minutes do
  rake "collect"
end

and rake file

task :collect => :environment do
  urls = Company.select("companies.url").all.map { |v| v[:url] }
  Company.update_all_feeds(urls)
end

OTHER TIPS

If your whenever script is going to call Company.load_news, then that method needs to be a class method and not an instance method:

def self.load_news

or

def Company.load_news

Whatever your command, always try running it in the rails console the same way it appears in your whenever script, to debug it. Also check your rails log when the script fires, it should show any errors assuming that your whenever jobs successfully made it into cron.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top