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