FEATURE: Introduce pg_force_readonly_mode GlobalSetting (#19612)

This allows the entire cluster to be forced into pg readonly mode. Equivalent to running `Discourse.enable_pg_force_readonly_mode` on the console.
This commit is contained in:
David Taylor 2023-01-19 13:59:11 +00:00 committed by GitHub
parent 998c47cf82
commit 5406e24acb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 2 deletions

View File

@ -365,3 +365,6 @@ preload_link_header = false
# When using an external upload store, redirect `user_avatar` requests instead of proxying
redirect_avatar_requests = false
# Force the entire cluster into postgres readonly mode. Equivalent to running `Discourse.enable_pg_force_readonly_mode`
pg_force_readonly_mode = false

View File

@ -69,7 +69,11 @@ if defined?(RailsFailover::ActiveRecord)
end
RailsFailover::ActiveRecord.register_force_reading_role_callback do
Discourse.redis.exists?(Discourse::PG_READONLY_MODE_KEY, Discourse::PG_FORCE_READONLY_MODE_KEY)
GlobalSetting.pg_force_readonly_mode ||
Discourse.redis.exists?(
Discourse::PG_READONLY_MODE_KEY,
Discourse::PG_FORCE_READONLY_MODE_KEY,
)
rescue => e
if !e.is_a?(Redis::CannotConnectError)
Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"

View File

@ -691,7 +691,7 @@ module Discourse
end
def self.readonly_mode?(keys = READONLY_KEYS)
recently_readonly? || Discourse.redis.exists?(*keys)
recently_readonly? || GlobalSetting.pg_force_readonly_mode || Discourse.redis.exists?(*keys)
end
def self.staff_writes_only_mode?

View File

@ -300,6 +300,12 @@ RSpec.describe Discourse do
Discourse.disable_readonly_mode(user_readonly_mode_key)
expect(Discourse.readonly_mode?).to eq(false)
end
it "returns true when forced via global setting" do
expect(Discourse.readonly_mode?).to eq(false)
global_setting :pg_force_readonly_mode, true
expect(Discourse.readonly_mode?).to eq(true)
end
end
describe ".received_postgres_readonly!" do