diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index 86dcb6de3c8..4bb3cd156d5 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -81,7 +81,10 @@ class DiscourseRedis client = ::Redis::Client.new(options) client.call([:role])[0] @options - rescue Redis::ConnectionError, Redis::CannotConnectError => ex + rescue Redis::ConnectionError, Redis::CannotConnectError, RuntimeError => ex + # A consul service name may be deregistered for a redis container setup + raise ex if ex.class == RuntimeError && ex.message != "Name or service not known" + return @slave_options if !@fallback_handler.master @fallback_handler.master = false raise ex diff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rb index 77d9bc6e569..c2d05c85dbe 100644 --- a/spec/components/discourse_redis_spec.rb +++ b/spec/components/discourse_redis_spec.rb @@ -53,6 +53,28 @@ describe DiscourseRedis do fallback_handler.master = true end end + + it "should return the slave config when master's hostname cannot be resolved" do + begin + error = RuntimeError.new('Name or service not known') + + Redis::Client.any_instance.expects(:call).raises(error).twice + expect { connector.resolve }.to raise_error(error) + + config = connector.resolve + + expect(config[:host]).to eq(slave_host) + expect(config[:port]).to eq(slave_port) + ensure + fallback_handler.master = true + end + end + + it "should raise the right error" do + error = RuntimeError.new('test error') + Redis::Client.any_instance.expects(:call).raises(error).twice + 2.times { expect { connector.resolve }.to raise_error(error) } + end end describe DiscourseRedis::FallbackHandler do