FIX: Clear anon cache when disabling readonly mode.

FIX: Clear anon cache when disabling readonly mode.

SiteSerializer#is_readonly is cached for an anonymous user so we have
to clear the cache when disabling readonly mode. Otherwise, the site may
appear to be in readonly mode for an extended period of time.

diff --git a/app/models/site.rb b/app/models/site.rb
index 8577329..cdc34c2 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -125,10 +125,12 @@ class Site
     json
   end
 
+  SITE_JSON_CHANNEL = '/site_json'
+
   def self.clear_anon_cache!
     # publishing forces the sequence up
     # the cache is validated based on the sequence
-    MessageBus.publish('/site_json', '')
+    MessageBus.publish(SITE_JSON_CHANNEL, '')
   end
 
 end
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 21a3e33..149b2f5 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -346,6 +346,7 @@ module Discourse
   def self.disable_readonly_mode(key = READONLY_MODE_KEY)
     $redis.del(key)
     MessageBus.publish(readonly_channel, false)
+    Site.clear_anon_cache!
     true
   end
 
diff --git a/spec/components/discourse_spec.rb b/spec/components/discourse_spec.rb
index 5d53b71..068dad3 100644
--- a/spec/components/discourse_spec.rb
+++ b/spec/components/discourse_spec.rb
@@ -190,11 +190,17 @@ describe Discourse do
     describe ".disable_readonly_mode" do
       it "removes a key from redis and publish a message through the message bus" do
         Discourse.enable_readonly_mode
+        message = nil
 
-        message = get_readonly_message do
+        messages = MessageBus.track_publish do
           Discourse.disable_readonly_mode
         end
 
+        expect(messages.any? { |m| m.channel == Site::SITE_JSON_CHANNEL })
+          .to eq(true)
+
+        message = messages.find { |m| m.channel == Discourse.readonly_channel }
+
         assert_readonly_mode_disabled(message, readonly_mode_key)
       end

GitHub
sha: c0aae16f

1 Like