0
我在heroku上有一個應用程序,它將定價數據從網站上刪除並定期保存。由於某種原因,每當我在本地運行rake任務時,它會按預期運行,但是當我在heroku上運行該任務時,它會增加額外的輸出並由於某種原因而保存該對象兩次。當我在本地運行Rake任務在Heroku上運行兩次
a,12.80 saved at 2012-10-26 03:36:17 UTC.
b,38.03 saved at 2012-10-26 03:36:24 UTC.
c,22.38 saved at 2012-10-26 03:36:31 UTC.
輸出,當我在Heroku上運行
輸出
a,12.80 saved at 2012-10-26 03:36:17 UTC.
b,38.03 saved at 2012-10-26 03:36:24 UTC.
c,22.38 saved at 2012-10-26 03:36:31 UTC.
#<Stock:0x000000047bb5e8>,12.80 saved at 2012-10-26 03:36:45 UTC.
#<Stock:0x000000047baaf8>,38.03 saved at 2012-10-26 03:36:48 UTC.
#<Stock:0x000000047b00a8>,22.38 saved at 2012-10-26 03:36:52 UTC.
代碼
require 'open-uri'
require 'date'
namespace :data do
desc "import current blah stock price data to database"
task :importblah => :environment do
#pass in a stock and price and save it to the database
def save_stock(stock, price)
#store stock data in database
p = stock.prices.build
p.price = price
p.datetime = Time.now.utc.to_datetime
if p.save
puts "#{stock.symbol},#{price} saved at #{p.datetime.to_s}."
else
puts "#{stock.symbol} didn't save."
end
end
actives = Parent.where("test1 = ?", true)
actives.each do |m|
stocks = m.stocks.where('test2 = ?', false)
stocks.each do |stock|
if stock.title.start_with?('blah')
#grab stock price data from blah.com
url = "blah"+stock.symbol
doc = Nokogiri::HTML(open(url))
price = doc.at_css(".value").text[/\d+\.\d+/]
save_stock(stock, price)
end
end
end
end
end
我有幾乎相同的耙任務不同的網站,它沒有兩次保存定價數據。如果影響到任何內容,我正在使用Amazon RDS db。