我使用的Roo的宝石导入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
只是我用来跟踪的文件名和文件路径表(我试图通过这些作为PARAMS而是试图哈希转换为YAML时DelayedJob总是有问题,所以我只是传入记录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)。这是我收到的特定错误(错误发送给我,我用一个观察者检查延迟时::招聘节省了记录,并检查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文件系统的东西,但后来我认为没有delayed_job也不行。我的另一个想法是,因为它是异步的,也许它是一个时间问题(也许临时文件或者还没有或者不存在了)。
任何想法,不胜感激。
我没有用在Heroku DJ,但我已经使用Resque - 我必须确保工作人员已经分手了,你有没有使用dynos来处理Delayed Job工作? – jbnunn
我正在使用无工作模式,并且DelayedJob正在运行,因为那是给我的错误,我可以尝试关闭无工作并且只需要工人准备好的情况下,它是一个时间的事情。 – bcb