ruby Sidekiq worker出现错误“no implicit conversion of Symbol to symbol excluded from capture:DSN not set”(没有从捕获中隐式转换为符号),

nqwrtyyt  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(169)

我想从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的初学者。但似乎错误与参数有关。谢谢!

ssgvzors

ssgvzors1#

在你写作时,

CouponsImportWorker.perform_async(params)

字符串
sidekiq不接受params(parameter Controller::Parameters)作为输入,因为它对sidekiq来说相当复杂。传递给perform_parameter c的参数必须由简单的JSON数据集组成:string,integer,float,boolean,null(nil),array和hash。这意味着你不能使用ruby符号作为参数。
这里是best practices for sideki q

相关问题