2
2
3
3
module Jobs
4
4
class GenerateInferredConcepts < ::Jobs ::Base
5
- sidekiq_options queue : ' low'
5
+ sidekiq_options queue : " low"
6
6
7
7
# Process items to generate new concepts
8
8
#
@@ -13,55 +13,57 @@ class GenerateInferredConcepts < ::Jobs::Base
13
13
# @option args [Boolean] :match_only (false) Only match against existing concepts without generating new ones
14
14
def execute ( args = { } )
15
15
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 ] )
18
18
Rails . logger . error ( "Invalid item_type for GenerateInferredConcepts: #{ args [ :item_type ] } " )
19
19
return
20
20
end
21
-
21
+
22
22
# Process items in smaller batches to avoid memory issues
23
23
batch_size = args [ :batch_size ] || 100
24
-
24
+
25
25
# Get the list of item IDs
26
26
item_ids = args [ :item_ids ]
27
27
match_only = args [ :match_only ] || false
28
-
28
+
29
29
# Process items in batches
30
30
item_ids . each_slice ( batch_size ) do |batch_item_ids |
31
31
process_batch ( batch_item_ids , args [ :item_type ] , match_only )
32
32
end
33
33
end
34
-
34
+
35
35
private
36
-
36
+
37
37
def process_batch ( item_ids , item_type , match_only )
38
38
klass = item_type . singularize . classify . constantize
39
39
items = klass . where ( id : item_ids )
40
-
40
+
41
41
items . each do |item |
42
42
begin
43
43
process_item ( item , item_type , match_only )
44
44
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
+ )
46
48
end
47
49
end
48
50
end
49
-
51
+
50
52
def process_item ( item , item_type , match_only )
51
53
# Use the Manager method that handles both identifying and creating concepts
52
54
if match_only
53
- if item_type == ' topics'
55
+ if item_type == " topics"
54
56
DiscourseAi ::InferredConcepts ::Manager . match_topic_to_concepts ( item )
55
57
else # posts
56
58
DiscourseAi ::InferredConcepts ::Manager . match_post_to_concepts ( item )
57
59
end
58
60
else
59
- if item_type == ' topics'
61
+ if item_type == " topics"
60
62
DiscourseAi ::InferredConcepts ::Manager . analyze_topic ( item )
61
63
else # posts
62
64
DiscourseAi ::InferredConcepts ::Manager . analyze_post ( item )
63
65
end
64
66
end
65
67
end
66
68
end
67
- end
69
+ end
0 commit comments