Class: Retailer::BatchPriceChecker
- Inherits:
-
Object
- Object
- Retailer::BatchPriceChecker
- 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.
Constant Summary collapse
- BATCH_SIZE =
Submit jobs in batches of 50
50
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#url_constructor ⇒ Object
readonly
Returns the value of attribute url_constructor.
Instance Method Summary collapse
-
#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.
-
#check_all_retailers ⇒ Hash
Check all catalogs that have external_price_check_enabled Jobs are submitted asynchronously - results arrive via webhook.
-
#check_catalog(catalog) ⇒ Integer
Check all items in a catalog using batch processing.
-
#check_single_item(catalog_item) ⇒ Integer
Check a single catalog item Only processes active items that aren't set to skip URL checks.
-
#initialize(options = {}) ⇒ BatchPriceChecker
constructor
A new instance of BatchPriceChecker.
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( = {}) @api = [:api] || Retailer::OxylabsApi.new(timeout: 120) @url_constructor = [:url_constructor] || Retailer::UrlConstructor.new @logger = [:logger] || Rails.logger @price_checker = Retailer::PriceChecker.new(api: @api, url_constructor: @url_constructor, logger: @logger) end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
25 26 27 |
# File 'app/services/retailer/batch_price_checker.rb', line 25 def api @api end |
#logger ⇒ Object (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_constructor ⇒ Object (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
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_retailers ⇒ Hash
Check all catalogs that have external_price_check_enabled
Jobs are submitted asynchronously - results arrive via webhook
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| submitted = check_catalog(catalog) results[:total_submitted] += submitted results[:catalogs][catalog.name] = submitted @logger.info "[BatchPriceChecker] #{catalog.name}: #{submitted} 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
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
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 |