Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit d7a2af5

Browse files
authored
DEV: Prevent multiple translation per post (#1443)
We're seeing an aggressive number of translations being enqueued for a single post and locale. Historically, we trigger translation on `cooked` not `raw`, but that has changed a while back. ``` # from AiApiAuditLog, the same post is getting translated to the same locale within a few secs of each other zh_CN - 2025-06-17 13:02:31 UTC zh_CN - 2025-06-17 13:02:34 UTC zh_CN - 2025-06-17 13:02:35 UTC zh_CN - 2025-06-17 13:02:36 UTC zh_CN - 2025-06-17 13:02:38 UTC zh_CN - 2025-06-17 13:02:39 UTC zh_CN - 2025-06-17 13:02:40 UTC zh_CN - 2025-06-17 13:02:40 UTC zh_CN - 2025-06-17 13:02:43 UTC zh_CN - 2025-06-17 13:02:44 UTC ``` This PR prevents this from happening.
1 parent 6dbe19a commit d7a2af5

File tree

4 files changed

+5
-4
lines changed

4 files changed

+5
-4
lines changed

app/jobs/regular/detect_translate_post.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Jobs
44
class DetectTranslatePost < ::Jobs::Base
5+
cluster_concurrency 1
56
sidekiq_options retry: false
67

78
def execute(args)

app/jobs/regular/detect_translate_topic.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Jobs
44
class DetectTranslateTopic < ::Jobs::Base
5+
cluster_concurrency 1
56
sidekiq_options retry: false
67

78
def execute(args)

lib/translation/entry_point.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module DiscourseAi
44
module Translation
55
class EntryPoint
66
def inject_into(plugin)
7-
plugin.on(:post_process_cooked) do |_, post|
7+
plugin.on(:post_created) do |post|
88
if SiteSetting.discourse_ai_enabled && SiteSetting.ai_translation_enabled
99
Jobs.enqueue(:detect_translate_post, post_id: post.id)
1010
end

spec/lib/translation/entry_point_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
describe "upon post process cooked" do
1313
it "enqueues detect post locale and translate post job" do
14-
post = Fabricate(:post)
15-
CookedPostProcessor.new(post).post_process
16-
14+
post =
15+
PostCreator.create!(Fabricate(:user), raw: "post", title: "topic", skip_validations: true)
1716
expect_job_enqueued(job: :detect_translate_post, args: { post_id: post.id })
1817
end
1918

0 commit comments

Comments
 (0)