Class: FeaturedRestaurants::AiTranslationWorker

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers, Sidekiq::Worker
Defined in:
app/workers/featured_restaurants/ai_translation_worker.rb

Overview

Background worker for translating featured restaurant fields with AI This worker processes featured restaurant translations asynchronously to avoid request timeouts

Usage:

FeaturedRestaurants::AiTranslationWorker.perform_async(
  translation_id,
  featured_restaurant_id,
  source_language,
  target_languages,
  target_fields,
  only_blank_languages,
  only_blank_fields
)

Results are stored in Redis with key: “featured_restaurant_translation:#translation_id” TTL: 5 minutes

Queue: :default (production) / :critical (staging/local) Retry: 2 attempts with exponential backoff

Instance Method Summary collapse

Instance Method Details

#perform(translation_id, featured_restaurant_id, source_language, target_languages, target_fields, only_blank_languages, only_blank_fields) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
100
101
102
103
104
105
106
# File 'app/workers/featured_restaurants/ai_translation_worker.rb', line 38

def perform(translation_id, featured_restaurant_id, source_language, target_languages, target_fields, only_blank_languages, only_blank_fields)
  BUSINESS_LOGGER.set_business_context({ featured_restaurant_id: featured_restaurant_id,
                                         translation_id: translation_id })
  BUSINESS_LOGGER.info('Starting featured restaurant AI translation', {
                         source_language: source_language,
                         target_languages: target_languages,
                         target_fields: target_fields,
                         only_blank_languages: only_blank_languages,
                         only_blank_fields: only_blank_fields,
                       })

  # Store initial "processing" status in Redis so frontend can track immediately
  store_processing_status(translation_id)

  featured_restaurant = CompactRestaurant.find_by(id: featured_restaurant_id)
  unless featured_restaurant
    error_message = 'Featured restaurant not found'
    store_translation_error(translation_id, error_message)
    BUSINESS_LOGGER.error('Featured restaurant translation failed', { error: error_message })
    return
  end

  # Call the translation service
  translation_service = FeaturedRestaurants::AiTranslationService.new(
    featured_restaurant,
    source_language: source_language,
    target_languages: target_languages,
    target_fields: target_fields,
    only_blank_languages: only_blank_languages,
    only_blank_fields: only_blank_fields,
  )

  result = translation_service.call

  if result[:success]
    translations = result[:translations] || {}
    store_translation_result(translation_id, translations, result[:message])

    BUSINESS_LOGGER.info('Featured restaurant translation completed successfully', {
                           translation_id: translation_id,
                           languages_translated: translations.keys.size,
                           fields_count: translations.values.first&.keys&.size || 0,
                         })
  else
    error_message = result[:message] || 'Translation failed'
    store_translation_error(translation_id, error_message)

    BUSINESS_LOGGER.error('Featured restaurant translation failed', {
                            translation_id: translation_id,
                            error: error_message,
                          })
  end
rescue StandardError => e
  APMErrorHandler.report(e, context: {
                           translation_id: translation_id,
                           featured_restaurant_id: featured_restaurant_id,
                           source_language: source_language,
                           target_languages: target_languages,
                         })

  error_message = "Unexpected error: #{e.message}"
  store_translation_error(translation_id, error_message)

  BUSINESS_LOGGER.error('Featured restaurant translation encountered error', {
                          translation_id: translation_id,
                          error: error_message,
                          backtrace: e.backtrace.first(5),
                        })
end