2020-05-28 03:40:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-15 04:23:24 -04:00
|
|
|
if defined?(RailsFailover::Redis)
|
2020-06-15 03:56:57 -04:00
|
|
|
message_bus_keepalive_interval = nil
|
2020-06-08 00:32:14 -04:00
|
|
|
|
2020-06-15 03:56:57 -04:00
|
|
|
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
|
2020-06-02 05:24:14 -04:00
|
|
|
|
2020-06-15 03:56:57 -04:00
|
|
|
RailsFailover::Redis.on_fallback do
|
|
|
|
Discourse.clear_redis_readonly!
|
|
|
|
Discourse.request_refresh!
|
|
|
|
MessageBus.keepalive_interval = message_bus_keepalive_interval
|
2020-09-17 23:17:54 -04:00
|
|
|
|
|
|
|
ObjectSpace.each_object(DistributedCache) do |cache|
|
|
|
|
cache.clear
|
|
|
|
end
|
2020-09-18 06:44:25 -04:00
|
|
|
|
|
|
|
SiteSetting.refresh!
|
2020-06-10 02:03:20 -04:00
|
|
|
end
|
2020-06-15 23:53:52 -04:00
|
|
|
|
|
|
|
if Rails.logger.respond_to? :chained
|
|
|
|
RailsFailover::Redis.logger = Rails.logger.chained.first
|
|
|
|
end
|
2020-06-11 01:45:46 -04:00
|
|
|
end
|
2020-06-10 02:03:20 -04:00
|
|
|
|
2020-06-15 04:23:24 -04:00
|
|
|
if defined?(RailsFailover::ActiveRecord)
|
2020-06-15 23:03:47 -04:00
|
|
|
return unless Rails.configuration.active_record_rails_failover
|
|
|
|
|
2020-06-15 03:56:57 -04:00
|
|
|
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
|
2020-05-28 03:40:37 -04:00
|
|
|
end
|
|
|
|
|
2020-11-11 05:27:24 -05:00
|
|
|
RailsFailover::ActiveRecord.on_failover do |role|
|
|
|
|
if role == ActiveRecord::Base.writing_role # Multisite master
|
2020-07-08 23:46:30 -04:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
2020-07-21 01:51:37 -04:00
|
|
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
2020-07-08 23:46:30 -04:00
|
|
|
end
|
2020-11-11 05:27:24 -05:00
|
|
|
else
|
|
|
|
ActiveRecord::Base.connected_to(role: role) do
|
|
|
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test connection to the master, and trigger master failover if needed
|
|
|
|
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
|
|
|
|
ActiveRecord::Base.connection.active?
|
|
|
|
rescue PG::ConnectionBad, PG::UnableToSend, PG::ServerError
|
|
|
|
RailsFailover::ActiveRecord.verify_primary(ActiveRecord::Base.writing_role)
|
|
|
|
end
|
2020-06-15 03:56:57 -04:00
|
|
|
end
|
2020-05-28 03:40:37 -04:00
|
|
|
end
|
|
|
|
|
2020-11-11 05:27:24 -05:00
|
|
|
RailsFailover::ActiveRecord.on_fallback do |role|
|
|
|
|
if role == ActiveRecord::Base.writing_role # Multisite master
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
ActiveRecord::Base.connected_to(role: role) do
|
|
|
|
Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
|
|
|
end
|
2020-06-15 03:56:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if Rails.configuration.multisite
|
|
|
|
RailsMultisite::ConnectionManagement.default_connection_handler =
|
|
|
|
ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
|
|
|
|
end
|
2020-05-31 23:23:58 -04:00
|
|
|
end
|
|
|
|
|
2020-06-15 03:56:57 -04:00
|
|
|
RailsFailover::ActiveRecord.register_force_reading_role_callback do
|
|
|
|
Discourse.redis.exists?(
|
|
|
|
Discourse::PG_READONLY_MODE_KEY,
|
|
|
|
Discourse::PG_FORCE_READONLY_MODE_KEY
|
|
|
|
)
|
|
|
|
rescue => e
|
2020-07-21 02:53:07 -04:00
|
|
|
if !e.is_a?(Redis::CannotConnectError)
|
2020-07-08 23:14:19 -04:00
|
|
|
Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
|
2020-06-28 23:54:55 -04:00
|
|
|
end
|
2020-07-21 00:32:50 -04:00
|
|
|
|
|
|
|
false
|
2020-06-15 03:56:57 -04:00
|
|
|
end
|
2020-05-28 03:40:37 -04:00
|
|
|
end
|