Class: ImageFullAnalysisWorker
Overview
Background worker for running the complete AI analysis pipeline on an image.
== Pipeline (Gemini)
- pHash Fingerprint - Perceptual hash for duplicate detection (local)
- Gemini Embedding 2 - Native multimodal embedding (image + metadata text)
- Gemini Flash Vision - Describes image content (independent, for CRM/metadata)
The embedding step no longer depends on vision analysis — Gemini Embedding 2
natively understands images without a text intermediary.
Constant Summary
collapse
- JOB_TRACKING_KEY =
"image_full_analysis_jid"
- JOB_TRACKING_TTL =
2.hours.to_i
- MODEL =
'gemini-embedding-2-preview'
- DIMENSIONS =
1536
Instance Attribute Summary
#broadcast_status_updates
Class Method Summary
collapse
Instance Method Summary
collapse
#at, #store, #total
Class Method Details
.find_running_jid(image_id) ⇒ Object
35
36
37
38
39
40
|
# File 'app/workers/image_full_analysis_worker.rb', line 35
def self.find_running_jid(image_id)
jid = Sidekiq.redis { |r| r.get("#{JOB_TRACKING_KEY}:#{image_id}") }
return nil unless jid
Sidekiq::Status.status(jid).in?(%i[queued working]) ? jid : nil
end
|
.lock_args(args) ⇒ Object
24
25
26
|
# File 'app/workers/image_full_analysis_worker.rb', line 24
def self.lock_args(args)
[args[0]]
end
|
.track_jid(image_id, jid) ⇒ Object
31
32
33
|
# File 'app/workers/image_full_analysis_worker.rb', line 31
def self.track_jid(image_id, jid)
Sidekiq.redis { |r| r.set("#{JOB_TRACKING_KEY}:#{image_id}", jid, ex: JOB_TRACKING_TTL) }
end
|
Instance Method Details
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'app/workers/image_full_analysis_worker.rb', line 49
def perform(image_id, options = {})
options = options.with_indifferent_access
@force = options[:force].to_b
total 4
at 0, 'Starting AI analysis...'
image = Image.find_by(id: image_id)
unless image
store error_message: "Image #{image_id} not found"
return log_info("Image #{image_id} not found")
end
redirect_to_path = options[:redirect_to]
redirect_to_path ||= "/en-US/images/#{image.slug}/tab_embeddings?target_id=ai_embeddings"
store redirect_to: redirect_to_path
if image.inactive?
store error_message: "Image #{image_id} is inactive"
return log_info("Image #{image_id} inactive")
end
if image.asset&.dig('file_type') == 'non-image'
store error_message: 'Non-image file type'
return log_info("Image #{image_id} is non-image")
end
analysis = analyze_needs(image)
log_info "Image #{image_id}: #{analysis}"
unless analysis[:needs_anything]
at 4, 'Already complete'
store info_message: 'All up to date'
return log_info("Image #{image_id}: Up to date")
end
at 1, 'Step 1/3: pHash...'
if analysis[:needs_phash]
begin
ImageFingerprintWorker.new.perform(image_id, { force: @force })
log_info "Image #{image_id}: pHash complete"
rescue StandardError => e
log_error "Image #{image_id}: pHash failed: #{e.message}"
end
end
at 2, 'Step 2/3: Multimodal embedding...'
if analysis[:needs_embedding]
begin
generate_multimodal_embedding(image)
log_info "Image #{image_id}: Multimodal embedding complete"
rescue StandardError => e
log_error "Image #{image_id}: Embedding failed: #{e.message}"
end
end
at 3, 'Step 3/3: Vision analysis...'
if analysis[:needs_vision]
begin
result = ImageAnalysis::VisionAnalyzer.call(image, force: @force)
if result.success?
log_info "Image #{image_id}: Vision analysis complete"
else
log_error "Image #{image_id}: Vision failed: #{result.error}"
end
rescue StandardError => e
log_error "Image #{image_id}: Vision failed: #{e.message}"
end
end
at 4, 'Complete!'
store info_message: 'AI analysis complete'
log_info "Image #{image_id}: Complete"
rescue StandardError => e
store error_message: "Error: #{e.message}"
log_error "Error for Image #{image_id}: #{e.message}"
ErrorReporting.error(e)
raise
end
|