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

This commit is contained in:
Guo Xiang Tan 2020-05-28 15:40:37 +08:00
parent 0854785175
commit 8c86a109bb
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
6 changed files with 41 additions and 40 deletions

View File

@ -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'

View File

@ -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

View File

@ -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,16 +169,10 @@ 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
end
c[:password] = redis_password if redis_password.present?
c[:db] = redis_db if redis_db != 0
@ -195,16 +193,10 @@ 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
end
c[:password] = message_bus_redis_password if message_bus_redis_password.present?
c[:db] = message_bus_redis_db if message_bus_redis_db != 0

View File

@ -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

View File

@ -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

View File

@ -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