@@ -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
0 commit comments