2020-05-28 15:40:37 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-15 16:23:24 +08:00
|
|
|
if defined?(RailsFailover::Redis)
|
2020-06-15 15:56:57 +08:00
|
|
|
message_bus_keepalive_interval = nil
|
2020-06-08 12:32:14 +08:00
|
|
|
|
2020-06-15 15:56:57 +08: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 17:24:14 +08:00
|
|
|
|
2020-06-15 15:56:57 +08:00
|
|
|
RailsFailover::Redis.on_fallback do
|
|
|
|
Discourse.clear_redis_readonly!
|
|
|
|
Discourse.request_refresh!
|
|
|
|
MessageBus.keepalive_interval = message_bus_keepalive_interval
|
2020-06-10 14:03:20 +08:00
|
|
|
end
|
2020-06-16 11:53:52 +08:00
|
|
|
|
|
|
|
if Rails.logger.respond_to? :chained
|
|
|
|
RailsFailover::Redis.logger = Rails.logger.chained.first
|
|
|
|
end
|
2020-06-11 13:45:46 +08:00
|
|
|
end
|
2020-06-10 14:03:20 +08:00
|
|
|
|
2020-06-15 16:23:24 +08:00
|
|
|
if defined?(RailsFailover::ActiveRecord)
|
2020-06-16 11:03:47 +08:00
|
|
|
return unless Rails.configuration.active_record_rails_failover
|
|
|
|
|
2020-06-15 15:56:57 +08: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 15:40:37 +08:00
|
|
|
end
|
|
|
|
|
2020-06-15 15:56:57 +08:00
|
|
|
RailsFailover::ActiveRecord.on_failover do
|
2020-07-13 10:13:37 +08:00
|
|
|
if RailsMultisite::ConnectionManagement.current_db == RailsMultisite::ConnectionManagement::DEFAULT
|
2020-07-09 11:46:30 +08:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
|
2020-07-21 13:51:37 +08:00
|
|
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
2020-07-09 11:46:30 +08:00
|
|
|
end
|
2020-06-15 15:56:57 +08:00
|
|
|
end
|
2020-05-28 15:40:37 +08:00
|
|
|
end
|
|
|
|
|
2020-06-15 15:56:57 +08:00
|
|
|
RailsFailover::ActiveRecord.on_fallback do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
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
|
2020-06-01 11:23:58 +08:00
|
|
|
end
|
|
|
|
|
2020-06-15 15:56:57 +08: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 14:53:07 +08:00
|
|
|
if !e.is_a?(Redis::CannotConnectError)
|
2020-07-09 11:14:19 +08:00
|
|
|
Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
|
2020-06-29 11:54:55 +08:00
|
|
|
end
|
2020-07-21 12:32:50 +08:00
|
|
|
|
|
|
|
false
|
2020-06-15 15:56:57 +08:00
|
|
|
end
|
2020-05-28 15:40:37 +08:00
|
|
|
end
|