Class: GroupLandingPages::AiFieldTranslationWorker

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

Overview

Sidekiq worker for field-level AI translation

Instance Method Summary collapse

Instance Method Details

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



12
13
14
15
16
17
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
# File 'app/workers/group_landing_pages/ai_field_translation_worker.rb', line 12

def perform(group_landing_id, translation_id, field_name, source_language, source_text, target_languages)
  group_landing = GroupLandingPage.find(group_landing_id)

  redis_key = "group_landing_field_translation:#{group_landing_id}:#{translation_id}"

  # Set initial status
  $persistent_redis.with do |redis|
    redis.setex(redis_key, 300, { status: 'processing' }.to_json) # 5 minutes TTL
  end

  BUSINESS_LOGGER.info('group_landing_field_ai_translation_started', {
                         group_landing_id: group_landing_id,
                         translation_id: translation_id,
                         field_name: field_name,
                         source_language: source_language,
                         target_languages: target_languages,
                       })

  # Call the field translation service
  service = GroupLandingPages::AiFieldTranslationService.new(
    field_name: field_name,
    source_language: source_language,
    source_text: source_text,
    target_languages: target_languages,
  )

  result = service.call

  if result[:success]
    # Store successful result
    $persistent_redis.with do |redis|
      redis.setex(redis_key, 300, {
        status: 'completed',
        translations: result[:translations],
      }.to_json)
    end

    BUSINESS_LOGGER.info('group_landing_field_ai_translation_completed', {
                           group_landing_id: group_landing_id,
                           translation_id: translation_id,
                           field_name: field_name,
                           translated_languages: result[:translations].keys,
                         })
  else
    # Store failure
    $persistent_redis.with do |redis|
      redis.setex(redis_key, 300, {
        status: 'failed',
        error: result[:message],
      }.to_json)
    end

    BUSINESS_LOGGER.error('group_landing_field_ai_translation_failed', {
                            group_landing_id: group_landing_id,
                            translation_id: translation_id,
                            field_name: field_name,
                            error: result[:message],
                          })
  end
rescue StandardError => e
  APMErrorHandler.report(e, context: {
                           group_landing_id: group_landing_id,
                           translation_id: translation_id,
                           field_name: field_name,
                         })

  redis_key = "group_landing_field_translation:#{group_landing_id}:#{translation_id}"
  $persistent_redis.with do |redis|
    redis.setex(redis_key, 300, {
      status: 'failed',
      error: e.message,
    }.to_json)
  end

  BUSINESS_LOGGER.error('group_landing_field_ai_translation_exception', {
                          group_landing_id: group_landing_id,
                          translation_id: translation_id,
                          field_name: field_name,
                          error: e.message,
                        })
end