From 50f68c328544531cee3b1b47e3aa3a94f9c0278f Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Fri, 4 Aug 2023 13:28:54 +0000 Subject: [PATCH] Fixes #31545: Add status of Rails cache to Ping API --- app/services/ping.rb | 46 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/app/services/ping.rb b/app/services/ping.rb index c7386bee8c8..d19da1a8dc5 100644 --- a/app/services/ping.rb +++ b/app/services/ping.rb @@ -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 @@ -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| @@ -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