Skip to content

Commit 3501883

Browse files
committed
Updates
1 parent f6a46ec commit 3501883

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

app/jobs/scheduled/automatic_translation_backfill.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def process_batch
9393
models_translated = [Post, Topic].size
9494
translations_per_model = [translations_per_run / models_translated, 1].max
9595
topic_ids = fetch_untranslated_model_ids(Topic, translations_per_model)
96-
translations_per_model = translations_per_run if topic_ids.empty?
96+
translations_per_model = translations_per_run - topic_ids.size
9797
post_ids = fetch_untranslated_model_ids(Post, translations_per_model)
9898
return if topic_ids.empty? && post_ids.empty?
9999

plugin.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ module ::DiscourseTranslator
4040

4141
on(:post_process_cooked) do |_, post|
4242
if SiteSetting.automatic_translation_target_languages.present?
43-
Jobs.enqueue(:translate_translatable, type: Post, translatable_id: post.id)
43+
Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id)
4444
end
4545
end
4646

4747
on(:topic_created) do |topic|
4848
if SiteSetting.automatic_translation_target_languages.present?
49-
Jobs.enqueue(:translate_translatable, type: Topic, translatable_id: topic.id)
49+
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
5050
end
5151
end
5252

5353
on(:topic_edited) do |topic|
5454
if SiteSetting.automatic_translation_target_languages.present?
55-
Jobs.enqueue(:translate_translatable, type: Topic, translatable_id: topic.id)
55+
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
5656
end
5757
end
5858

spec/jobs/automatic_translation_backfill_spec.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def expect_google_translate(text)
7474
describe "with two locales ['de', 'es']" do
7575
before do
7676
SiteSetting.automatic_translation_target_languages = "de|es"
77-
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 10
77+
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
7878
expect_google_check_language
7979
end
8080

@@ -104,6 +104,31 @@ def expect_google_translate(text)
104104
expect(post.translations.pluck(:locale, :translation)).to eq([%w[de hallo]])
105105
end
106106
end
107+
108+
describe "with just one locale ['de']" do
109+
before do
110+
SiteSetting.automatic_translation_target_languages = "de"
111+
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 5 * 12
112+
expect_google_check_language
113+
end
114+
115+
it "backfills all (1) topics and (4) posts as it is within the maximum per job run" do
116+
topic = Fabricate(:topic)
117+
posts = Fabricate.times(4, :post, topic: topic)
118+
119+
topic.set_detected_locale("es")
120+
posts.each { |p| p.set_detected_locale("es") }
121+
122+
expect_google_translate("hallo").times(5)
123+
124+
described_class.new.execute
125+
126+
expect(topic.translations.pluck(:locale, :translation)).to eq([%w[de hallo]])
127+
expect(posts.map { |p| p.translations.pluck(:locale, :translation).flatten }).to eq(
128+
[%w[de hallo]] * 4,
129+
)
130+
end
131+
end
107132
end
108133

109134
describe ".fetch_untranslated_model_ids" do

spec/jobs/detect_posts_language_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@
4242
end
4343

4444
it "processes a maximum of MAX_QUEUE_SIZE posts per run" do
45-
large_number = 2000
46-
large_number.times { |i| Discourse.redis.sadd?(redis_key, i + 1) }
45+
queue_size = 4
46+
described_class.const_set(:MAX_QUEUE_SIZE, queue_size)
47+
48+
existing_posts = Discourse.redis.scard(redis_key)
49+
posts = 5
50+
posts.times { |i| Discourse.redis.sadd?(redis_key, i + 1) }
51+
4752
described_class.new.execute({})
4853

4954
remaining = Discourse.redis.scard(redis_key)
50-
expect(remaining).to eq(large_number - Jobs::DetectPostsLanguage::MAX_QUEUE_SIZE)
55+
expect(remaining).to eq((existing_posts + posts) - queue_size)
5156
end
5257

5358
it "handles an empty Redis queue gracefully" do

0 commit comments

Comments
 (0)