Sidekiq 工作人员给出错误“没有将符号隐式转换为从捕获中排除的整数:DSN 未设置”

问题描述 投票:0回答:1

我想将促销代码从 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
  

我尝试运行代码,但每次它返回

no implicit conversion of Symbol into Integer excluded from capture: DSN not set 
我想知道代码是否被破坏。我是 Rails 的初学者。但似乎错误与参数有关。谢谢!

ruby-on-rails ruby sidekiq spree
1个回答
0
投票

当你写的时候,

       CouponsImportWorker.perform_async(
          params
        )

sidekiq 没有将 params 作为输入,因为它对于 sidekiq 来说相当复杂。传递给 Perform_async 的参数必须由简单的 JSON 数据类型组成:字符串、整数、浮点数、布尔值、null(nil)、数组和哈希。这意味着您不能使用 ruby 符号作为参数。

这里是 sideki 的最佳实践q

© www.soinside.com 2019 - 2024. All rights reserved.