Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit e2d7ca0

Browse files
authored
DEV: Indicate backfill rate for translations is hourly (#1451)
* DEV: Indicate backfill rate for translations is hourly * add ai_translation_max_post_length * default value update
1 parent 238538c commit e2d7ca0

21 files changed

+209
-172
lines changed

app/jobs/regular/localize_categories.rb

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,51 @@ class LocalizeCategories < ::Jobs::Base
55
cluster_concurrency 1
66
sidekiq_options retry: false
77

8-
BATCH_SIZE = 50
9-
108
def execute(args)
9+
limit = args[:limit]
10+
raise Discourse::InvalidParameters.new(:limit) if limit.nil?
11+
return if limit <= 0
12+
1113
return if !SiteSetting.discourse_ai_enabled
1214
return if !SiteSetting.ai_translation_enabled
13-
1415
locales = SiteSetting.content_localization_supported_locales.split("|")
1516
return if locales.blank?
1617

17-
cat_id = args[:from_category_id] || Category.order(:id).first&.id
18-
last_id = nil
18+
categories = Category.where("locale IS NOT NULL")
19+
20+
if SiteSetting.ai_translation_backfill_limit_to_public_content
21+
categories = categories.where(read_restricted: false)
22+
end
1923

20-
categories =
21-
Category.where("id >= ? AND locale IS NOT NULL", cat_id).order(:id).limit(BATCH_SIZE)
24+
categories = categories.order(:id).limit(limit)
2225
return if categories.empty?
2326

24-
categories.each do |category|
25-
if SiteSetting.ai_translation_backfill_limit_to_public_content && category.read_restricted?
26-
last_id = category.id
27-
next
28-
end
27+
remaining_limit = limit
2928

30-
locales.each do |locale|
31-
localization = category.category_localizations.find_by(locale:)
32-
33-
if locale == category.locale && localization
34-
localization.destroy
35-
else
36-
next if locale == category.locale
37-
begin
38-
DiscourseAi::Translation::CategoryLocalizer.localize(category, locale)
39-
rescue FinalDestination::SSRFDetector::LookupFailedError
40-
# do nothing, there are too many sporadic lookup failures
41-
rescue => e
42-
DiscourseAi::Translation::VerboseLogger.log(
43-
"Failed to translate category #{category.id} to #{locale}: #{e.message}",
44-
)
45-
end
29+
categories.each do |category|
30+
break if remaining_limit <= 0
31+
32+
existing_locales = CategoryLocalization.where(category_id: category.id).pluck(:locale)
33+
missing_locales = locales - existing_locales - [category.locale]
34+
missing_locales.each do |locale|
35+
break if remaining_limit <= 0
36+
37+
begin
38+
DiscourseAi::Translation::CategoryLocalizer.localize(category, locale)
39+
rescue FinalDestination::SSRFDetector::LookupFailedError
40+
# do nothing, there are too many sporadic lookup failures
41+
rescue => e
42+
DiscourseAi::Translation::VerboseLogger.log(
43+
"Failed to translate category #{category.id} to #{locale}: #{e.message}",
44+
)
45+
ensure
46+
remaining_limit -= 1
4647
end
4748
end
48-
last_id = category.id
49-
end
5049

51-
if categories.size == BATCH_SIZE
52-
Jobs.enqueue_in(10.seconds, :localize_categories, from_category_id: last_id + 1)
50+
if existing_locales.include?(category.locale)
51+
CategoryLocalization.find_by(category_id: category.id, locale: category.locale).destroy
52+
end
5353
end
5454
end
5555
end

app/jobs/regular/localize_posts.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ class LocalizePosts < ::Jobs::Base
55
cluster_concurrency 1
66
sidekiq_options retry: false
77

8-
BATCH_SIZE = 50
9-
108
def execute(args)
9+
limit = args[:limit]
10+
raise Discourse::InvalidParameters.new(:limit) if limit.blank? || limit <= 0
11+
1112
return if !SiteSetting.discourse_ai_enabled
1213
return if !SiteSetting.ai_translation_enabled
1314

1415
locales = SiteSetting.content_localization_supported_locales.split("|")
1516
return if locales.blank?
1617

17-
limit = args[:limit] || BATCH_SIZE
18-
1918
locales.each do |locale|
2019
posts =
2120
Post

app/jobs/regular/localize_topics.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ class LocalizeTopics < ::Jobs::Base
55
cluster_concurrency 1
66
sidekiq_options retry: false
77

8-
BATCH_SIZE = 50
9-
108
def execute(args)
9+
limit = args[:limit]
10+
raise Discourse::InvalidParameters.new(:limit) if limit.blank? || limit <= 0
11+
1112
return if !SiteSetting.discourse_ai_enabled
1213
return if !SiteSetting.ai_translation_enabled
1314

1415
locales = SiteSetting.content_localization_supported_locales.split("|")
1516
return if locales.blank?
1617

17-
limit = args[:limit] || BATCH_SIZE
18-
1918
locales.each do |locale|
2019
topics =
2120
Topic

app/jobs/scheduled/categories_locale_detection_backfill.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ class CategoriesLocaleDetectionBackfill < ::Jobs::Scheduled
99
def execute(args)
1010
return if !SiteSetting.discourse_ai_enabled
1111
return if !SiteSetting.ai_translation_enabled
12-
return if SiteSetting.ai_translation_backfill_rate == 0
12+
limit = SiteSetting.ai_translation_backfill_hourly_rate
13+
return if limit == 0
1314

1415
categories = Category.where(locale: nil)
1516

1617
if SiteSetting.ai_translation_backfill_limit_to_public_content
1718
categories = categories.where(read_restricted: false)
1819
end
1920

20-
categories = categories.limit(SiteSetting.ai_translation_backfill_rate)
21+
categories = categories.limit(limit)
2122
return if categories.empty?
2223

2324
categories.each do |category|

app/jobs/scheduled/category_localization_backfill.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
module Jobs
44
class CategoryLocalizationBackfill < ::Jobs::Scheduled
5-
every 12.hours
5+
every 1.hour
66
cluster_concurrency 1
77

88
def execute(args)
99
return if !SiteSetting.discourse_ai_enabled
1010
return if !SiteSetting.ai_translation_enabled
1111
return if SiteSetting.content_localization_supported_locales.blank?
12+
limit = SiteSetting.ai_translation_backfill_hourly_rate
13+
return if limit == 0
1214

13-
Jobs.enqueue(:localize_categories)
15+
Jobs.enqueue(:localize_categories, limit:)
1416
end
1517
end
1618
end

app/jobs/scheduled/post_localization_backfill.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ def execute(args)
1010
return if !SiteSetting.ai_translation_enabled
1111

1212
return if SiteSetting.content_localization_supported_locales.blank?
13-
return if SiteSetting.ai_translation_backfill_rate == 0
13+
limit = SiteSetting.ai_translation_backfill_hourly_rate / (60 / 5) # this job runs in 5-minute intervals
14+
return if limit == 0
1415

15-
Jobs.enqueue(:localize_posts, limit: SiteSetting.ai_translation_backfill_rate)
16+
Jobs.enqueue(:localize_posts, limit:)
1617
end
1718
end
1819
end

app/jobs/scheduled/posts_locale_detection_backfill.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class PostsLocaleDetectionBackfill < ::Jobs::Scheduled
99
def execute(args)
1010
return if !SiteSetting.discourse_ai_enabled
1111
return if !SiteSetting.ai_translation_enabled
12-
return if SiteSetting.ai_translation_backfill_rate == 0
12+
limit = SiteSetting.ai_translation_backfill_hourly_rate / (60 / 5) # this job runs in 5-minute intervals
13+
return if limit == 0
1314

1415
posts =
1516
Post
@@ -35,7 +36,7 @@ def execute(args)
3536
)
3637
end
3738

