0

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
Anton
  • 87
  • 10
  • With a class named News, I hope you have something in your initializer to avoid having a singular news item referred to as 'new'. That's just begging to conflict with ruby's 'new' method. I would rename this to NewsItem or some such. Other than that, what exactly does your whenever config look like? Does the job appear among your cron jobs? For fetching in the background every 30 minutes, that's not a bad way to do it. – sockmonk May 17 '13 at 13:49
  • I dont have experience with whatever. But i'll use delayed_job or sidekick for time sheduled things. – Mindbreaker May 17 '13 at 15:12
  • 1
    @sockmonk `every 1.hours do runner "Company.load_news" end` – Anton May 18 '13 at 19:46

2 Answers2

1

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
Anton
  • 87
  • 10
0

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.

sockmonk
  • 4,195
  • 24
  • 40