我在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
对于其他网站,我的rake任务几乎相同,并且没有将定价数据保存两次。我正在使用Amazon RDS db,如果这会影响任何事情。
答案 0 :(得分:1)
原来这是git跟踪的一个问题,我在本地删除了文件,但忘记了
git rm oldraketask.rake
碰巧包含一个rake任务,其名称与我正在运行的任务相同,因此heroku连续运行两个rake任务,首先是新文件,然后是来自heroku服务器的旧文件中的一个。