Class: PgInsights::QueryAnalysisJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/pg_insights/query_analysis_job.rb

Instance Method Summary collapse

Instance Method Details

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



12
13
14
15
16
17
18
19
20
21
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
# File 'app/jobs/pg_insights/query_analysis_job.rb', line 12

def perform(execution_id, options = {})
  execution = QueryExecution.find(execution_id)

  # Skip if already completed or failed
  return execution if execution.completed? || execution.failed?

  Rails.logger.info "Starting query analysis for execution #{execution_id}"

  begin
    execution.mark_as_running!

    results = case execution.execution_type
    when "execute"
               execute_query(execution.sql_text, options)
    when "analyze"
               analyze_query(execution.sql_text, options)
    when "both"
               execute_and_analyze_query(execution.sql_text, options)
    else
               raise ArgumentError, "Invalid execution_type: #{execution.execution_type}"
    end

    execution.mark_as_completed!(results)
    Rails.logger.info "Completed query analysis for execution #{execution_id}"

    execution

  rescue => e
    Rails.logger.error "Query analysis job failed for execution #{execution_id}: #{e.message}"
    Rails.logger.error e.backtrace.join("\n") if Rails.env.development?

    execution.mark_as_failed!(e.message, e.backtrace&.first&.truncate(500))
    raise # Re-raise for job retry mechanism
  end
end