DEV: Use rails_failover gem for ActiveRecord and Redis failover handling

DEV: Use rails_failover gem for ActiveRecord and Redis failover handling

diff --git a/Gemfile b/Gemfile
index 8e4acf6..4d809fd 100644
--- a/Gemfile
+++ b/Gemfile
@@ -250,4 +250,4 @@ gem 'webpush', require: false
 gem 'colored2', require: false
 gem 'maxminddb'
 
-gem 'rails_failover', require: false, git: 'https://github.com/discourse/rails_failover'
+gem 'rails_failover', require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index b181c5e..d838afb 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,11 +1,3 @@
-GIT
-  remote: https://github.com/discourse/rails_failover
-  revision: 66602aa73785851b81c506f0023d3c2a2e40de0a
-  specs:
-    rails_failover (0.4.0)
-      activerecord (~> 6.0)
-      railties (~> 6.0)
-
 GEM
   remote: https://rubygems.org/
   specs:
@@ -288,6 +280,9 @@ GEM
       nokogiri (>= 1.6)
     rails-html-sanitizer (1.3.0)
       loofah (~> 2.3)
+    rails_failover (0.5.0)
+      activerecord (~> 6.0)
+      railties (~> 6.0)
     rails_multisite (2.3.0)
       activerecord (> 5.0, < 7)
       railties (> 5.0, < 7)
@@ -526,7 +521,7 @@ DEPENDENCIES
   rack (= 2.2.2)
   rack-mini-profiler
   rack-protection
-  rails_failover!
+  rails_failover
   rails_multisite
   railties (= 6.0.3.1)
   rake
diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb
index 1d8df22..dc660fc 100644
--- a/app/models/global_setting.rb
+++ b/app/models/global_setting.rb
@@ -134,12 +134,6 @@ class GlobalSetting
       end
     end
 
-    if hash["replica_host"]
-      if !ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
-        hash["adapter"] = "postgresql_fallback"
-      end
-    end
-
     hostnames = [ hostname ]
     hostnames << backup_hostname if backup_hostname.present?
 
@@ -170,15 +164,9 @@ class GlobalSetting
         c[:port] = redis_port if redis_port
 
         if redis_slave_host && redis_slave_port
-          if ENV["REDIS_RAILS_FAILOVER"]
-            c[:replica_host] = redis_slave_host
-            c[:replica_port] = redis_slave_port
-            c[:connector] = RailsFailover::Redis::Connector
-          else
-            c[:slave_host] = redis_slave_host
-            c[:slave_port] = redis_slave_port
-            c[:connector] = DiscourseRedis::Connector
-          end
+          c[:replica_host] = redis_slave_host
+          c[:replica_port] = redis_slave_port
+          c[:connector] = RailsFailover::Redis::Connector
         end
 
         c[:password] = redis_password if redis_password.present?
@@ -200,15 +188,9 @@ class GlobalSetting
         c[:port] = message_bus_redis_port if message_bus_redis_port
 
         if message_bus_redis_slave_host && message_bus_redis_slave_port
-          if ENV["REDIS_RAILS_FAILOVER"]
-            c[:replica_host] = message_bus_redis_slave_host
-            c[:replica_port] = message_bus_redis_slave_port
-            c[:connector] = RailsFailover::Redis::Connector
-          else
-            c[:slave_host] = message_bus_redis_slave_host
-            c[:slave_port] = message_bus_redis_slave_port
-            c[:connector] = DiscourseRedis::Connector
-          end
+          c[:replica_host] = message_bus_redis_slave_host
+          c[:replica_port] = message_bus_redis_slave_port
+          c[:connector] = RailsFailover::Redis::Connector
         end
 
         c[:password] = message_bus_redis_password if message_bus_redis_password.present?
diff --git a/config/application.rb b/config/application.rb
index a93c86c..0e17596 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -19,6 +19,8 @@ require 'action_controller/railtie'
 require 'action_view/railtie'
 require 'action_mailer/railtie'
 require 'sprockets/railtie'
+require 'rails_failover/active_record'
+require 'rails_failover/redis'
 
 # Plugin related stuff
 require_relative '../lib/plugin_initialization_guard'
@@ -27,14 +29,6 @@ require_relative '../lib/discourse_plugin_registry'
 
 require_relative '../lib/plugin_gem'
 
