上传csv文件,在后台导入和处理 [英] Upload a csv file, import and process in background

查看:769
本文介绍了上传csv文件,在后台导入和处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将CSV档案汇入我的资料库,例如:

I'm currently importing CSV files into my database like so:

class InventoryItemsController < ApplicationController

def import
    InventoryItem.import(params[:file], params[:store_id])
    redirect_to vendors_dashboard_path, notice: "Inventory Imported."
end

end

模型方法

class InventoryItem < ActiveRecord::Base

def self.import(file, store_id)
    CSV.foreach(file.path, headers: true) do |row|
    inventory_item = InventoryItem.find_or_initialize_by_upc_and_store_id(row[0], store_id)
    inventory_item.update_attributes(:price => row.to_hash["price"], :updated_at => "#{Time.now}")
end

end

问题是我现在开始做一些更大的csv文件(65k行)和应用程序只是挂在那里10分钟,而一切运行。有没有办法我可以上传文件,然后在后台处理?运行Rails 3和Ruby 1.9.3。

The problem is that I'm now beginning to do some larger csv files (65k rows) and the app just hangs there for 10 minutes while everything runs. Is there a way I can upload the file and then process it in the background? Running Rails 3 and Ruby 1.9.3.

推荐答案

你看过 delayed_job gem?它允许您对数据库中的作业进行排队,然后在后台异步处理它。

Have you looked at the delayed_job gem? It allows you to queue jobs in the database, and then process it asynchronously in the background.

来自github上的delayed_job README页面的快速说明:

Quick description from the delayed_job README page on github:


延迟::作业(或DJ)封装了在后台异步执行更长任务的常见模式。

Delayed::Job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background.



编辑:另请参阅sidekiq



另一个好的选择是sidekiq。您可以阅读 sidekiq常见问题,了解这些不同的异步处理宝石的效果。 Railscast 有点旧,但它仍然提供了一个很好的教程和介绍如何使用sidekiq 。

Also look at sidekiq

Another good option is sidekiq. You can see how these different asynch processing gems all compare by reading the sidekiq FAQ. The Railscast is a bit old, but it still gives a good tutorial and introduction on how to use sidekiq.

这篇关于上传csv文件,在后台导入和处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