Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #31545: Add status of Rails cache to Ping API #9793

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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