9
10
11
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'app/workers/restaurant_tags/bulk_ai_translate_worker.rb', line 9
def perform(tag_ids, target_languages, only_blank_fields = false)
job_id = jid redis_key = "bulk_translate_job:#{job_id}"
restaurant_tags = RestaurantTag.where(id: tag_ids).where.not(title_en: [nil, ''])
if restaurant_tags.blank?
update_job_status(redis_key, 'completed', 0, 0, 100, 'No restaurant tags found')
BUSINESS_LOGGER.warn('No restaurant tags found for bulk translation', { tag_ids: tag_ids })
return
end
if only_blank_fields
tags_needing_translation = restaurant_tags.select do |tag|
target_languages.any? { |lang| tag.send("title_#{lang}").blank? }
end
if tags_needing_translation.empty?
update_job_status(redis_key, 'completed', 0, 0, 100, 'No tags need translation - all fields already filled!')
BUSINESS_LOGGER.info('All tags already have translations for selected languages', {
job_id: job_id,
total_tags: restaurant_tags.count,
target_languages: target_languages,
})
return
end
restaurant_tags = tags_needing_translation
BUSINESS_LOGGER.info(
'Filtered tags to only those needing translation',
{
job_id: job_id,
original_count: RestaurantTag.where(id: tag_ids).count,
filtered_count: restaurant_tags.size,
target_languages: target_languages,
},
)
end
total_count = restaurant_tags.count
processed_count = 0
total_empty_fields = 0
if only_blank_fields
restaurant_tags.each do |tag|
target_languages.each do |lang|
total_empty_fields += 1 if tag.send("title_#{lang}").blank?
end
end
end
total_for_progress = only_blank_fields ? total_empty_fields : total_count
processed_for_progress = 0
update_job_status(redis_key, 'processing', total_for_progress, processed_for_progress, 0,
'Starting translation...')
BUSINESS_LOGGER.info(
'Starting bulk AI translation for restaurant tags',
{
job_id: job_id,
tag_count: total_count,
total_empty_fields: total_empty_fields,
total_for_progress: total_for_progress,
target_languages: target_languages,
only_blank_fields: only_blank_fields,
},
)
failed_batches = []
restaurant_tags.in_groups_of(10, false) do |tag_batch|
batch_empty_fields_count = 0
if only_blank_fields
tag_batch.each do |tag|
target_languages.each do |lang|
batch_empty_fields_count += 1 if tag.send("title_#{lang}").blank?
end
end
end
success = process_batch(tag_batch, target_languages, only_blank_fields)
failed_batches << tag_batch unless success
processed_count += tag_batch.size
processed_for_progress += only_blank_fields ? batch_empty_fields_count : tag_batch.size
progress = ((processed_for_progress.to_f / total_for_progress) * 100).round
update_job_status(
redis_key,
'processing',
total_for_progress,
processed_for_progress,
progress,
"Processing #{processed_for_progress} of #{total_for_progress} #{only_blank_fields ? 'empty fields' : 'tags'}...",
)
sleep(2) if tag_batch.size == 10
end
if failed_batches.any?
BUSINESS_LOGGER.info(
'Retrying failed batches',
{
job_id: job_id,
failed_batch_count: failed_batches.size,
failed_tag_ids: failed_batches.flatten.map(&:id),
},
)
sleep(3)
failed_batches.each do |tag_batch|
success = process_batch(tag_batch, target_languages, only_blank_fields)
unless success
BUSINESS_LOGGER.error(
'Batch failed again after retry',
{
job_id: job_id,
tag_ids: tag_batch.map(&:id),
},
)
end
sleep(3) end
end
update_job_status(
redis_key,
'completed',
total_for_progress,
processed_for_progress,
100,
"Translation completed! Processed #{processed_for_progress} #{only_blank_fields ? 'empty fields' : 'tags'} successfully!",
)
BUSINESS_LOGGER.info(
'Completed bulk AI translation for restaurant tags',
{
job_id: job_id,
tag_count: total_count,
total_empty_fields: total_empty_fields,
processed: processed_count,
processed_for_progress: processed_for_progress,
target_languages: target_languages,
only_blank_fields: only_blank_fields,
},
)
rescue StandardError => e
update_job_status(redis_key, 'failed', 0, 0, 0, "Error: #{e.message}")
APMErrorHandler.report(e, context: {
job_id: job_id,
tag_ids: tag_ids,
target_languages: target_languages,
only_blank_fields: only_blank_fields,
})
raise
end
|