Вопрос

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
Это было полезно?

Решение

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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top