Class: ItemizableDiscountResetWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/itemizable_discount_reset_worker.rb

Instance Method Summary collapse

Instance Method Details

#perform(itemizable_type, itemizable_id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/workers/itemizable_discount_reset_worker.rb', line 6

def perform(itemizable_type, itemizable_id)
  itemizable = itemizable_type.constantize.find(itemizable_id)
  logger.info "Updating discount on #{itemizable_type} #{itemizable_id}"

  if itemizable.store.nil?
    logger.warn "Skipping discount reset for #{itemizable_type} #{itemizable_id}: no store found " \
                "(from_store=#{itemizable.try(:from_store_id).inspect}, customer=#{itemizable.try(:customer_id).inspect})"
    return
  end

  ErrorReporting.scoped({ itemizable_type: itemizable_type, itemizable_id: itemizable_id }) do
    Retryable.retryable(tries: 3, sleep: ->(n) { 2**n }, on: [ActiveRecord::Deadlocked]) do |attempt_number, exception|
      if attempt_number > 1
        logger.warn(
          "Deadlock while resetting discount on #{itemizable_type} #{itemizable_id}; " \
          "attempt #{attempt_number}/3 (#{exception.class}: #{exception.message})"
        )
      end
      itemizable.reset_discount
    end
  end
end