2016-09-12 4 views
0

resqueでバックグラウンドジョブを使用してCSVファイルをインポートしようとしています。走っているように見えますが、何も起こりません。私はresqueを使用していなかったときにインポートが正常に機能しました(いくつかのインポートが大きいので、いくつかのインポートが大きいので、小さな2行のCSVをテストするためにバックグラウンドジョブに移りたいので使用しています)Ruby resque background CSVインポートが実行されません

あなたは非常に! (も初心者イムので、任意の助けダウンダム喜ば:))

inventories_controller.rb:

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

ワーカー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

+0

ステップバイステップを試してみましたが、実際に失敗したところで試しましたか?あなたの結果を同じように提供してください – Bijendra

+0

'current_user'をどのようにしてジョブの中に入れますか?パラメータとして渡されていないようです。それは仕事の中で「無」であるかもしれません。その場合、 'inventory'は有効な' user'を必要とするため、検証は失敗します。この場合、保存されず、何も起こりません( 'ValidationError'が生成されるので、' – Kkulikovskis

+0

@Kkulikovskisコントローラを変更しました:def import Resque.enqueue(Inventorycsvimport、params [:file]、current_user.id) redirect_to root_url、注意:「インベントリインポートジョブが開始しました。 終わり、今私が得る例外 NameError エラー 未定義のローカル変数やメソッドInventorycsvimportため 'CURRENT_USER」:クラス –

答えて

0

サイドキックのドキュメントを確認することをお勧めします。私が知る限り、渡されたすべてのパラメータは、optionsハッシュを通じてジョブ内で使用できます。したがって、パラメータを渡すときには、何らかの形で相手側で受け取る必要があります。

コントローラでこれを試してみてください:

あなたの仕事で
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は、どこでもアクセスできる魔法の変数ではなく、ActionController::BaseまたはApplicationControllerで定義されているヘルパーメソッドから来ていますあなたのコントローラクラスが継承するクラス。

ジョブクラスはコントローラクラスとは関係がないため、コントローラのメソッドや変数に直接アクセスすることはできません。したがって、メソッドを定義するときにparametersのようなオプションがあります。これらの変数をさまざまなクラスやスコープに渡すことができます。それはoptionsと同じ方法を持っているボンネットの下にActiveJob(またはプレーンSidekiq)クラスを使用しているため、この場合には混乱するかもしれない何

は、あなたが引数としてoptionsを宣言する必要はありません、ジョブのperform方法であります引数として定義されたハッシュfile:user_id:は、named argumentsと呼ばれるRubyのちょうどいいところです。したがって、関数を使用する場合、パラメータの順序を常に確認する必要はありません。しかしsrsly、私はいくつかのチュートリアルを取ることをお勧めします。あなたはすぐにこれを取得します:)

関連する問題