Class: Vert::Health::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/vert/health/checker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChecker

Returns a new instance of Checker.



8
9
10
11
# File 'lib/vert/health/checker.rb', line 8

def initialize
  @checks = {}
  setup_default_checks
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



6
7
8
# File 'lib/vert/health/checker.rb', line 6

def checks
  @checks
end

Class Method Details

.checkerObject



94
95
96
# File 'lib/vert/health/checker.rb', line 94

def checker
  @checker ||= new
end

Instance Method Details

#add_check(name, &block) ⇒ Object Also known as: register



13
14
15
# File 'lib/vert/health/checker.rb', line 13

def add_check(name, &block)
  @checks[name] = block
end

#check_allObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vert/health/checker.rb', line 23

def check_all
  results = {}
  overall_healthy = true

  @checks.each do |name, check|
    result = check.call
    results[name] = result
    overall_healthy = false unless result[:status] == "ok"
  rescue StandardError => e
    results[name] = { status: "error", message: e.message }
    overall_healthy = false
  end

  {
    status: overall_healthy ? "healthy" : "unhealthy",
    timestamp: Time.current.iso8601,
    checks: results
  }
end

#check_databaseObject



56
57
58
59
60
61
# File 'lib/vert/health/checker.rb', line 56

def check_database
  ActiveRecord::Base.connection.execute("SELECT 1")
  { status: "ok" }
rescue StandardError => e
  { status: "error", message: e.message }
end

#check_rabbitmqObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/vert/health/checker.rb', line 72

def check_rabbitmq
  return { status: "skipped", message: "RabbitMQ check disabled" } unless Vert.config.health_check_rabbitmq

  connection = Bunny.new(Vert.config.rabbitmq_url, connection_timeout: 5)
  connection.start
  connection.close
  { status: "ok" }
rescue StandardError => e
  { status: "error", message: e.message }
end

#check_redisObject



63
64
65
66
67
68
69
70
# File 'lib/vert/health/checker.rb', line 63

def check_redis
  return { status: "skipped", message: "Redis check disabled" } unless Vert.config.health_check_redis
  return { status: "skipped", message: "Redis not configured" } unless redis_available?

  Redis.current.ping == "PONG" ? { status: "ok" } : { status: "error", message: "Ping failed" }
rescue StandardError => e
  { status: "error", message: e.message }
end

#check_sidekiqObject



83
84
85
86
87
88
89
90
91
# File 'lib/vert/health/checker.rb', line 83

def check_sidekiq
  return { status: "skipped", message: "Sidekiq check disabled" } unless Vert.config.health_check_sidekiq
  return { status: "skipped", message: "Sidekiq not configured" } unless sidekiq_available?

  stats = Sidekiq::Stats.new
  { status: "ok", processed: stats.processed, failed: stats.failed, queues: stats.queues }
rescue StandardError => e
  { status: "error", message: e.message }
end

#livenessObject



43
44
45
# File 'lib/vert/health/checker.rb', line 43

def liveness
  { status: "ok", timestamp: Time.current.iso8601 }
end

#readinessObject



47
48
49
50
51
52
53
54
# File 'lib/vert/health/checker.rb', line 47

def readiness
  db_ok = check_database[:status] == "ok"
  {
    status: db_ok ? "ready" : "not_ready",
    timestamp: Time.current.iso8601,
    checks: { database: check_database }
  }
end

#remove_check(name) ⇒ Object



19
20
21
# File 'lib/vert/health/checker.rb', line 19

def remove_check(name)
  @checks.delete(name)
end