2020-05-28 15:40:37 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-02 17:24:14 +08:00
|
|
|
if ENV["REDIS_RAILS_FAILOVER"]
|
2020-06-08 12:32:14 +08:00
|
|
|
message_bus_keepalive_interval = nil
|
|
|
|
|
2020-06-02 17:24:14 +08:00
|
|
|
RailsFailover::Redis.on_failover do
|
2020-06-08 12:32:14 +08:00
|
|
|
message_bus_keepalive_interval = MessageBus.keepalive_interval
|
|
|
|
MessageBus.keepalive_interval = -1 # Disable MessageBus keepalive_interval
|
2020-06-02 17:24:14 +08:00
|
|
|
Discourse.received_redis_readonly!
|
|
|
|
end
|
|
|
|
|
|
|
|
RailsFailover::Redis.on_fallback do
|
|
|
|
Discourse.clear_readonly!
|
|
|
|
Discourse.request_refresh!
|
2020-06-08 12:32:14 +08:00
|
|
|
MessageBus.keepalive_interval = message_bus_keepalive_interval
|
2020-06-02 17:24:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-28 15:40:37 +08:00
|
|
|
if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
|
|
|
|
RailsFailover::ActiveRecord.on_failover do
|
2020-06-02 11:32:07 +08:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
2020-06-04 15:43:40 +08:00
|
|
|
Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
|
2020-06-02 11:32:07 +08:00
|
|
|
end
|
2020-05-28 15:40:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
RailsFailover::ActiveRecord.on_fallback do
|
2020-06-02 11:32:07 +08:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
2020-06-04 15:43:40 +08:00
|
|
|
Sidekiq.unpause! if Sidekiq.paused?
|
2020-06-02 11:32:07 +08:00
|
|
|
end
|
2020-05-28 15:40:37 +08:00
|
|
|
end
|
|
|
|
|
2020-06-01 11:23:58 +08:00
|
|
|
module Discourse
|
|
|
|
PG_FORCE_READONLY_MODE_KEY ||= 'readonly_mode:postgres_force'
|
2020-06-01 12:12:24 +08:00
|
|
|
|
|
|
|
READONLY_KEYS.push(PG_FORCE_READONLY_MODE_KEY)
|
|
|
|
|
|
|
|
def self.enable_pg_force_readonly_mode
|
|
|
|
Discourse.redis.set(PG_FORCE_READONLY_MODE_KEY, 1)
|
2020-06-02 09:20:03 +08:00
|
|
|
Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
|
2020-06-01 13:26:12 +08:00
|
|
|
MessageBus.publish(readonly_channel, true)
|
|
|
|
Site.clear_anon_cache!
|
|
|
|
true
|
2020-06-01 12:12:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.disable_pg_force_readonly_mode
|
2020-06-01 13:26:12 +08:00
|
|
|
result = Discourse.redis.del(PG_FORCE_READONLY_MODE_KEY)
|
2020-06-02 09:20:03 +08:00
|
|
|
Sidekiq.unpause!
|
2020-06-01 13:26:12 +08:00
|
|
|
MessageBus.publish(readonly_channel, false)
|
|
|
|
result > 0
|
2020-06-01 12:12:24 +08:00
|
|
|
end
|
2020-06-01 11:23:58 +08:00
|
|
|
end
|
|
|
|
|
2020-05-28 15:40:37 +08:00
|
|
|
RailsFailover::ActiveRecord.register_force_reading_role_callback do
|
2020-06-01 11:23:58 +08:00
|
|
|
Discourse.redis.exists(
|
|
|
|
Discourse::PG_READONLY_MODE_KEY,
|
|
|
|
Discourse::PG_FORCE_READONLY_MODE_KEY
|
|
|
|
)
|
2020-05-28 15:40:37 +08:00
|
|
|
end
|
|
|
|
end
|