From c544686540c7870eede271bb9e39f0a9917854d1 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Mon, 30 Sep 2024 13:59:51 -0500 Subject: [PATCH] DEV: Add expires flag to enable_readonly (#29033) ... to control whether readonly mode expires or not. --- lib/discourse.rb | 18 ++++++++++++------ spec/lib/discourse_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index c1dafbfedd7..1dfe73fee07 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -675,16 +675,20 @@ module Discourse 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 Sidekiq.pause!("pg_failover") if !Sidekiq.paused? end - if [USER_READONLY_MODE_KEY, PG_FORCE_READONLY_MODE_KEY, STAFF_WRITES_ONLY_MODE_KEY].include?( - key, - ) - Discourse.redis.set(key, 1) - else + if expires.nil? + expires = [ + USER_READONLY_MODE_KEY, + PG_FORCE_READONLY_MODE_KEY, + STAFF_WRITES_ONLY_MODE_KEY, + ].exclude?(key) + end + + if expires ttl = case key when PG_READONLY_MODE_KEY @@ -695,6 +699,8 @@ module Discourse Discourse.redis.setex(key, ttl, 1) keep_readonly_mode(key, ttl: ttl) if !Rails.env.test? + else + Discourse.redis.set(key, 1) end MessageBus.publish(readonly_channel, true) diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rb index becf38e8528..5671149c65c 100644 --- a/spec/lib/discourse_spec.rb +++ b/spec/lib/discourse_spec.rb @@ -253,6 +253,16 @@ RSpec.describe Discourse do end 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 expect(Discourse.redis.get(readonly_mode_key)).to eq(nil) end