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