Class: AddOns::FieldTranslationWorker

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

Overview

Background worker for add-on field translations Handles long-running AI translation requests that exceed Rack timeout

Instance Method Summary collapse

Instance Method Details

#perform(translation_id, source_text, field_name, source_language, target_languages) ⇒ Object



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

def perform(translation_id, source_text, field_name, source_language, target_languages)
  BUSINESS_LOGGER.set_business_context({
                                         translation_id: translation_id,
                                         field_name: field_name,
                                       })

  BUSINESS_LOGGER.info('Starting add-on field translation job', {
                         source_language: source_language,
                         target_languages: target_languages,
                         text_length: source_text.length,
                       })

  result = AddOns::FieldTranslationService.new(
    add_on_id: nil, # Not needed for field translation
    source_text: source_text,
    field_name: field_name,
    source_language: source_language,
    target_languages: target_languages,
  ).call

  if result[:success]
    # Store result in Redis with 5-minute expiration
    translations = result[:data][:translations] || {}

    BUSINESS_LOGGER.info('Add-on field translation result received', {
                           translation_id: translation_id,
                           translations_data: translations,
                           translations_keys: translations.keys,
                           translations_count: translations.keys.size,
                         })

    # Check for partial success (some languages translated, some failed)
    requested_count = target_languages.size
    translated_count = translations.keys.size

    if translated_count == 0
      # No translations succeeded - treat as failure
      error_message = 'All translations failed. Please try translating fewer languages or check the source text.'
      store_translation_error(translation_id, error_message)

      BUSINESS_LOGGER.error('Add-on field translation failed - no languages translated', {
                              translation_id: translation_id,
                              requested_languages: target_languages,
                            })
    elsif translated_count < requested_count
      # Partial success - some languages translated
      failed_languages = target_languages - translations.keys
      store_translation_result(translation_id, translations, partial: true, failed_languages: failed_languages)

      BUSINESS_LOGGER.warn('Add-on field translation partially completed', {
                             translation_id: translation_id,
                             requested_count: requested_count,
                             translated_count: translated_count,
                             succeeded_languages: translations.keys,
                             failed_languages: failed_languages,
                           })
    else
      # Full success - all languages translated
      store_translation_result(translation_id, translations)

      BUSINESS_LOGGER.info('Add-on field translation completed', {
                             translation_id: translation_id,
                             languages_count: translations.keys.size,
                           })
    end
  else
    # Store error in Redis
    store_translation_error(translation_id, result[:message] || 'Translation failed')

    BUSINESS_LOGGER.error('Add-on field translation failed', {
                            translation_id: translation_id,
                            error: result[:message],
                          })
  end
rescue StandardError => e
  store_translation_error(translation_id, e.message)

  APMErrorHandler.report(e, context: {
                           translation_id: translation_id,
                           field_name: field_name,
                         })

  BUSINESS_LOGGER.error('Add-on field translation worker error', {
                          translation_id: translation_id,
                          error: e.message,
                          backtrace: e.backtrace.first(5),
                        })
end