A platform for community discussion. Free, open, simple.
FIX: Also make sure quota is applied to detection (#35838)
Natalie Tayba63bbeacf37
diff --git a/plugins/discourse-ai/app/jobs/regular/localize_posts.rb b/plugins/discourse-ai/app/jobs/regular/localize_posts.rbindex 747b7e119a7..e6ccc18b936 100644--- a/plugins/discourse-ai/app/jobs/regular/localize_posts.rb+++ b/plugins/discourse-ai/app/jobs/regular/localize_posts.rb@@ -37,6 +37,8 @@ module Jobsnext if posts.empty?posts.each do |post|+ next unless DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)+beginDiscourseAi::Translation::PostLocalizer.localize(post, locale)rescue FinalDestination::SSRFDetector::LookupFailedErrordiff --git a/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rbindex 5d5e64277be..b7db5a0a2fb 100644--- a/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb+++ b/plugins/discourse-ai/app/jobs/scheduled/posts_locale_detection_backfill.rb@@ -32,6 +32,8 @@ module Jobsposts.each do |post|begin+ next if !DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, "")+DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)rescue FinalDestination::SSRFDetector::LookupFailedError# do nothing, there are too many sporadic lookup failuresdiff --git a/plugins/discourse-ai/lib/translation/post_localizer.rb b/plugins/discourse-ai/lib/translation/post_localizer.rbindex 0fa15a30d7a..4a84d85c255 100644--- a/plugins/discourse-ai/lib/translation/post_localizer.rb+++ b/plugins/discourse-ai/lib/translation/post_localizer.rb@@ -3,6 +3,8 @@module DiscourseAimodule Translationclass PostLocalizer+ MAX_QUOTA_PER_DAY = 2+def self.localize(post, target_locale = I18n.locale)if post.blank? || target_locale.blank? ||LocaleNormalizer.is_same?(post.locale, target_locale) || post.raw.blank?@@ -38,7 +40,7 @@ module DiscourseAienddef self.has_relocalize_quota?(post, locale, skip_incr: false)- return false if get_relocalize_quota(post, locale).to_i >= 2+ return false if get_relocalize_quota(post, locale).to_i >= MAX_QUOTA_PER_DAYincr_relocalize_quota(post, locale) unless skip_incrtruediff --git a/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rbindex da0d1f48920..6b812871a81 100644--- a/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb@@ -145,6 +145,22 @@ describe Jobs::LocalizePosts doendend+ context "when relocalize quota is exhausted" do+ before { post.update(locale: "es") }++ it "skips localization for posts that have exceeded quota for a specific locale" do+ DiscourseAi::Translation::PostLocalizer::MAX_QUOTA_PER_DAY.times do+ DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, "en")+ end++ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "en").never+ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once+ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "de").once++ job.execute({ limit: 10 })+ end+ end+describe "with public content limitation" dofab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }fab!(:private_topic) { Fabricate(:topic, category: private_category) }diff --git a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rbindex fa9e44e5137..edc54e49168 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb@@ -78,6 +78,21 @@ describe Jobs::PostsLocaleDetectionBackfill doexpect { job.execute({}) }.not_to raise_errorend+ context "when relocalize quota is exhausted" do+ before do+ # max out quota+ DiscourseAi::Translation::PostLocalizer::MAX_QUOTA_PER_DAY.times do+ DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, "")+ end+ end++ it "skips locale detection for posts that have exceeded quota" do+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never++ job.execute({})+ end+ end+it "logs a summary after running" doDiscourseAi::Translation::PostLocaleDetector.stubs(:detect_locale)DiscourseAi::Translation::VerboseLogger.expects(:log).with(includes("Detected 1 post locales"))