Sha256: 0334aee3eb96ae06a21240b46858ade4b17c2f5b9a75f8d0b3e0a5bf140d6725

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

module Workarea
  class IndexCategoryChanges
    include Sidekiq::Worker
    include Sidekiq::CallbacksWorker

    sidekiq_options(
      enqueue_on: { Catalog::Category => [:save, :save_release_changes], with: -> { [changes] } },
      ignore_if: -> { changes['product_ids'].blank? },
      lock: :until_executing,
      query_cache: true
    )

    def perform(changes)
      return unless changes['product_ids'].present?

      ids = require_index_ids(*changes['product_ids'])

      if ids.size > max_count
        ids.each { |id| IndexProduct.perform_async(id) }
      else
        Catalog::Product.in(id: ids).each do |product|
          begin
            IndexProduct.perform(product)
          rescue
            IndexProduct.perform_async(product.id)
          end
        end
      end
    end

    def require_index_ids(previous_ids, new_ids)
      previous_ids = Array.wrap(previous_ids)
      new_ids = Array.wrap(new_ids)

      new_ids.reject do |id|
        previous_ids.index(id).present? &&
          previous_ids.index(id) == new_ids.index(id)
      end + (previous_ids - new_ids)
    end

    def max_count
      Workarea.config.category_inline_index_product_max_count
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
workarea-core-3.5.12 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.11 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.10 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.9 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.8 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.7 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.6 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.5 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.4 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.3 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.2 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.1 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.0 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.0.beta.1 app/workers/workarea/index_category_changes.rb