0

我正在使用 mongodb 开发 ROR 应用程序。基本上我的模型课之一是这样的:

require 'open-uri'   
class StockPrice
 include Mongoid::Document
 include Mongoid::Timestamps

 field :stock_name, type: String
 field :price, type: Float

 index :stock_name => +1, :updated_at => -1

 def self.price(stock_name)
   #print stock_name
   g=StockPrice.new
   g.crawl(stock_name)
   where(stock_name: stock_name).desc(:updated_at).first.price
end

#Search for the stock in screener.in and get its last price.
def crawl(stock_name)
  company_name=stock_name
  agent = Mechanize.new
  page = agent.get('http://www.screener.in/')
  form = agent.page.forms[0]
  agent.page.forms[0]["q"]=company_name
  button = agent.page.forms[0].button_with(:value => "Search Company")
  pages=agent.submit(form, button)  
  new_page=pages.uri.to_s
  doc=Nokogiri::HTML(open(new_page))
  row_data = doc.css('.table.draggable.table-striped.table-hover tr.strong td').map     do       |tdata|
      tdata.text
  end
  g=row_data[2]
 StockPrice.create(stock_name:stock_name,price:g)
end
handle_asynchronously :crawl
end

现在在我的应用程序中,我想使用延迟的作业运行爬网功能,以便它可以在后台运行为此我正在使用延迟的作业_mongoid gem。但是当我使用 handle_asynchronously 时出现此错误:在记录被持久化之前无法为记录创建作业

4

1 回答 1

0

您自己说明了记录必须保留在数据库中的原因,以便延迟作业工作

解决方案

而是做这样的事情

def self.price(stock_name)
   #print stock_name
   crawl stock_name
   where(stock_name: stock_name).desc(:updated_at).first.price
end

def self.crawl(stock_name)
  s = StockPrice.new(name: stock_name)
  company_name=stock_name
  agent = Mechanize.new
  page = agent.get('http://www.screener.in/')
  form = agent.page.forms[0]
  agent.page.forms[0]["q"]=company_name
  button = agent.page.forms[0].button_with(:value => "Search Company")
  pages=agent.submit(form, button)  
  new_page=pages.uri.to_s
  doc=Nokogiri::HTML(open(new_page))
  row_data = doc.css('.table.draggable.table-striped.table-hover tr.strong td').map     do       |tdata|
      tdata.text
  end
  g=row_data[2]
  s.price = g 
  s.save
end

这更有意义,因为crawl在技术上(在现实世界中)应该是class方法而不是实例方法

于 2013-08-22T06:44:48.590 回答