DEV: Allow multisite apps to boot with reading connection handler.

This commit is contained in:
Guo Xiang Tan 2020-06-10 14:03:20 +08:00
parent 7589551d68
commit 2ce829cc01
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 13 additions and 1 deletions

View File

@ -288,7 +288,7 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
rails_multisite (2.2.2)
rails_multisite (2.3.0)
activerecord (> 5.0, < 7)
railties (> 5.0, < 7)
railties (6.0.3.1)

View File

@ -17,6 +17,13 @@ if ENV["REDIS_RAILS_FAILOVER"]
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
end
RailsFailover::ActiveRecord.on_failover do
RailsMultisite::ConnectionManagement.each_connection do
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
@ -29,6 +36,11 @@ if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
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
end
module Discourse