mirror of
https://ghfast.top/https://github.com/discourse/discourse-translator.git
synced 2026-07-17 11:56:50 +08:00
The following changes allow the saving of translation metadata to be in the single base class instead of sprinkled in all the subclasses. This is to prepare for the move from custom fields to proper tables. This PR does two things: 1. introduce a `detect!` (and also `translate!`) in translator subclasses (Amazon, Google, Microsoft, etc) which will set the value from the API all the time. The base class invokes `detect!`.4c5d8e74de/app/services/discourse_translator/google.rb (L77-L85)2. update `detect` to return the stored value or invoke the `!` variant to get the value if it does not exist.4c5d8e74de/app/services/discourse_translator/base.rb (L51-L57)There is already test coverage for this refactor.
36 lines
989 B
Ruby
36 lines
989 B
Ruby
# frozen_string_literal: true
|
|
|
|
module ::Jobs
|
|
class DetectPostsLanguage < ::Jobs::Scheduled
|
|
sidekiq_options retry: false
|
|
every 5.minutes
|
|
|
|
BATCH_SIZE = 100
|
|
MAX_QUEUE_SIZE = 1000
|
|
|
|
def execute(args)
|
|
return unless SiteSetting.translator_enabled
|
|
|
|
post_ids = Discourse.redis.spop(DiscourseTranslator::LANG_DETECT_NEEDED, MAX_QUEUE_SIZE)
|
|
return if post_ids.blank?
|
|
|
|
post_ids.each_slice(BATCH_SIZE) { |batch| process_batch(batch) }
|
|
end
|
|
|
|
private
|
|
|
|
def process_batch(post_ids)
|
|
posts = Post.where(id: post_ids).to_a
|
|
posts.each do |post|
|
|
DistributedMutex.synchronize("detect_translation_#{post.id}") do
|
|
begin
|
|
translator = "DiscourseTranslator::#{SiteSetting.translator}".constantize
|
|
translator.detect(post)
|
|
rescue ::DiscourseTranslator::ProblemCheckedTranslationError
|
|
# problem-checked translation errors gracefully
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|