|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Jobs |
| 4 | + class AutomaticTranslationBackfill < ::Jobs::Scheduled |
| 5 | + every 5.minutes |
| 6 | + |
| 7 | + BACKFILL_LOCK_KEY = "discourse_translator_backfill_lock" |
| 8 | + |
| 9 | + def execute(args = nil) |
| 10 | + return unless SiteSetting.translator_enabled |
| 11 | + return unless should_backfill? |
| 12 | + return unless secure_backfill_lock |
| 13 | + |
| 14 | + begin |
| 15 | + process_batch |
| 16 | + ensure |
| 17 | + Discourse.redis.del(BACKFILL_LOCK_KEY) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = backfill_locales) |
| 22 | + m = model.name.downcase |
| 23 | + DB.query_single(<<~SQL, target_locales: target_locales, limit: limit) |
| 24 | + SELECT m.id |
| 25 | + FROM #{m}s m |
| 26 | + LEFT JOIN discourse_translator_#{m}_locales dl ON dl.#{m}_id = m.id |
| 27 | + LEFT JOIN LATERAL ( |
| 28 | + SELECT array_agg(DISTINCT locale)::text[] as locales |
| 29 | + FROM discourse_translator_#{m}_translations dt |
| 30 | + WHERE dt.#{m}_id = m.id |
| 31 | + ) translations ON true |
| 32 | + WHERE NOT ( |
| 33 | + ARRAY[:target_locales]::text[] <@ |
| 34 | + (COALESCE( |
| 35 | + array_cat( |
| 36 | + ARRAY[COALESCE(dl.detected_locale, '')]::text[], |
| 37 | + COALESCE(translations.locales, ARRAY[]::text[]) |
| 38 | + ), |
| 39 | + ARRAY[]::text[] |
| 40 | + )) |
| 41 | + ) |
| 42 | + ORDER BY m.id DESC |
| 43 | + LIMIT :limit |
| 44 | + SQL |
| 45 | + end |
| 46 | + |
| 47 | + private |
| 48 | + |
| 49 | + def should_backfill? |
| 50 | + return false if SiteSetting.automatic_translation_target_languages.blank? |
| 51 | + return false if SiteSetting.automatic_translation_backfill_maximum_translations_per_hour == 0 |
| 52 | + true |
| 53 | + end |
| 54 | + |
| 55 | + def secure_backfill_lock |
| 56 | + Discourse.redis.set(BACKFILL_LOCK_KEY, "1", ex: 5.minutes.to_i, nx: true) |
| 57 | + end |
| 58 | + |
| 59 | + def translations_per_run |
| 60 | + [ |
| 61 | + (SiteSetting.automatic_translation_backfill_maximum_translations_per_hour / 12) / |
| 62 | + backfill_locales.size, |
| 63 | + 1, |
| 64 | + ].max |
| 65 | + end |
| 66 | + |
| 67 | + def backfill_locales |
| 68 | + @backfill_locales ||= SiteSetting.automatic_translation_target_languages.split("|") |
| 69 | + end |
| 70 | + |
| 71 | + def translator |
| 72 | + @translator_klass ||= "DiscourseTranslator::#{SiteSetting.translator}".constantize |
| 73 | + end |
| 74 | + |
| 75 | + def translate_records(type, record_ids) |
| 76 | + record_ids.each do |id| |
| 77 | + record = type.find(id) |
| 78 | + backfill_locales.each do |target_locale| |
| 79 | + begin |
| 80 | + translator.translate(record, target_locale.to_sym) |
| 81 | + rescue => e |
| 82 | + # continue with other locales even if one fails |
| 83 | + Rails.logger.warn( |
| 84 | + "Failed to machine-translate #{type.name}##{id} to #{target_locale}: #{e.message}\n#{e.backtrace.join("\n")}", |
| 85 | + ) |
| 86 | + next |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def process_batch |
| 93 | + models_translated = [Post, Topic].size |
| 94 | + translations_per_model = [translations_per_run / models_translated, 1].max |
| 95 | + topic_ids = fetch_untranslated_model_ids(Topic, translations_per_model) |
| 96 | + translations_per_model = translations_per_run - topic_ids.size |
| 97 | + post_ids = fetch_untranslated_model_ids(Post, translations_per_model) |
| 98 | + return if topic_ids.empty? && post_ids.empty? |
| 99 | + |
| 100 | + translate_records(Topic, topic_ids) |
| 101 | + translate_records(Post, post_ids) |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments