DEV: Add expires flag to enable_readonly (#29033)

... to control whether readonly mode expires or not.
This commit is contained in:
Daniel Waterworth 2024-09-30 13:59:51 -05:00 committed by GitHub
parent 8cef93a45d
commit c544686540
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View File

@ -675,16 +675,20 @@ module Discourse
PG_FORCE_READONLY_MODE_KEY, PG_FORCE_READONLY_MODE_KEY,
] ]
def self.enable_readonly_mode(key = READONLY_MODE_KEY) def self.enable_readonly_mode(key = READONLY_MODE_KEY, expires: nil)
if key == PG_READONLY_MODE_KEY || key == PG_FORCE_READONLY_MODE_KEY if key == PG_READONLY_MODE_KEY || key == PG_FORCE_READONLY_MODE_KEY
Sidekiq.pause!("pg_failover") if !Sidekiq.paused? Sidekiq.pause!("pg_failover") if !Sidekiq.paused?
end end
if [USER_READONLY_MODE_KEY, PG_FORCE_READONLY_MODE_KEY, STAFF_WRITES_ONLY_MODE_KEY].include?( if expires.nil?
key, expires = [
) USER_READONLY_MODE_KEY,
Discourse.redis.set(key, 1) PG_FORCE_READONLY_MODE_KEY,
else STAFF_WRITES_ONLY_MODE_KEY,
].exclude?(key)
end
if expires
ttl = ttl =
case key case key
when PG_READONLY_MODE_KEY when PG_READONLY_MODE_KEY
@ -695,6 +699,8 @@ module Discourse
Discourse.redis.setex(key, ttl, 1) Discourse.redis.setex(key, ttl, 1)
keep_readonly_mode(key, ttl: ttl) if !Rails.env.test? keep_readonly_mode(key, ttl: ttl) if !Rails.env.test?
else
Discourse.redis.set(key, 1)
end end
MessageBus.publish(readonly_channel, true) MessageBus.publish(readonly_channel, true)

View File

@ -253,6 +253,16 @@ RSpec.describe Discourse do
end end
describe ".enable_readonly_mode" do describe ".enable_readonly_mode" do
it "doesn't expire when expires is false" do
Discourse.enable_readonly_mode(user_readonly_mode_key, expires: false)
expect(Discourse.redis.ttl(user_readonly_mode_key)).to eq(-1)
end
it "expires when expires is true" do
Discourse.enable_readonly_mode(user_readonly_mode_key, expires: true)
expect(Discourse.redis.ttl(user_readonly_mode_key)).not_to eq(-1)
end
it "adds a key in redis and publish a message through the message bus" do it "adds a key in redis and publish a message through the message bus" do
expect(Discourse.redis.get(readonly_mode_key)).to eq(nil) expect(Discourse.redis.get(readonly_mode_key)).to eq(nil)
end end