Class: GoogleLocalInventoryFeedWorker
- Inherits:
-
Object
- Object
- GoogleLocalInventoryFeedWorker
- Includes:
- Sidekiq::Worker
- Defined in:
- app/workers/google_local_inventory_feed_worker.rb
Overview
Background worker to pre-generate the Google Local Inventory feed
and store it in S3 via the Upload model. This ensures the API endpoint
can serve the feed quickly by redirecting to a presigned S3 URL.
Schedule this to run periodically (every 6 hours) to keep the feed fresh.
Can also be triggered manually: GoogleLocalInventoryFeedWorker.perform_async
Constant Summary collapse
- UPLOAD_CATEGORY =
'google_local_inventory_feed'- MAX_UPLOADS_PER_CATALOG =
Keep the last N uploads per catalog for debugging/rollback
3
Class Method Summary collapse
-
.generate_all ⇒ Object
Generate feeds for all main catalogs.
-
.latest_upload_for(catalog) ⇒ Upload?
Find the most recent feed upload for a catalog.
Instance Method Summary collapse
Class Method Details
.generate_all ⇒ Object
Generate feeds for all main catalogs
58 59 60 61 62 |
# File 'app/workers/google_local_inventory_feed_worker.rb', line 58 def self.generate_all Catalog.main_catalogs.each do |catalog| perform_async(catalog.id) end end |
.latest_upload_for(catalog) ⇒ Upload?
Find the most recent feed upload for a catalog
67 68 69 70 71 72 73 |
# File 'app/workers/google_local_inventory_feed_worker.rb', line 67 def self.latest_upload_for(catalog) Upload.where( resource_type: 'Catalog', resource_id: catalog.id, category: UPLOAD_CATEGORY ).order(created_at: :desc).first end |
Instance Method Details
#perform(catalog_id = nil) ⇒ Object
19 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 55 |
# File 'app/workers/google_local_inventory_feed_worker.rb', line 19 def perform(catalog_id = nil) catalog = Catalog.find(catalog_id || CatalogConstants::US_CATALOG_ID) logger.info("[GoogleLocalInventory] Starting feed generation for catalog #{catalog.id}") result = Feed::Google::LocalInventoryGenerator.new.process(catalogs: [catalog]) xml_content = result[:xml] product_count = result[:products] || 0 if xml_content.blank? logger.error('[GoogleLocalInventory] Generator returned empty XML') raise 'Generator returned empty XML' end logger.info("[GoogleLocalInventory] Generated #{product_count} products, #{xml_content.bytesize} bytes") # Store feed as Upload linked to Catalog file_name = "google_local_inventory_#{catalog.id}_#{Time.current.strftime('%Y%m%d_%H%M%S')}.xml" upload = Upload.uploadify_from_data( file_name: file_name, data: xml_content, category: UPLOAD_CATEGORY, resource: catalog ) unless upload&.persisted? logger.error('[GoogleLocalInventory] Failed to create upload') raise 'Upload creation failed' end logger.info("[GoogleLocalInventory] Successfully stored feed as Upload #{upload.id} for catalog #{catalog.id}") # Cleanup old uploads, keeping only the most recent ones cleanup_old_uploads(catalog) product_count end |