Class: Retailer::BatchPriceChecker

Inherits:
Object
  • Object
show all
Defined in:
app/services/retailer/batch_price_checker.rb

Overview

Batch price checker using Oxylabs Push-Pull API with webhook callbacks.
Submits jobs asynchronously with per-item callback URLs. Results are
processed by WebhookProcessors::OxylabsProcessor when webhooks arrive.

Creates pending WebhookLog entries for each submitted job to enable:

  • Audit trail of all submitted jobs
  • Detection of missed webhook callbacks
  • Retry logic via StaleTranscriptionRecoveryWorker

In development, use the dev tunnel (script/dev_tunnel_api.sh) to receive callbacks.

Examples:

Check all retailers

checker = Retailer::BatchPriceChecker.new
checker.check_all_retailers

Check a single catalog

checker = Retailer::BatchPriceChecker.new
checker.check_catalog(Catalog.find(18))

Constant Summary collapse

BATCH_SIZE =

Submit jobs in batches of 50

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BatchPriceChecker

Returns a new instance of BatchPriceChecker.



27
28
29
30
31
32
# File 'app/services/retailer/batch_price_checker.rb', line 27

def initialize(options = {})
  @api = options[:api] || Retailer::OxylabsApi.new(timeout: 120)
  @url_constructor = options[:url_constructor] || Retailer::UrlConstructor.new
  @logger = options[:logger] || Rails.logger
  @price_checker = Retailer::PriceChecker.new(api: @api, url_constructor: @url_constructor, logger: @logger)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



25
26
27
# File 'app/services/retailer/batch_price_checker.rb', line 25

def api
  @api
end

#loggerObject (readonly)

Returns the value of attribute logger.



25
26
27
# File 'app/services/retailer/batch_price_checker.rb', line 25

def logger
  @logger
end

#url_constructorObject (readonly)

Returns the value of attribute url_constructor.



25
26
27
# File 'app/services/retailer/batch_price_checker.rb', line 25

def url_constructor
  @url_constructor
end

Instance Method Details

#callback_url_for(catalog_item_id) ⇒ String

Generate a callback URL with a time-limited authentication token (24 hours)
Each job gets its own callback URL with the catalog_item_id embedded

Parameters:

  • catalog_item_id (Integer)

    The catalog item ID to embed in the callback URL

Returns:

  • (String)

    The callback URL based on environment:

    • Production: api.warmlyyours.com
    • Staging: api.warmlyyours.ws
    • Development: api-hostname.warmlyyours.dev (via Cloudflare tunnel)


41
42
43
44
45
46
47
48
# File 'app/services/retailer/batch_price_checker.rb', line 41

def callback_url_for(catalog_item_id)
  if Rails.env.development?
    Retailer::CallbackTokenService.dev_callback_url(catalog_item_id: catalog_item_id)
  else
    # Production and staging use API_HOSTNAME_WITHOUT_PORT constant
    Retailer::CallbackTokenService.callback_url(catalog_item_id: catalog_item_id)
  end
end

#check_all_retailersHash

Check all catalogs that have external_price_check_enabled
Jobs are submitted asynchronously - results arrive via webhook

Returns:

  • (Hash)

    Summary of submitted jobs by catalog



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/retailer/batch_price_checker.rb', line 53

def check_all_retailers
  catalogs = Catalog.where(external_price_check_enabled: true)
  results = { total_submitted: 0, catalogs: {} }

  catalogs.each do |catalog|
     = check_catalog(catalog)

    results[:total_submitted] += 
    results[:catalogs][catalog.name] = 

    @logger.info "[BatchPriceChecker] #{catalog.name}: #{} jobs submitted"
  end

  @logger.info "[BatchPriceChecker] Total: #{results[:total_submitted]} jobs submitted, awaiting callbacks"
  results
end

#check_catalog(catalog) ⇒ Integer

Check all items in a catalog using batch processing

Parameters:

Returns:

  • (Integer)

    Number of jobs submitted



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/retailer/batch_price_checker.rb', line 73

def check_catalog(catalog)
  items = catalog.catalog_items
                 .where(state: 'active')
                 .where(skip_url_checks: false)
                 .includes(:catalog)
                 .to_a

  return 0 if items.empty?

  # For Wayfair catalogs, skip items with fresh API data (synced within 24 hours)
  # The Wayfair Catalog API provides authoritative pricing, making scraping secondary
  if wayfair_catalog?(catalog)
    original_count = items.size
    items = items.reject { |item| wayfair_api_data_fresh?(item) }
    skipped_count = original_count - items.size
    @logger.info "[BatchPriceChecker] Skipped #{skipped_count} items with fresh Wayfair API data (< 24h old)" if skipped_count > 0
  end

  return 0 if items.empty?

  @logger.info "[BatchPriceChecker] Processing #{items.size} items for #{catalog.name}"

  # Process in batches and count total submitted
  items.each_slice(BATCH_SIZE).sum do |batch|
    process_batch(batch, catalog)
  end
end

#check_single_item(catalog_item) ⇒ Integer

Check a single catalog item
Only processes active items that aren't set to skip URL checks

Parameters:

Returns:

  • (Integer)

    1 if submitted, 0 if skipped



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/services/retailer/batch_price_checker.rb', line 105

def check_single_item(catalog_item)
  # Only check active items (skip pending_onboarding, phased_out, etc.)
  unless catalog_item.state == 'active'
    @logger.info "[BatchPriceChecker] Skipping item #{catalog_item.id} - state is '#{catalog_item.state}' (not active)"
    return 0
  end

  # Skip items explicitly marked to not check URLs
  if catalog_item.skip_url_checks?
    @logger.info "[BatchPriceChecker] Skipping item #{catalog_item.id} - skip_url_checks is true"
    return 0
  end

  @logger.info "[BatchPriceChecker] Processing single item #{catalog_item.id}"
  process_batch([catalog_item], catalog_item.catalog)
end