ruby-on-rails - Ruby resque 后台 CSV 导入不运行

标签 ruby-on-rails ruby csv redis resque

我正在尝试使用带有 resque 的后台作业导入 CSV 文件。它似乎在运行,但没有任何反应。当我不使用 resque 时,导入工作正常(我使用 resque 是因为有些导入很大,我想转到后台作业,以测试它的小 2 行 csv)

非常感谢任何帮助,非常感谢! (我也是初学者,所以请降低任何帮助:))

库存 Controller .rb :

  def import
    Resque.enqueue(Inventorycsvimport, params[:file], current_user.id)
    redirect_to root_url, notice: "Inventory import job started."
  end

worker JOB inventorycsvimport.rb :

class Inventorycsvimport
  @queue = :Inventorycsvimport
  def self.perform()
    Inventory.destroy_all(user_id: current_user.id)
    Inventory.import(params[:file], current_user.id)
  end
end

导入类 inventory.rb :

class Inventory < ApplicationRecord
    belongs_to :user

  def self.import(file, user_id)
    allowed_attributes = [ "user_id", "id","description","part_number","price","created_at","updated_at", "alternate_part_number", "condition_code", "qty", "mfg_code", "serial_number", "part_comments"]
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      inventory = find_by_id(row["id"]) || new
      inventory.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
      inventory.user_id = user_id
      inventory.save!
    end
  end

  def self.open_spreadsheet(file)
    case File.extname(file.original_filename)
      when ".csv" then Roo::CSV.new(file.path)
      when ".xls" then Excel.new(file.path, nil, :ignore)
      when ".xlsx" then Excelx.new(file.path, nil, :ignore)
    else raise "Unknown file type: #{file.original_filename}"
    end
  end
end

这是我在作业中遇到的错误:

enter image description here

最佳答案

我建议您查看 Sidekiq 文档。据我所知,所有传入的参数都可以通过 options 散列在作业中使用。所以当传递参数时,你需要以某种方式在另一端接收它。

在 Controller 中试试这个:

  def import
    Resque.enqueue(Inventorycsvimport, file: params[:file], user_id: current_user.id)
    redirect_to root_url, notice: "Inventory import job started."
  end

在你的工作中:

  def self.perform
    Inventory.destroy_all(user_id: options[:user_id])
    Inventory.import(options[:file], options[:user_id])
  end

你的代码没有多大意义(我建议一些编程教程(codecademy.com 可能是一个好的开始),因为你试图在不同的范围内使用变量。current_user你在你的 Controller 中拥有的不是一个你可以在任何地方访问的神奇变量。它来自 ActionController::BaseApplicationController 类下定义的辅助方法,你的 Controller 类从这些类继承.

由于您的 Job 类与 Controller 类无关,因此它不能直接访问 Controller 的任何方法或变量。因此,在定义方法时,您可以使用诸如 parameters 之类的选项,这使您可以跨不同的类和范围传递这些变量。

在这种情况下可能令人困惑的是作业的 perform 方法,您不必将 options 声明为参数,因为它使用 ActiveJob (或普通的 Sidekiq)类,它具有与定义为参数的 options 散列相同的方法。 file:user_id: 只是 Ruby 中称为命名参数 的漂亮东西。因此,在使用函数时,您不必经常检查参数顺序。但是,srsly,我建议学习一些教程,你很快就会得到这个:)

关于ruby-on-rails - Ruby resque 后台 CSV 导入不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443335/

相关文章:

ruby-on-rails - 根据上下文自定义验证错误消息

javascript - 如何有效地使用 Jasmine 来测试通过 Jammit 打包的 javascript Assets ?

ruby-on-rails - 如何在 Rails 3 中从 ActionMailer 发送签名的电子邮件?

python - 如何通过python将多页数据导出到excel?

Python 选择特定的行和列

ruby-on-rails - 带有 simple_form 的按钮内的 HTML 代码

mysql - 跳过 mysql 更新中锁定的行以避免锁定超时

ruby - 超时和打开超时有什么区别?

ruby-on-rails - rails : Methods shared by multiple controllers

javascript - 将对象的属性转换为逗号分隔的列表?