我正在使用 Roo gem 导入 Excel 文件、CSV 文件等。
该代码在我的本地机器上运行良好,无论是否有延迟作业,b)在 Heroku 上没有延迟作业。不幸的是,在 Heroku 上运行时,如果我使用延迟作业,它会失败。
这是详细信息。
控制器:
def importdo
fileimport = Fileimport.new
fileimport.file_name = params[:file].original_filename
fileimport.file_path = params[:file].path
fileimport.save
fileimportid = fileimport.id
Building.delay.import(fileimportid)
redirect_to buildings_path, notice: "Buildings import started . . . you will receive an email upon completion."
end
fileimport
只是我用来跟踪文件名和文件路径的表(我尝试将这些作为参数传递,但 DelayedJob 在尝试将哈希转换为 YAML 时总是会遇到问题,所以我只传递记录 ID)。这也给了我在表格中跟踪我的导入的好处。
这是我的导入类方法:
def self.import(fileimportid)
fileimport = Fileimport.find(fileimportid)
file_name = fileimport.file_name
file_path = fileimport.file_path
newcount = 0
updatecount = 0
updating = false
Building.acts_as_gmappable :process_geocoding => false
spreadsheet = open_spreadsheet(file_name, file_path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
if zip = row["zip"].to_i.to_s
if zip.length > 5
zip = zip.first(5)
end
else
raise "zip not valid"
end
if building = find_by_address_and_zip(row["address"], zip)
updating = true
else
building = Building.new
end
#building.name = row["name"]
building.zip = zip
building.address = row["address"]
building.city = row["city"]
building.save!
if updating
updatecount += 1
else
newcount += 1
end
updating=false
end
Building.acts_as_gmappable :process_geocoding => true
subject = "Your Building Import Completed Successfully!"
body = "Your Building Import completed successfully!\n\n" + newcount.to_s + " buildings were created.\n" + updatecount.to_s + " buildings were updated."
require 'rest_client'
RestClient.post MAILGUN_API_URL+"/messages", :from => "obfuscated", :to => "obfuscated", :subject => subject, :text => body
end
def self.open_spreadsheet(file_name, file_path)
case File.extname(file_name)
when ".csv" then Roo::Csv.new(file_path, nil, :ignore)
when ".xls" then Roo::Excel.new(file_path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file_path, nil, :ignore)
else
raise "Unknown file type: #{file_name}"
end
end
这是我的文件输入表单:
<% provide(:title, 'Import Buildings') %>
<div class="row">
<aside class="span4">
<section>
<h1>
Import Products
</h1>
<%= form_tag importdo_buildings_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
</section>
</aside>
</div>
路线如下所示:
resources :buildings do
collection { post :importdo }
end
无论如何,就像我说的那样,在我的盒子上工作得很好(无论有没有延迟工作)。只能在 Heroku 上工作而不会延迟工作(即我必须取出 .delay)。这是我收到的具体错误(错误通过电子邮件发送给我,我使用观察者检查 Delayed::Job 何时保存记录并检查 last_error):
file /tmp/RackMultipart20130516-13-7li3o7 does not exist
/app/vendor/bundle/ruby/1.9.1/gems/roo-1.11.2/lib/roo/excel.rb:26:in `block in initialize'
/app/vendor/bundle/ruby/1.9.1/gems/roo-1.11.2/lib/roo/generic_spreadsheet.rb:579:in `block in make_tmpdir'
/app/vendor/ruby-1.9.3/lib/ruby/1.9.1/tmpdir.rb:83:in `mktmpdir'
/app/vendor/bundle/ruby/1.9.1/gems/roo-1.11.2/lib/roo/generic_spreadsheet.rb:578:in `make_tmpdir'
/app/vendor/bundle/ruby/1.9.1/gems/roo-1.11.2/lib/roo/excel.rb:19:in `initialize'
/app/app/models/building.rb:84:in `new'
/app/app/models/building.rb:84:in `open_spreadsheet'
/app/app/models/building.rb:39:in `import'
我最初的想法是它只是一个 Heroku 文件系统的东西,但后来我认为如果没有 delay_job 它也行不通。我的另一个想法是,因为它是异步的,所以可能是时间问题(可能临时文件还不存在或不再存在)。
任何想法都非常感谢。