2020-05-28 02:40:37 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-11 00:45:46 -05:00
|
|
|
message_bus_keepalive_interval = nil
|
2020-06-07 23:32:14 -05:00
|
|
|
|
2020-06-11 00:45:46 -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-11 00:45:46 -05:00
|
|
|
RailsFailover::Redis.on_fallback do
|
|
|
|
Discourse.clear_redis_readonly!
|
|
|
|
Discourse.request_refresh!
|
|
|
|
MessageBus.keepalive_interval = message_bus_keepalive_interval
|
2020-06-02 04:24:14 -05:00
|
|
|
end
|
|
|
|
|
2020-06-11 00:45:46 -05: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]
|
2020-06-10 01:03:20 -05:00
|
|
|
end
|
2020-06-11 00:45:46 -05:00
|
|
|
end
|
2020-06-10 01:03:20 -05:00
|
|
|
|
2020-06-11 00:45:46 -05:00
|
|
|
RailsFailover::ActiveRecord.on_failover do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
|
|
|
Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
|
2020-05-28 02:40:37 -05:00
|
|
|
end
|
2020-06-11 00:45:46 -05:00
|
|
|
end
|
2020-05-28 02:40:37 -05:00
|
|
|
|
2020-06-11 00:45:46 -05: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?
|
2020-05-28 02:40:37 -05:00
|
|
|
end
|
|
|
|
|
2020-06-11 00:45:46 -05:00
|
|
|
if Rails.configuration.multisite
|
|
|
|
RailsMultisite::ConnectionManagement.default_connection_handler =
|
|
|
|
ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
|
2020-05-31 22:23:58 -05:00
|
|
|
end
|
2020-06-11 00:45:46 -05:00
|
|
|
end
|
2020-05-31 22:23:58 -05:00
|
|
|
|
2020-06-11 00:45:46 -05: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
|
|
|
|
Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
|
|
|
|
false
|
2020-05-28 02:40:37 -05:00
|
|
|
end
|