38-
posts = posts.order(updated_at: :desc).limit(SiteSetting.ai_translation_backfill_rate)
39+
posts = posts.order(updated_at: :desc).limit(limit)
3940
return if posts.empty?
4041

4142
posts.each do |post|

app/jobs/scheduled/topic_localization_backfill.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ def execute(args)
1010
return if !SiteSetting.ai_translation_enabled
1111

1212
return if SiteSetting.content_localization_supported_locales.blank?
13-
return if SiteSetting.ai_translation_backfill_rate == 0
13+
limit = SiteSetting.ai_translation_backfill_hourly_rate / (60 / 5) # this job runs in 5-minute intervals
14+
return if limit == 0
1415

15-
Jobs.enqueue(:localize_topics, limit: SiteSetting.ai_translation_backfill_rate)
16+
Jobs.enqueue(:localize_topics, limit:)
1617
end
1718
end
1819
end

app/jobs/scheduled/topics_locale_detection_backfill.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ class TopicsLocaleDetectionBackfill < ::Jobs::Scheduled
99
def execute(args)
1010
return if !SiteSetting.discourse_ai_enabled
1111
return if !SiteSetting.ai_translation_enabled
12-
limit = SiteSetting.ai_translation_backfill_rate
13-
12+
limit = SiteSetting.ai_translation_backfill_hourly_rate / (60 / 5) # this job runs in 5-minute intervals
1413
return if limit == 0
1514

1615
topics = Topic.where(locale: nil, deleted_at: nil).where("topics.user_id > 0")

config/locales/server.en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ en:
118118
ai_discord_allowed_guilds: "Discord guilds (servers) where the bot is allowed to search"
119119
ai_bot_enable_dedicated_ux: "Allow for full screen bot interface, instead of a PM"
120120

121+
ai_translation_enabled: "Enables the AI translation feature"
122+
ai_translation_model: "The model to use for translation. This model must support translation. Personas can override this setting."
123+
ai_translation_backfill_limit_to_public_content: "When enabled, only content in public categories will be translated. When disabled, content in group PMs and private categories will also be sent for translation."
124+
ai_translation_max_post_length: "The maximum length of a post to be translated. Posts longer than this will not be translated."
125+
ai_translation_backfill_max_age_days: "The maximum age of a post and topic to be translated. Posts and topics older than this will not be translated."
126+
121127
reviewables:
122128
reasons:
123129
flagged_by_toxicity: The AI plugin flagged this after classifying it as toxic.

0 commit comments

Comments
 (0)