A platform for community discussion. Free, open, simple.
DEV: Add more early exits for credit limits on translation jobs (#35578)
Keegan George7b17447c6820
diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rbindex bee6fbcffa8..baab2a98454 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb@@ -9,6 +9,13 @@ module Jobsreturn if !DiscourseAi::Translation.enabled?return if args[:post_id].blank?+ unless DiscourseAi::Translation.credits_available_for_post_detection?+ Rails.logger.info(+ "Translation skipped for post: insufficient credits. Will resume when credits reset.",+ )+ return+ end+post = Post.find_by(id: args[:post_id])return if post.blank? || post.raw.blank? || post.deleted_at.present? || post.user_id <= 0diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rbindex 7a45b374879..1940f11f8ad 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb@@ -9,6 +9,13 @@ module Jobsreturn if !DiscourseAi::Translation.enabled?return if args[:topic_id].blank?+ unless DiscourseAi::Translation.credits_available_for_topic_detection?+ Rails.logger.info(+ "Translation skipped for topic: insufficient credits. Will resume when credits reset.",+ )+ return+ end+topic = Topic.find_by(id: args[:topic_id])if topic.blank? || topic.title.blank? || topic.deleted_at.present? || topic.user_id <= 0returndiff --git a/plugins/discourse-ai/app/jobs/regular/localize_categories.rb b/plugins/discourse-ai/app/jobs/regular/localize_categories.rbindex 8136eb62e62..c404b1a7799 100644--- a/plugins/discourse-ai/app/jobs/regular/localize_categories.rb+++ b/plugins/discourse-ai/app/jobs/regular/localize_categories.rb@@ -8,6 +8,13 @@ module Jobsdef execute(args)return if !DiscourseAi::Translation.enabled?+ unless DiscourseAi::Translation.credits_available_for_category_localization?+ Rails.logger.info(+ "Translation skipped for categories: insufficient credits. Will resume when credits reset.",+ )+ return+ end+limit = args[:limit]raise Discourse::InvalidParameters.new(:limit) if limit.nil?return if limit <= 0diff --git a/plugins/discourse-ai/app/jobs/regular/localize_posts.rb b/plugins/discourse-ai/app/jobs/regular/localize_posts.rbindex 0c4ef3f9309..747b7e119a7 100644--- a/plugins/discourse-ai/app/jobs/regular/localize_posts.rb+++ b/plugins/discourse-ai/app/jobs/regular/localize_posts.rb@@ -11,6 +11,13 @@ module Jobsreturn if !DiscourseAi::Translation.backfill_enabled?+ unless DiscourseAi::Translation.credits_available_for_post_localization?+ Rails.logger.info(+ "Translation skipped for posts: insufficient credits. Will resume when credits reset.",+ )+ return+ end+locales = SiteSetting.content_localization_supported_locales.split("|")locales.each do |locale|base_locale = locale.split("_").firstdiff --git a/plugins/discourse-ai/app/jobs/regular/localize_topics.rb b/plugins/discourse-ai/app/jobs/regular/localize_topics.rbindex cad7ed15f16..47a29fc61a0 100644--- a/plugins/discourse-ai/app/jobs/regular/localize_topics.rb+++ b/plugins/discourse-ai/app/jobs/regular/localize_topics.rb@@ -11,6 +11,13 @@ module Jobsreturn if !DiscourseAi::Translation.backfill_enabled?+ unless DiscourseAi::Translation.credits_available_for_topic_localization?+ Rails.logger.info(+ "Translation skipped for topics: insufficient credits. Will resume when credits reset.",+ )+ return+ end+locales = SiteSetting.content_localization_supported_locales.split("|")locales.each do |locale|base_locale = locale.split("_").firstdiff --git a/plugins/discourse-ai/lib/translation.rb b/plugins/discourse-ai/lib/translation.rbindex ac278874e38..fb310c797b0 100644--- a/plugins/discourse-ai/lib/translation.rb+++ b/plugins/discourse-ai/lib/translation.rb@@ -33,5 +33,66 @@ module DiscourseAienabled? && SiteSetting.ai_translation_backfill_hourly_rate > 0 &&SiteSetting.ai_translation_backfill_max_age_days > 0end++ def self.llm_model_for_persona(persona_id)+ return nil if persona_id.blank?++ ai_persona = AiPersona.find_by_id_from_cache(persona_id)+ return nil if ai_persona.blank?++ persona_klass = ai_persona.class_instance+ BaseTranslator.preferred_llm_model(persona_klass)+ end++ def self.credits_available_for_persona_ids?(persona_ids)+ return true if persona_ids.blank?++ models = persona_ids.map { |persona_id| llm_model_for_persona(persona_id) }.compact.uniq++ return true if models.empty?++ models.all? { |model| LlmCreditAllocation.credits_available?(model) }+ end++ def self.credits_available_for_post_detection?+ credits_available_for_persona_ids?(+ [+ SiteSetting.ai_translation_locale_detector_persona,+ SiteSetting.ai_translation_post_raw_translator_persona,+ ],+ )+ end++ def self.credits_available_for_topic_detection?+ credits_available_for_persona_ids?(+ [+ SiteSetting.ai_translation_locale_detector_persona,+ SiteSetting.ai_translation_topic_title_translator_persona,+ SiteSetting.ai_translation_post_raw_translator_persona,+ ],+ )+ end++ def self.credits_available_for_post_localization?+ credits_available_for_persona_ids?([SiteSetting.ai_translation_post_raw_translator_persona])+ end++ def self.credits_available_for_topic_localization?+ credits_available_for_persona_ids?(+ [+ SiteSetting.ai_translation_topic_title_translator_persona,+ SiteSetting.ai_translation_post_raw_translator_persona,+ ],+ )+ end++ def self.credits_available_for_category_localization?+ credits_available_for_persona_ids?(+ [+ SiteSetting.ai_translation_short_text_translator_persona,+ SiteSetting.ai_translation_post_raw_translator_persona,+ ],+ )+ endendenddiff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rbindex a3258b0b52a..6bfa56894ed 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb@@ -36,6 +36,14 @@ describe Jobs::DetectTranslatePost dojob.execute({ post_id: post.id })end+ it "skips translation when credits are unavailable" do+ DiscourseAi::Translation.expects(:credits_available_for_post_detection?).returns(false)+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never+ DiscourseAi::Translation::PostLocalizer.expects(:localize).never++ job.execute({ post_id: post.id })+ end+it "detects locale" doallow(DiscourseAi::Translation::PostLocaleDetector).to receive(:detect_locale).with(post,diff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rbindex 33b7c60113f..cd1d387516f 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb@@ -30,6 +30,14 @@ describe Jobs::DetectTranslateTopic dojob.execute({ topic_id: topic.id })end+ it "skips translation when credits are unavailable" do+ DiscourseAi::Translation.expects(:credits_available_for_topic_detection?).returns(false)+ DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).never+ DiscourseAi::Translation::TopicLocalizer.expects(:localize).never++ job.execute({ topic_id: topic.id })+ end+it "detects locale" doallow(DiscourseAi::Translation::TopicLocaleDetector).to receive(:detect_locale).with(topic,diff --git a/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rbindex 34726801648..13a31e2fc4e 100644--- a/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rb@@ -50,6 +50,13 @@ describe Jobs::LocalizeCategories dojob.execute({ limit: 10 })end+ it "skips translation when credits are unavailable" do+ DiscourseAi::Translation.expects(:credits_available_for_category_localization?).returns(false)+ DiscourseAi::Translation::CategoryLocalizer.expects(:localize).never++ job.execute({ limit: 10 })+ end+it "does nothing when the limit is zero" doDiscourseAi::Translation::CategoryLocalizer.expects(:localize).neverdiff --git a/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rbindex 8c0a863a57a..da0d1f48920 100644--- a/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/localize_posts_spec.rb@@ -44,6 +44,13 @@ describe Jobs::LocalizePosts dojob.execute({ limit: 10 })end+ it "skips translation when credits are unavailable" do+ DiscourseAi::Translation.expects(:credits_available_for_post_localization?).returns(false)+ DiscourseAi::Translation::PostLocalizer.expects(:localize).never++ job.execute({ limit: 10 })+ end+it "does nothing when there are no posts to translate" doPost.destroy_allDiscourseAi::Translation::PostLocalizer.expects(:localize).neverdiff --git a/plugins/discourse-ai/spec/jobs/regular/localize_topics_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_topics_spec.rbindex 394854b2d7a..4c4afa3cbc1 100644--- a/plugins/discourse-ai/spec/jobs/regular/localize_topics_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/localize_topics_spec.rb@@ -44,6 +44,13 @@ describe Jobs::LocalizeTopics dojob.execute({ limit: 10 })end+ it "skips translation when credits are unavailable" do+ DiscourseAi::Translation.expects(:credits_available_for_topic_localization?).returns(false)+ DiscourseAi::Translation::TopicLocalizer.expects(:localize).never++ job.execute({ limit: 10 })+ end+it "skips topics that already have localizations" doTopic.all.each do |topic|Fabricate(:topic_localization, topic:, locale: "en")