Class: ImageVisionWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker, Workers::StatusBroadcastable
Defined in:
app/workers/image_vision_worker.rb

Overview

Background worker for analyzing images using Gemini Flash.
Delegates to ImageAnalysis::VisionAnalyzer service.

Examples:

Queue single image

ImageVisionWorker.perform_async(123)

Queue with force regeneration

ImageVisionWorker.perform_async(123, force: true)

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(image_id, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/workers/image_vision_worker.rb', line 22

def perform(image_id, options = {})
  options = options.with_indifferent_access
  force = options[:force].to_b

  total 3
  at 1, 'Loading image...'

  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

  # Set default redirect_to if not provided
  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

  at 2, 'Analyzing with Gemini Flash...'
  log_info "Analyzing image #{image_id}: #{image.title}"

  result = ImageAnalysis::VisionAnalyzer.call(image, force: force)

  if result.success?
    at 3, 'Complete!'
    store info_message: "Analysis complete (#{result.description.length} chars)"
    log_info "Image #{image_id} analyzed successfully"
  else
    store error_message: result.error
    log_error "Image #{image_id}: #{result.error}"
    raise result.error if result.error.include?('Vision error')
  end
end