From 3c28d94706b545981ddd589374090ccd4945e535 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 2 Feb 2017 13:47:57 +0800 Subject: [PATCH] FIX: Don't configure Redis connector if Redis slave config is not set. --- app/models/global_setting.rb | 18 ++++++++++++++---- spec/models/global_setting_spec.rb | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index a7600d48206..36fc07ab45b 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -37,29 +37,39 @@ class GlobalSetting {"production" => hash} end + # For testing purposes + def self.reset_redis_config! + @config = nil + end + def self.redis_config @config ||= begin c = {} c[:host] = redis_host if redis_host c[:port] = redis_port if redis_port - c[:slave_host] = redis_slave_host if redis_slave_host - c[:slave_port] = redis_slave_port if redis_slave_port + + if redis_slave_host && redis_slave_port + c[:slave_host] = redis_slave_host + c[:slave_port] = redis_slave_port + c[:connector] = DiscourseRedis::Connector + end + c[:password] = redis_password if redis_password.present? c[:db] = redis_db if redis_db != 0 c[:db] = 1 if Rails.env == "test" + if redis_sentinels.present? c[:sentinels] = redis_sentinels.split(",").map do |address| host,port = address.split(":") {host: host, port: port} end.to_a end - c[:connector] = DiscourseRedis::Connector + c.freeze end end - class BaseProvider def self.coerce(setting) return setting == "true" if setting == "true" || setting == "false" diff --git a/spec/models/global_setting_spec.rb b/spec/models/global_setting_spec.rb index dbe1b94c2e0..20f53b8598a 100644 --- a/spec/models/global_setting_spec.rb +++ b/spec/models/global_setting_spec.rb @@ -1,6 +1,26 @@ require 'rails_helper' require 'tempfile' +describe GlobalSetting do + describe '.redis_config' do + describe 'when slave config is not present' do + it "should not set any connector" do + expect(GlobalSetting.redis_config[:connector]).to eq(nil) + end + end + + describe 'when slave config is present' do + it "should set the right connector" do + GlobalSetting.reset_redis_config! + GlobalSetting.expects(:redis_slave_port).returns(6379).at_least_once + GlobalSetting.expects(:redis_slave_host).returns('0.0.0.0').at_least_once + + expect(GlobalSetting.redis_config[:connector]).to eq(DiscourseRedis::Connector) + end + end + end +end + describe GlobalSetting::EnvProvider do it "can detect keys from env" do ENV['DISCOURSE_BLA'] = '1' @@ -9,6 +29,7 @@ describe GlobalSetting::EnvProvider do expect(GlobalSetting::EnvProvider.new.keys).to include(:bla_2) end end + describe GlobalSetting::FileProvider do it "can parse a simple file" do f = Tempfile.new('foo') @@ -48,5 +69,4 @@ describe GlobalSetting::FileProvider do f.unlink end - end