22
33module Jobs
44 class GenerateInferredConcepts < ::Jobs ::Base
5- sidekiq_options queue : ' low'
5+ sidekiq_options queue : " low"
66
77 # Process items to generate new concepts
88 #
@@ -13,55 +13,57 @@ class GenerateInferredConcepts < ::Jobs::Base
1313 # @option args [Boolean] :match_only (false) Only match against existing concepts without generating new ones
1414 def execute ( args = { } )
1515 return if args [ :item_ids ] . blank? || args [ :item_type ] . blank?
16-
17- unless [ ' topics' , ' posts' ] . include? ( args [ :item_type ] )
16+
17+ unless %w[ topics posts ] . include? ( args [ :item_type ] )
1818 Rails . logger . error ( "Invalid item_type for GenerateInferredConcepts: #{ args [ :item_type ] } " )
1919 return
2020 end
21-
21+
2222 # Process items in smaller batches to avoid memory issues
2323 batch_size = args [ :batch_size ] || 100
24-
24+
2525 # Get the list of item IDs
2626 item_ids = args [ :item_ids ]
2727 match_only = args [ :match_only ] || false
28-
28+
2929 # Process items in batches
3030 item_ids . each_slice ( batch_size ) do |batch_item_ids |
3131 process_batch ( batch_item_ids , args [ :item_type ] , match_only )
3232 end
3333 end
34-
34+
3535 private
36-
36+
3737 def process_batch ( item_ids , item_type , match_only )
3838 klass = item_type . singularize . classify . constantize
3939 items = klass . where ( id : item_ids )
40-
40+
4141 items . each do |item |
4242 begin
4343 process_item ( item , item_type , match_only )
4444 rescue => e
45- Rails . logger . error ( "Error generating concepts from #{ item_type . singularize } #{ item . id } : #{ e . message } \n #{ e . backtrace . join ( "\n " ) } " )
45+ Rails . logger . error (
46+ "Error generating concepts from #{ item_type . singularize } #{ item . id } : #{ e . message } \n #{ e . backtrace . join ( "\n " ) } " ,
47+ )
4648 end
4749 end
4850 end
49-
51+
5052 def process_item ( item , item_type , match_only )
5153 # Use the Manager method that handles both identifying and creating concepts
5254 if match_only
53- if item_type == ' topics'
55+ if item_type == " topics"
5456 DiscourseAi ::InferredConcepts ::Manager . match_topic_to_concepts ( item )
5557 else # posts
5658 DiscourseAi ::InferredConcepts ::Manager . match_post_to_concepts ( item )
5759 end
5860 else
59- if item_type == ' topics'
61+ if item_type == " topics"
6062 DiscourseAi ::InferredConcepts ::Manager . analyze_topic ( item )
6163 else # posts
6264 DiscourseAi ::InferredConcepts ::Manager . analyze_post ( item )
6365 end
6466 end
6567 end
6668 end
67- end
69+ end
0 commit comments