2020-05-28 03:40:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
|
|
|
|
RailsFailover::ActiveRecord.on_failover do
|
|
|
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
|
|
|
Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
|
|
|
|
end
|
|
|
|
|
|
|
|
RailsFailover::ActiveRecord.on_fallback do
|
|
|
|
Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
|
|
|
Sidekiq.unpause!
|
|
|
|
end
|
|
|
|
|
2020-05-31 23:23:58 -04:00
|
|
|
module Discourse
|
|
|
|
PG_FORCE_READONLY_MODE_KEY ||= 'readonly_mode:postgres_force'
|
2020-06-01 00:12:24 -04: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)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.disable_pg_force_readonly_mode
|
|
|
|
Discourse.redis.del(PG_FORCE_READONLY_MODE_KEY)
|
|
|
|
end
|
2020-05-31 23:23:58 -04:00
|
|
|
end
|
|
|
|
|
2020-05-28 03:40:37 -04:00
|
|
|
RailsFailover::ActiveRecord.register_force_reading_role_callback do
|
2020-05-31 23:23:58 -04:00
|
|
|
Discourse.redis.exists(
|
|
|
|
Discourse::PG_READONLY_MODE_KEY,
|
|
|
|
Discourse::PG_FORCE_READONLY_MODE_KEY
|
|
|
|
)
|
2020-05-28 03:40:37 -04:00
|
|
|
end
|
|
|
|
end
|