csvファイルをインポートしたら、csvを保存する処理の裏でsidekiqを使ってjobを動かす。
class ClientImportsController < ApplicationController
略
def create
@client_import = ClientImport.new(client_import_params)
if @client_import.save
ClientImportJob.perform_later(@client_import)
redirect_to client_imports_path, notice: action_message
else
render :new
end
end
略
end
データをバリデーションチェックして、新しいデータ&更新されたデータをClientsテーブルに保存する処理を実行。
require 'csv'
class ClientImportJob < ApplicationJob
略
def perform(client_import)
@client_import = client_import
@client_import.update status: :processing
if Rails.env == "development"
temp_path = @client_import.import_file.current_path
elsif Rails.env == "production"
temp_path = @client_import.import_file.url
end
CSV.read(temp_path, headers: true).each do |row|
client = Client.find_or_initialize_by(id: row['client_id'])
client_attributes = Client.find_or_initialize_by(
id: row['client_id'],
company_name: row['*会社名'],
department_name: row['部署名'],
postal_code: row['郵便番号'],
address: row['住所'],
building_name: row['ビル名'],
staff_position_name: row['役職名'],
staff_name: row['担当者名'],
staff_email: row['担当者メールアドレス'],
nda_checked: row['契約書類チェック'],
check_by_id: row['*media4u担当者'],
approve_by_id: row['*承認者'],
yayoi_code: row['*弥生コード'],
obc_code: row['勘定奉行コード'],
memo: row['メモ'],
ceil_digit: row['*切り上げ桁'],
need_items_sum: row['合算必要性'],
important: row['重要フラグ']
)
client.update!(client_attributes)
end
@client_import.update! status: :finished
end
略
end
ClientImport Load (0.5ms) SELECT "client_imports".* FROM "client_imports" WHERE "client_imports"."id" = $1 LIMIT $2 [["id", 13], ["LIMIT", 1]]
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] Performing ClientImportJob (Job ID: 9336254b-dbdd-454f-b761-7eb037ac5afc) from Sidekiq(default) with arguments: #<GlobalID:0x00007fa88d92a968 @uri=#<URI::GID gid://m4ware/ClientImport/13>>
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] (0.1ms) BEGIN
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] SQL (43.2ms) UPDATE "client_imports" SET "status" = $1, "updated_at" = $2 WHERE "client_imports"."id" = $3 [["status", "processing"], ["updated_at", "2019-04-05 14:03:23.110657"], ["id", 13]]
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] (40.5ms) COMMIT
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] Client Load (0.4ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] Client Load (0.7ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 AND "clients"."company_name" = $2 AND "clients"."department_name" = $3 AND "clients"."postal_code" = $4 AND "clients"."address" = $5 AND "clients"."building_name" = $6 AND "clients"."staff_position_name" IS NULL AND "clients"."staff_name" = $7 AND "clients"."staff_email" = $8 AND "clients"."nda_checked" = $9 AND "clients"."check_by_id" = $10 AND "clients"."approve_by_id" = $11 AND "clients"."yayoi_code" = $12 AND "clients"."obc_code" = $13 AND "clients"."memo" IS NULL AND "clients"."ceil_digit" = $14 AND "clients"."need_items_sum" = $15 AND "clients"."important" = $16 LIMIT $17 [["id", 1], ["company_name", "株式会社 あいうソリューションズ"], ["department_name", "システム開発部"], ["postal_code", "1410022"], ["address", "東京都品川区東五反田1-11-15"], ["building_name", "電波ビル xF"], ["staff_name", "林 佳志乃"], ["staff_email", "yamada@test1.com"], ["nda_checked", "FALSE"], ["check_by_id", 0], ["approve_by_id", 0], ["yayoi_code", "F601"], ["obc_code", "701"], ["ceil_digit", 0], ["need_items_sum", "FALSE"], ["important", "FALSE"], ["LIMIT", 1]]
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] (0.2ms) BEGIN
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] (0.3ms) ROLLBACK
[ActiveJob] [ClientImportJob] [9336254b-dbdd-454f-b761-7eb037ac5afc] Error performing ClientImportJob (Job ID: 9336254b-dbdd-454f-b761-7eb037ac5afc) from Sidekiq(default) in 213.55ms: ArgumentError (When assigning attributes, you must pass a hash as an argument.):
今日得られたもの
sidekiqのログの見方が分からずデバッグに困っていた
→ lib/development.log の中にsidekiqのログが入っているとわかった。
明日以降の課題
・エラーを直す
・Sidekiqのセットアップ方法は学べていないので、自分で導入も行ってみたい