Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal: true

module Jobs
class DetectPostsLocale < ::Jobs::Base
class PostsLocaleDetectionBackfill < ::Jobs::Scheduled
every 5.minutes
cluster_concurrency 1
sidekiq_options retry: false

BATCH_SIZE = 50

def execute(args)
return unless SiteSetting.translator_enabled
return unless SiteSetting.experimental_content_translation
return if SiteSetting.automatic_translation_backfill_rate == 0

limit = SiteSetting.automatic_translation_backfill_rate
posts =
Post
.where(locale: nil)
.where(deleted_at: nil)
.where("posts.user_id > 0")
.where.not(raw: [nil, ""])
.order(updated_at: :desc)
.limit(BATCH_SIZE)
.limit(limit)
return if posts.empty?

posts.each do |post|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

describe Jobs::DetectPostsLocale do
describe Jobs::PostsLocaleDetectionBackfill do
fab!(:post) { Fabricate(:post, locale: nil) }
subject(:job) { described_class.new }

before do
SiteSetting.translator_enabled = true
SiteSetting.experimental_content_translation = true
SiteSetting.automatic_translation_backfill_rate = 100
end

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

original_batch = described_class::BATCH_SIZE
described_class.const_set(:BATCH_SIZE, 1)
SiteSetting.automatic_translation_backfill_rate = 1

DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post_2).once
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).never
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post_3).never

job.execute({})
ensure
described_class.const_set(:BATCH_SIZE, original_batch)
end

it "skips bot posts" do
Expand Down
Loading