DEV: Add ENV flag to test out `ActiveRecord::Failover`.

DEV: Add ENV flag to test out ActiveRecord::Failover.

diff --git a/Gemfile b/Gemfile
index a80f23d..9cfaff2 100644
--- a/Gemfile
+++ b/Gemfile
@@ -249,4 +249,4 @@ gem 'webpush', require: false
 gem 'colored2', require: false
 gem 'maxminddb'
 
-gem 'rails_failover', require: false
+gem 'rails_failover', require: false, git: 'https://github.com/discourse/rails_failover'
diff --git a/Gemfile.lock b/Gemfile.lock
index 967f197..ba0aba2 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,3 +1,12 @@
+GIT
+  remote: https://github.com/discourse/rails_failover
+  revision: ba904987444b96a3fb627ed0b145059ff9f1cf6c
+  specs:
+    rails_failover (0.4.0)
+      activerecord (~> 6.0)
+      listen (~> 3.2)
+      railties (~> 6.0)
+
 GEM
   remote: https://rubygems.org/
   specs:
@@ -277,8 +286,6 @@ GEM
       nokogiri (>= 1.6)
     rails-html-sanitizer (1.3.0)
       loofah (~> 2.3)
-    rails_failover (0.2.0)
-      redis (~> 4)
     rails_multisite (2.1.2)
       activerecord (> 5.0, < 7)
       railties (> 5.0, < 7)
@@ -514,7 +521,7 @@ DEPENDENCIES
   rack (= 2.2.2)
   rack-mini-profiler
   rack-protection
-  rails_failover
+  rails_failover!
   rails_multisite
   railties (= 6.0.3)
   rake
diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb
index 890f5ef..a31df60 100644
--- a/app/models/global_setting.rb
+++ b/app/models/global_setting.rb
@@ -134,7 +134,11 @@ class GlobalSetting
       end
     end
 
-    hash["adapter"] = "postgresql_fallback" if hash["replica_host"]
+    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?
@@ -165,15 +169,9 @@ class GlobalSetting
         c[:port] = redis_port if redis_port
 
         if redis_slave_host && redis_slave_port
-          if ENV["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[:slave_host] = redis_slave_host
+          c[:slave_port] = redis_slave_port
+          c[:connector] = DiscourseRedis::Connector
         end
 
         c[:password] = redis_password if redis_password.present?
@@ -195,15 +193,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["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[:slave_host] = message_bus_redis_slave_host
+          c[:slave_port] = message_bus_redis_slave_port
+          c[:connector] = DiscourseRedis::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 808f9e4..3f0f558 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -27,8 +27,8 @@ require_relative '../lib/discourse_plugin_registry'
 
 require_relative '../lib/plugin_gem'
 
-if ENV['RAILS_FAILOVER']
-  require 'rails_failover'
+if ENV['ACTIVE_RECORD_RAILS_FAILOVER']
+  require 'rails_failover/active_record'
 end
 
 # Global config
diff --git a/config/initializers/001-redis.rb b/config/initializers/001-redis.rb
index f28c59a..d11303a 100644
--- a/config/initializers/001-redis.rb
+++ b/config/initializers/001-redis.rb
@@ -4,18 +4,3 @@ if Rails.env.development? && ENV['DISCOURSE_FLUSH_REDIS']
   puts "Flushing redis (development mode)"
   Discourse.redis.flushdb
 end
-
-if ENV['RAILS_FAILOVER']
-  message_bus_keepalive_interval = MessageBus.keepalive_interval
-
-  RailsFailover::Redis.register_master_up_callback do
-    MessageBus.keepalive_interval = message_bus_keepalive_interval
-    Discourse.clear_readonly!
-    Discourse.request_refresh!
-  end
-
-  RailsFailover::Redis.register_master_down_callback do
-    # Disables MessageBus keepalive when Redis is in readonly mode
-    MessageBus.keepalive_interval = 0
-  end
-end
diff --git a/config/initializers/002-rails_failover.rb b/config/initializers/002-rails_failover.rb
new file mode 100644
index 0000000..e4bb24e
--- /dev/null
+++ b/config/initializers/002-rails_failover.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
+  RailsFailover::ActiveRecord.on_failover do
+    Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
+    Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
+  end
+
+  RailsFailover::ActiveRecord.on_fallback do
+    Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
+    Sidekiq.unpause!
+  end
+
+  RailsFailover::ActiveRecord.register_force_reading_role_callback do
+    Discourse.pg_readonly_mode?
+  end
+end

GitHub sha: 8c86a109