Class: AmazonBuyBoxRecoveryWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/amazon_buy_box_recovery_worker.rb

Overview

Hourly worker to recover Buy Box for non-winners in Amazon US and Canada catalogs.
More responsive than the nightly AmazonPricingAutomationWorker - focuses only on
lowering prices to win back the Buy Box.

Schedule: Runs hourly, but skips during the nightly pricing automation window (3-7am CT)
to avoid conflicts with the full repricing run.

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/workers/amazon_buy_box_recovery_worker.rb', line 20

def perform(options = {})
  options = options.symbolize_keys

  # Skip during nightly pricing automation window to avoid conflicts
  # The nightly worker runs at 3am CT and processes all items including price increases
  current_hour = Time.current.in_time_zone('America/Chicago').hour
  if current_hour.in?(3..6)
    logger.info 'AmazonBuyBoxRecoveryWorker: Skipping during nightly pricing automation window (3-7am CT)'
    return
  end

  options[:catalog_ids] ||= [CatalogConstants::AMAZON_SC_US_CATALOG_ID, CatalogConstants::AMAZON_SC_CA_CATALOG_ID]

  logger.info 'AmazonBuyBoxRecoveryWorker starting...'
  start_time = Time.current

  result = Catalog::AmazonBuyBoxRecoveryService.new.process(options) do |status|
    total status[:total]
    at status[:at], status[:message]

    # Log progress every 10 items
    if status[:at] % 10 == 0
      elapsed = Time.current - start_time
      rate = status[:at] / elapsed * 60 # items per minute
      logger.info "Progress: #{status[:at]}/#{status[:total]} items (#{rate.round(1)}/min)"
    end
  end

  elapsed = Time.current - start_time
  logger.info "AmazonBuyBoxRecoveryWorker completed in #{elapsed.round(0)}s: " \
              "#{result.processed_count} processed, #{result.price_lowered_count} prices lowered, " \
              "#{result.flagged_count} flagged, #{result.skipped_count} skipped"

  store redirect_to: options[:redirect_to]
end