FIX: Skip edits if localization exists (#1422)

We will fine tune updating an outdated localization in the future. For now we are seeing that quick edits are happening and we need to prevent the job from being too trigger-happy.
This commit is contained in:
Natalie Tay 2025-06-11 11:00:22 +08:00 committed by GitHub
parent f99309d1e1
commit 35d62a659b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 16 deletions

View File

@ -20,12 +20,15 @@ module Jobs
end end
end end
begin # the user may fill locale in manually
detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post) if (detected_locale = post.locale).blank?
rescue FinalDestination::SSRFDetector::LookupFailedError begin
# this job is non-critical detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
# the backfill job will handle failures rescue FinalDestination::SSRFDetector::LookupFailedError
return # this job is non-critical
# the backfill job will handle failures
return
end
end end
locales = SiteSetting.experimental_content_localization_supported_locales.split("|") locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
@ -33,6 +36,7 @@ module Jobs
locales.each do |locale| locales.each do |locale|
next if locale == detected_locale next if locale == detected_locale
next if post.post_localizations.exists?(locale:)
begin begin
DiscourseAi::Translation::PostLocalizer.localize(post, locale) DiscourseAi::Translation::PostLocalizer.localize(post, locale)

View File

@ -18,12 +18,14 @@ module Jobs
return if topic.category&.read_restricted? return if topic.category&.read_restricted?
end end
begin if (detected_locale = topic.locale).blank?
detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic) begin
rescue FinalDestination::SSRFDetector::LookupFailedError detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)
# this job is non-critical rescue FinalDestination::SSRFDetector::LookupFailedError
# the backfill job will handle failures # this job is non-critical
return # the backfill job will handle failures
return
end
end end
locales = SiteSetting.experimental_content_localization_supported_locales.split("|") locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
@ -31,6 +33,7 @@ module Jobs
locales.each do |locale| locales.each do |locale|
next if locale == detected_locale next if locale == detected_locale
next if topic.topic_localizations.exists?(locale:)
begin begin
DiscourseAi::Translation::TopicLocalizer.localize(topic, locale) DiscourseAi::Translation::TopicLocalizer.localize(topic, locale)

View File

@ -39,6 +39,14 @@ describe Jobs::DetectTranslatePost do
job.execute({ post_id: post.id }) job.execute({ post_id: post.id })
end end
it "skips locale detection when post has a locale" do
post.update!(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once
job.execute({ post_id: post.id })
end
it "skips bot posts" do it "skips bot posts" do
post.update!(user: Discourse.system_user) post.update!(user: Discourse.system_user)
DiscourseAi::Translation::PostLocalizer.expects(:localize).never DiscourseAi::Translation::PostLocalizer.expects(:localize).never
@ -56,16 +64,23 @@ describe Jobs::DetectTranslatePost do
it "skips translating to the post's language" do it "skips translating to the post's language" do
post.update(locale: "en") post.update(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).returns("en")
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "en").never 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, "ja").once
job.execute({ post_id: post.id }) job.execute({ post_id: post.id })
end end
it "skips translating if the post is already localized" do
post.update(locale: "en")
Fabricate(:post_localization, post: post, locale: "ja")
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
job.execute({ post_id: post.id })
end
it "handles translation errors gracefully" do it "handles translation errors gracefully" do
post.update(locale: "en") post.update(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).returns("en")
DiscourseAi::Translation::PostLocalizer.expects(:localize).raises( DiscourseAi::Translation::PostLocalizer.expects(:localize).raises(
StandardError.new("API error"), StandardError.new("API error"),
) )

View File

@ -39,6 +39,14 @@ describe Jobs::DetectTranslateTopic do
job.execute({ topic_id: topic.id }) job.execute({ topic_id: topic.id })
end end
it "skips locale detection when topic has a locale" do
topic.update!(locale: "en")
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).never
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once
job.execute({ topic_id: topic.id })
end
it "skips bot topics" do it "skips bot topics" do
topic.update!(user: Discourse.system_user) topic.update!(user: Discourse.system_user)
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
@ -56,16 +64,22 @@ describe Jobs::DetectTranslateTopic do
it "skips translating to the topic's language" do it "skips translating to the topic's language" do
topic.update(locale: "en") topic.update(locale: "en")
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en")
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "en").never DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "en").never
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once
job.execute({ topic_id: topic.id }) job.execute({ topic_id: topic.id })
end end
it "skips translating if the topic is already localized" do
topic.update(locale: "en")
Fabricate(:topic_localization, topic:, locale: "ja")
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
job.execute({ topic_id: topic.id })
end
it "handles translation errors gracefully" do it "handles translation errors gracefully" do
topic.update(locale: "en") topic.update(locale: "en")
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en")
DiscourseAi::Translation::TopicLocalizer.expects(:localize).raises( DiscourseAi::Translation::TopicLocalizer.expects(:localize).raises(
StandardError.new("API error"), StandardError.new("API error"),
) )