This commit is contained in:
Rafael Silva 2025-05-09 15:06:27 -03:00
parent 44391e276d
commit 210d6c4b75

View File

@ -2,7 +2,7 @@
module Jobs
class GenerateInferredConcepts < ::Jobs::Base
sidekiq_options queue: 'low'
sidekiq_options queue: "low"
# Process items to generate new concepts
#
@ -13,50 +13,52 @@ module Jobs
# @option args [Boolean] :match_only (false) Only match against existing concepts without generating new ones
def execute(args = {})
return if args[:item_ids].blank? || args[:item_type].blank?
unless ['topics', 'posts'].include?(args[:item_type])
unless %w[topics posts].include?(args[:item_type])
Rails.logger.error("Invalid item_type for GenerateInferredConcepts: #{args[:item_type]}")
return
end
# Process items in smaller batches to avoid memory issues
batch_size = args[:batch_size] || 100
# Get the list of item IDs
item_ids = args[:item_ids]
match_only = args[:match_only] || false
# Process items in batches
item_ids.each_slice(batch_size) do |batch_item_ids|
process_batch(batch_item_ids, args[:item_type], match_only)
end
end
private
def process_batch(item_ids, item_type, match_only)
klass = item_type.singularize.classify.constantize
items = klass.where(id: item_ids)
items.each do |item|
begin
process_item(item, item_type, match_only)
rescue => e
Rails.logger.error("Error generating concepts from #{item_type.singularize} #{item.id}: #{e.message}\n#{e.backtrace.join("\n")}")
Rails.logger.error(
"Error generating concepts from #{item_type.singularize} #{item.id}: #{e.message}\n#{e.backtrace.join("\n")}",
)
end
end
end
def process_item(item, item_type, match_only)
# Use the Manager method that handles both identifying and creating concepts
if match_only
if item_type == 'topics'
if item_type == "topics"
DiscourseAi::InferredConcepts::Manager.match_topic_to_concepts(item)
else # posts
DiscourseAi::InferredConcepts::Manager.match_post_to_concepts(item)
end
else
if item_type == 'topics'
if item_type == "topics"
DiscourseAi::InferredConcepts::Manager.analyze_topic(item)
else # posts
DiscourseAi::InferredConcepts::Manager.analyze_post(item)
@ -64,4 +66,4 @@ module Jobs
end
end
end
end
end