From 2aff33cfdf15932b95223b1b7a3164b977e5f91c Mon Sep 17 00:00:00 2001 From: Stephen Gelman Date: Fri, 17 Jan 2020 21:49:38 +0000 Subject: [PATCH] Rescue Metric::HaproxyBackendsHealth current_health when haproxy isn't running Otherwise this raises an uncaught exception and causes litmus to return a 500. (Closes: #45) --- lib/litmus_paper/metric/haproxy_backends_health.rb | 7 +++++++ spec/litmus_paper/app_spec.rb | 10 ++++++++++ .../metric/haproxy_backends_health_spec.rb | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/lib/litmus_paper/metric/haproxy_backends_health.rb b/lib/litmus_paper/metric/haproxy_backends_health.rb index e0c74dc..108a29c 100644 --- a/lib/litmus_paper/metric/haproxy_backends_health.rb +++ b/lib/litmus_paper/metric/haproxy_backends_health.rb @@ -23,6 +23,13 @@ def current_health .inject(0) { |sum, server| sum + server["weight"].to_f } ((up_weight / total_weight) * @weight).to_i + + rescue Timeout::Error + LitmusPaper.logger.info("HAProxy available check timed out for #{@cluster}") + 0 + rescue => e + LitmusPaper.logger.info("HAProxy available check failed for #{@cluster} with #{e.message}") + 0 end def stats diff --git a/spec/litmus_paper/app_spec.rb b/spec/litmus_paper/app_spec.rb index ee0becf..b81bd91 100644 --- a/spec/litmus_paper/app_spec.rb +++ b/spec/litmus_paper/app_spec.rb @@ -60,6 +60,16 @@ def app last_response.body.should include('Forcing health') last_response.body.should include('88') end + + it "returns successfully if a haproxy backend socket is unreachable" do + LitmusPaper.services['test'] = LitmusPaper::Service.new('test', [], [LitmusPaper::Metric::HaproxyBackendsHealth.new(100, "/tmp/non-existant-socketfile", "haproxy")]) + + get "/" + + last_response.status.should == 200 + last_response.body.should include('test') + last_response.body.should include('0') + end end describe "POST /up" do diff --git a/spec/litmus_paper/metric/haproxy_backends_health_spec.rb b/spec/litmus_paper/metric/haproxy_backends_health_spec.rb index e91fe0e..e3bcd92 100644 --- a/spec/litmus_paper/metric/haproxy_backends_health_spec.rb +++ b/spec/litmus_paper/metric/haproxy_backends_health_spec.rb @@ -28,6 +28,11 @@ health = LitmusPaper::Metric::HaproxyBackendsHealth.new(50, "/tmp/stub-haproxy-stats", "yellow_cluster") health.current_health.should == 16 end + + it "should return a health of 0 if haproxy is not running" do + health = LitmusPaper::Metric::HaproxyBackendsHealth.new(100, "/tmp/non-existant-socketfile", "red_cluster") + health.current_health.should == 0 + end end describe "#to_s" do