From 325a9d0da11563e5c18a874052f560b73e4b11ba Mon Sep 17 00:00:00 2001 From: Christian Ruppert Date: Fri, 20 May 2022 13:39:09 +0200 Subject: [PATCH] Serve from cache when response.code != 200 Example: You have a loadbalancer in between Puppet and Foreman and it'll serve a 503 when the backend(s) (actual Foreman) are down, it wouldn't serve from the cache without this new NodeRetrievalError exception So raising the exception, still logging a useful message but serving from cache makes the most sense here --- files/enc.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/files/enc.rb b/files/enc.rb index 6a963fd..94eb9e7 100644 --- a/files/enc.rb +++ b/files/enc.rb @@ -93,6 +93,7 @@ def pop end class FactUploadError < StandardError; end +class NodeRetrievalError < StandardError; end require 'etc' require 'net/http' @@ -272,7 +273,7 @@ def enc(certname) response = http.request(req) unless response.code == "200" - raise "Error retrieving node #{certname}: #{response.class}\nCheck Foreman's /var/log/foreman/production.log for more information." + raise NodeRetrievalError, "Error retrieving node #{certname}: #{response.class}\nCheck Foreman's /var/log/foreman/production.log for more information." end response.body end @@ -443,7 +444,7 @@ def watch_and_send_facts(parallel) result = enc(certname) cache(certname, result) end - rescue TimeoutError, SocketError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, FactUploadError => e + rescue TimeoutError, SocketError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, NodeRetrievalError, FactUploadError => e $stderr.puts "Serving cached ENC: #{e}" # Read from cache, we got some sort of an error. result = read_cache(certname)