-if ENV['ACTIVE_RECORD_RAILS_FAILOVER']
-  require 'rails_failover/active_record'
-end
-
-if ENV['REDIS_RAILS_FAILOVER']
-  require 'rails_failover/redis'
-end
-
 # Global config
 require_relative '../app/models/global_setting'
 GlobalSetting.configure!
diff --git a/config/initializers/002-rails_failover.rb b/config/initializers/002-rails_failover.rb
index 6952bc9..1c0edc9 100644
--- a/config/initializers/002-rails_failover.rb
+++ b/config/initializers/002-rails_failover.rb
@@ -1,81 +1,51 @@
 # frozen_string_literal: true
 
-if ENV["REDIS_RAILS_FAILOVER"]
-  message_bus_keepalive_interval = nil
+message_bus_keepalive_interval = nil
 
-  RailsFailover::Redis.on_failover do
-    message_bus_keepalive_interval = MessageBus.keepalive_interval
-    MessageBus.keepalive_interval = -1 # Disable MessageBus keepalive_interval
-    Discourse.received_redis_readonly!
-  end
+RailsFailover::Redis.on_failover do
+  message_bus_keepalive_interval = MessageBus.keepalive_interval
+  MessageBus.keepalive_interval = -1 # Disable MessageBus keepalive_interval
+  Discourse.received_redis_readonly!
+end
 
-  RailsFailover::Redis.on_fallback do
-    Discourse.clear_redis_readonly!
-    Discourse.request_refresh!
-    MessageBus.keepalive_interval = message_bus_keepalive_interval
-  end
+RailsFailover::Redis.on_fallback do
+  Discourse.clear_redis_readonly!
+  Discourse.request_refresh!
+  MessageBus.keepalive_interval = message_bus_keepalive_interval
 end
 
-if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
-  if Rails.configuration.multisite
-    if ActiveRecord::Base.current_role == ActiveRecord::Base.reading_role
-      RailsMultisite::ConnectionManagement.default_connection_handler =
-        ActiveRecord::Base.connection_handlers[ActiveRecord::Base.reading_role]
-    end
+if Rails.configuration.multisite
+  if ActiveRecord::Base.current_role == ActiveRecord::Base.reading_role
+    RailsMultisite::ConnectionManagement.default_connection_handler =
+      ActiveRecord::Base.connection_handlers[ActiveRecord::Base.reading_role]
   end
+end
 
-  RailsFailover::ActiveRecord.on_failover do
-    RailsMultisite::ConnectionManagement.each_connection do
-      Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
-      Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
-    end
-  rescue => e
-    Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
-    false
+RailsFailover::ActiveRecord.on_failover do
+  RailsMultisite::ConnectionManagement.each_connection do
+    Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
+    Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
   end
+end
 
-  RailsFailover::ActiveRecord.on_fallback do
-    RailsMultisite::ConnectionManagement.each_connection do
-      Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
-      Sidekiq.unpause! if Sidekiq.paused?
-    end
-
-    if Rails.configuration.multisite
-      RailsMultisite::ConnectionManagement.default_connection_handler =
-        ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
-    end
-  rescue => e
-    Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
-    false
+RailsFailover::ActiveRecord.on_fallback do
+  RailsMultisite::ConnectionManagement.each_connection do
+    Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
+    Sidekiq.unpause! if Sidekiq.paused?
   end
 
-  module Discourse
-    PG_FORCE_READONLY_MODE_KEY ||= 'readonly_mode:postgres_force'
-
-    READONLY_KEYS.push(PG_FORCE_READONLY_MODE_KEY)
-
-    def self.enable_pg_force_readonly_mode
-      Discourse.redis.set(PG_FORCE_READONLY_MODE_KEY, 1)
-      Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
-      MessageBus.publish(readonly_channel, true)
-      true
-    end
-
-    def self.disable_pg_force_readonly_mode
-      result = Discourse.redis.del(PG_FORCE_READONLY_MODE_KEY)
-      Sidekiq.unpause!
-      MessageBus.publish(readonly_channel, false)
-      result > 0
-    end
+  if Rails.configuration.multisite
+    RailsMultisite::ConnectionManagement.default_connection_handler =

[... diff too long, it was truncated ...]

GitHub sha: 58e52c0e