Skip to content

Commit 3584da4

Browse files
committed
DEV: Add backfill job
1 parent 5e10c50 commit 3584da4

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

app/jobs/regular/translate_posts.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def execute(args)
1414
locales = SiteSetting.automatic_translation_target_languages.split("|")
1515
return if locales.blank?
1616

17+
limit = args[:limit] || BATCH_SIZE
18+
1719
locales.each do |locale|
1820
posts =
1921
Post
@@ -26,7 +28,7 @@ def execute(args)
2628
.where.not(locale: nil)
2729
.where.not(locale: locale)
2830
.where("pl.id IS NULL")
29-
.limit(BATCH_SIZE)
31+
.limit(limit)
3032

3133
next if posts.empty?
3234

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module Jobs
4+
class PostTranslationBackfill < ::Jobs::Scheduled
5+
every 5.minutes
6+
cluster_concurrency 1
7+
8+
def execute(args)
9+
return unless SiteSetting.translator_enabled
10+
return unless SiteSetting.experimental_content_translation
11+
12+
return if SiteSetting.automatic_translation_target_languages.blank?
13+
return if SiteSetting.automatic_translation_backfill_rate == 0
14+
15+
Jobs.enqueue(:translate_posts, limit: SiteSetting.automatic_translation_backfill_rate)
16+
end
17+
end
18+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
describe Jobs::PostTranslationBackfill do
4+
before do
5+
SiteSetting.automatic_translation_backfill_rate = 100
6+
SiteSetting.automatic_translation_target_languages = "en"
7+
end
8+
9+
it "does not enqueue post translation when translator disabled" do
10+
SiteSetting.translator_enabled = false
11+
12+
described_class.new.execute({})
13+
14+
expect_not_enqueued_with(job: :translate_posts)
15+
end
16+
17+
it "does not enqueue post translation when experimental translation disabled" do
18+
SiteSetting.translator_enabled = true
19+
SiteSetting.experimental_content_translation = false
20+
21+
described_class.new.execute({})
22+
23+
expect_not_enqueued_with(job: :translate_posts)
24+
end
25+
26+
it "does not enqueue psot translation if backfill languages are not set" do
27+
SiteSetting.translator_enabled = true
28+
SiteSetting.experimental_content_translation = true
29+
SiteSetting.automatic_translation_target_languages = ""
30+
31+
described_class.new.execute({})
32+
33+
expect_not_enqueued_with(job: :translate_posts)
34+
end
35+
36+
it "does not enqueue psot translation if backfill limit is set to 0" do
37+
SiteSetting.translator_enabled = true
38+
SiteSetting.experimental_content_translation = true
39+
SiteSetting.automatic_translation_backfill_rate = 0
40+
41+
described_class.new.execute({})
42+
43+
expect_not_enqueued_with(job: :translate_posts)
44+
end
45+
46+
it "enqueues post translation with correct limit" do
47+
SiteSetting.translator_enabled = true
48+
SiteSetting.experimental_content_translation = true
49+
SiteSetting.automatic_translation_backfill_rate = 10
50+
51+
described_class.new.execute({})
52+
53+
expect_job_enqueued(job: :translate_posts, args: { limit: 10 })
54+
end
55+
end

0 commit comments

Comments
 (0)