Techioz Blog

Sidekiq ワーカーで「シンボルから整数への暗黙的な変換がキャプチャから除外されません: DSN が設定されていません」というエラーが発生する

概要

プロモーションコードをCSVからデータベースにインポートしたい そのためにワーカーを作成しました 労働者は次のように派遣されている

def create
  CouponsImportWorker.perform_async(params)
  @status = Spree.t('coupons_import.in_progress')
  flash[:notice] = "CSV processing is in progress. You will be notified upon completion."
  redirect_back fallback_location: spree.admin_promotions_path
end

下が作業員です

require 'csv'
require 'json'

class CouponsImportWorker
  include Sidekiq::Worker
  
  def perform(params)
    Rails.logger.info("Worker is running  #{params[:coupons_import]}")
    # begin

      products = []

        params[:coupons_import][:product].split(',').each do |product_id|
          products << Spree::Product.find(product_id)
        end
      # Rails.logger.info("Worker is running after finding products ")
      filename = params[:coupons_import][:file].path
      Rails.logger.info(filename)
      keys = File.readlines(filename)
      csv_table = CSV.parse(File.read(filename), headers: true, col_sep: ';')
      codes = csv_table.map(&:to_h).map { |e| e['code'] }
     
      Rails.logger.info("Worker is running after finding codes ")

      percent = params[:coupons_import][:percent].to_i
      amount = params[:coupons_import][:amount].to_d
      Rails.logger.info("just before checking if percent and amount are more than 0")
     if percent > 0 || amount > 0
      codes.each do |code|
        promotion = Spree::Promotion.create(
          name: params[:coupons_import][:promo_name],
          description: params[:coupons_import][:promo_desc],
          match_policy: 'all',
          usage_limit: params[:coupons_import][:usage_limit],
          starts_at: params[:coupons_import][:starts_at],
          expires_at: params[:coupons_import][:expires_at],
          code: code,
          store_ids: 1
        )
        Rails.logger.info("just before calculating flatrate")
        if amount > 0
          promotion.promotion_actions << Spree::Promotion::Actions::CreateAdjustment.create(
            calculator: Spree::Calculator::FlatRate.new(preferred_amount: amount)
          )
        else
          promotion.promotion_actions << Spree::Promotion::Actions::CreateItemAdjustments.create(
            calculator: Spree::Calculator::PercentOnLineItem.new(preferred_percent: percent)
          )
        end
  
        if params[:coupons_import][:option_values].blank?
          create_promotion_rule(promotion, products)
        else
          create_promotion_option_value_rule(promotion, products, params[:coupons_import][:option_values])
        end
       end
     end 
      Rails.logger.info("running fine until now.....")
      ActionCable.server.broadcast('coupons_channel', message: 'CSV processing completed.')
    # rescue StandardError => e
    #  Rails.logger.error("Error in CouponsImportWorker: #{e}")
    # end   
    end
  
    private
  
    def create_promotion_rule(promotion, products)
      rule = Spree::Promotion::Rules::Product.create(promotion: promotion)
      rule.products << products
      rule.save!
    end
  
    def create_promotion_option_value_rule(promotion, products, option_value)
      Spree::Promotion::Rules::OptionValue.create(
        promotion_id: promotion.id,
        type: 'Spree::Promotion::Rules::OptionValue',
        preferences: {
          eligible_values: { products.last.id => [option_value] },
          match_policy: 'any'
        }
      )
    end
  end
  

コードを実行しようとしましたが、毎回返されます シンボルから整数への暗黙的な変換はキャプチャから除外されません: DSN が設定されていません コードが壊れていないか知りたいです。私はRailsの初心者です。しかし、エラーはparamsに関連しているようです。ありがとうございます!

解決策

ここにあなたが書いているように、

CouponsImportWorker.perform_async(params)

Sidekiq は、params(ActionController::Parameters) を入力として受け取りませんでした。これは、sidekiq にとって非常に複雑であるためです。perform_async に渡す引数は、単純な JSON データ型 (string、integer、float、boolean、null(nil)、配列とハッシュ。これは、ルビ記号を引数として使用してはいけないことを意味します。

これがsidekiqのベストプラクティスです