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