Class: ReviewsIoFeedGeneratorWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/reviews_io_feed_generator_worker.rb

Overview

Background worker to pre-generate the Reviews.io product catalog 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: ReviewsIoFeedGeneratorWorker.perform_async

Constant Summary collapse

UPLOAD_CATEGORY =
'reviews_io_product_feed'
MAX_UPLOADS_TO_KEEP =

Keep the last N uploads for debugging/rollback

3

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.latest_uploadUpload?

Find the most recent feed upload

Returns:

  • (Upload, nil)

    The most recent feed upload, or nil if none exists



55
56
57
58
59
60
# File 'app/workers/reviews_io_feed_generator_worker.rb', line 55

def self.latest_upload
  Upload.where(
    resource_type: nil,
    category: UPLOAD_CATEGORY
  ).order(created_at: :desc).first
end

Instance Method Details

#performObject



18
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
# File 'app/workers/reviews_io_feed_generator_worker.rb', line 18

def perform
  logger.info('[ReviewsIo] Starting feed generation')

  result = Feed::ReviewsIo::ProductCatalogGenerator.new.process
  xml_content = result[:xml]
  product_count = result[:products] || 0

  if xml_content.blank?
    logger.error('[ReviewsIo] Generator returned empty XML')
    raise 'Generator returned empty XML'
  end

  logger.info("[ReviewsIo] Generated #{product_count} products, #{xml_content.bytesize} bytes")

  # Store feed as Upload (no resource - this is a global feed)
  file_name = "reviews_io_product_catalog_#{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
  )

  unless upload&.persisted?
    logger.error('[ReviewsIo] Failed to create upload')
    raise 'Upload creation failed'
  end

  logger.info("[ReviewsIo] Successfully stored feed as Upload #{upload.id}")

  # Cleanup old uploads, keeping only the most recent ones
  cleanup_old_uploads

  product_count
end