Skip to content

Commit 1d6840e

Browse files
authored
DEV: Post detection on a schedule (#292)
The job was originally created as a regular job for testing. With this commit it will be a scheduled job gated by SiteSetting.experimental_content_translation.
1 parent 7c3bc25 commit 1d6840e

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

app/jobs/regular/detect_posts_locale.rb renamed to app/jobs/scheduled/posts_locale_detection_backfill.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# frozen_string_literal: true
22

33
module Jobs
4-
class DetectPostsLocale < ::Jobs::Base
4+
class PostsLocaleDetectionBackfill < ::Jobs::Scheduled
5+
every 5.minutes
56
cluster_concurrency 1
6-
sidekiq_options retry: false
7-
8-
BATCH_SIZE = 50
97

108
def execute(args)
119
return unless SiteSetting.translator_enabled
1210
return unless SiteSetting.experimental_content_translation
11+
return if SiteSetting.automatic_translation_backfill_rate == 0
1312

13+
limit = SiteSetting.automatic_translation_backfill_rate
1414
posts =
1515
Post
1616
.where(locale: nil)
1717
.where(deleted_at: nil)
1818
.where("posts.user_id > 0")
1919
.where.not(raw: [nil, ""])
2020
.order(updated_at: :desc)
21-
.limit(BATCH_SIZE)
21+
.limit(limit)
2222
return if posts.empty?
2323

2424
posts.each do |post|

spec/jobs/detect_posts_locale_spec.rb renamed to spec/jobs/posts_locale_detection_backfill_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# frozen_string_literal: true
22

3-
describe Jobs::DetectPostsLocale do
3+
describe Jobs::PostsLocaleDetectionBackfill do
44
fab!(:post) { Fabricate(:post, locale: nil) }
55
subject(:job) { described_class.new }
66

77
before do
88
SiteSetting.translator_enabled = true
99
SiteSetting.experimental_content_translation = true
10+
SiteSetting.automatic_translation_backfill_rate = 100
1011
end
1112

1213
it "does nothing when translator is disabled" do
@@ -43,16 +44,13 @@
4344
post_2.update!(updated_at: 2.day.ago)
4445
post_3.update!(updated_at: 4.day.ago)
4546

46-
original_batch = described_class::BATCH_SIZE
47-
described_class.const_set(:BATCH_SIZE, 1)
47+
SiteSetting.automatic_translation_backfill_rate = 1
4848

4949
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post_2).once
5050
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).never
5151
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post_3).never
5252

5353
job.execute({})
54-
ensure
55-
described_class.const_set(:BATCH_SIZE, original_batch)
5654
end
5755

5856
it "skips bot posts" do

0 commit comments

Comments
 (0)