Rails - 使用延迟作业时Roo“文件不存在”错误

时间:2013-05-16 15:11:58

标签: ruby-on-rails heroku delayed-job import-from-excel

我正在使用Roo gem导入Excel文件,CSV文件等。

代码很有用a)在我的本地方框上有或没有延迟工作,和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)。这是我得到的具体错误(错误通过电子邮件发送给我,我使用观察者检查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文件系统的东西,但是我认为如果没有delayed_job它也行不通。我的另一个想法是,因为它是异步的,也许它是一个计时问题(也许临时文件不存在或者不存在)。

非常感谢任何想法。

1 个答案:

答案 0 :(得分:0)

你最初的想法是正确的..你的问题背后是Heroku文件系统。即只读规则!

当您向Heroku发送文件时,它将存储在临时文件夹中,仅在此一个请求期间可用。它可以在没有delayed_job的情况下工作,因为您在一个请求期间完成工作并且文件可用。

但是当request请求结束时,delayed_job正在单独完成,因此删除了文件。

最简单的解决方案是将文件放在某个云存储中(我为此目的使用了S3和carrierwave。