Skip to content

Commit

Permalink
Fixes #31545: Add status of Rails cache to Ping API
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Nov 15, 2023
1 parent c50ba11 commit 50f68c3
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion app/services/ping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ class Ping

class << self
def ping
{
response = {
'foreman': {
database: ping_database,
},
}.merge(plugins_ping)

response[:foreman][:cache] = ping_cache if redis_cache_store?
response
end

def statuses
Expand Down Expand Up @@ -47,6 +50,39 @@ def ping_database
}
end

def ping_cache
response = {}

if redis_cache_store?
redis = Rails.cache.redis
response[:servers] = []

if redis.respond_to?(:nodes)
nodes = redis.nodes
else
nodes = [redis]
end

nodes.each do |node|
start = current_time

begin
node.ping
status = STATUS_OK
rescue Redis::CannotConnectError
status = STATUS_FAIL
ensure
response[:servers] << {
status: status,
duration_ms: duration_ms(start),
}
end
end
end

response
end

def statuses_compute_resources
results = []
ComputeResource.all.index.map do |resource|
Expand Down Expand Up @@ -106,5 +142,13 @@ def plugins_statuses
def current_time
Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
end

def cache_store
Rails.application.config.cache_store
end

def redis_cache_store?
cache_store.is_a?(Array) && cache_store.first == :redis_cache_store
end
end
end

0 comments on commit 50f68c3

Please sign in to comment.