Class: PgInsights::DatabaseSnapshotJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/pg_insights/database_snapshot_job.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.schedule_next_snapshotObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/jobs/pg_insights/database_snapshot_job.rb', line 32

def self.schedule_next_snapshot
  return false unless PgInsights.snapshots_available?
  return false unless PgInsights.background_jobs_available?

  begin
    # Schedule the next snapshot based on configured frequency
    next_run_time = Time.current + PgInsights.snapshot_frequency
    set(wait_until: next_run_time).perform_later

    Rails.logger.info "PgInsights: Next snapshot scheduled for #{next_run_time}"
    true
  rescue => e
    Rails.logger.warn "PgInsights: Failed to schedule next snapshot: #{e.message}"
    false
  end
end

.start_recurring_snapshotsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/jobs/pg_insights/database_snapshot_job.rb', line 49

def self.start_recurring_snapshots
  return false unless PgInsights.snapshots_available?
  return false unless PgInsights.background_jobs_available?

  begin
    # Start the recurring snapshot cycle
    perform_later
    Rails.logger.info "PgInsights: Recurring snapshots started with frequency: #{PgInsights.snapshot_frequency}"
    true
  rescue => e
    Rails.logger.warn "PgInsights: Failed to start recurring snapshots: #{e.message}"
    false
  end
end

.validate_configurationObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/jobs/pg_insights/database_snapshot_job.rb', line 64

def self.validate_configuration
  issues = []

  issues << "Snapshots are disabled" unless PgInsights.enable_snapshots
  issues << "Snapshot collection is disabled" unless PgInsights.snapshot_collection_enabled
  issues << "Background jobs not available" unless PgInsights.background_jobs_available?
  issues << "HealthCheckResult model not available" unless defined?(HealthCheckResult)

  frequency = PgInsights.snapshot_frequency
  if frequency.respond_to?(:to_i) && frequency.to_i < 60
    issues << "Snapshot frequency too low (minimum 1 minute recommended)"
  end

  if issues.empty?
    { valid: true, message: "Database snapshots are properly configured" }
  else
    { valid: false, issues: issues }
  end
end

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/jobs/pg_insights/database_snapshot_job.rb', line 9

def perform
  unless PgInsights.snapshots_available?
    Rails.logger.warn "PgInsights: Snapshots not available, skipping snapshot collection"
    return
  end

  Rails.logger.info "PgInsights: Starting database snapshot collection"

  begin
    # Use existing infrastructure to collect and store snapshot
    HealthCheckService.execute_and_cache_check("database_snapshot")

    # Cleanup old snapshots after successful collection
    cleanup_count = HealthCheckResult.cleanup_old_snapshots

    Rails.logger.info "PgInsights: Database snapshot completed successfully"
    Rails.logger.info "PgInsights: Cleaned up #{cleanup_count} old snapshots" if cleanup_count > 0
  rescue => e
    Rails.logger.error "PgInsights: Database snapshot collection failed: #{e.message}"
    raise e
  end
end