DEV: Use a longer TTL for pg readonly mode.

This commit is contained in:
Guo Xiang Tan 2020-07-14 16:15:58 +08:00
parent 2196d0b9ae
commit 94a2a70462
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 17 additions and 7 deletions

View File

@ -431,6 +431,7 @@ module Discourse
READONLY_MODE_KEY_TTL ||= 60
READONLY_MODE_KEY ||= 'readonly_mode'
PG_READONLY_MODE_KEY ||= 'readonly_mode:postgres'
PG_READONLY_MODE_KEY_TTL ||= 300
USER_READONLY_MODE_KEY ||= 'readonly_mode:user'
PG_FORCE_READONLY_MODE_KEY ||= 'readonly_mode:postgres_force'
@ -445,16 +446,24 @@ module Discourse
if key == USER_READONLY_MODE_KEY || key == PG_FORCE_READONLY_MODE_KEY
Discourse.redis.set(key, 1)
else
Discourse.redis.setex(key, READONLY_MODE_KEY_TTL, 1)
keep_readonly_mode(key) if !Rails.env.test?
ttl =
case key
when PG_READONLY_MODE_KEY
PG_READONLY_MODE_KEY_TTL
else
READONLY_MODE_KEY_TTL
end
Discourse.redis.setex(key, ttl, 1)
keep_readonly_mode(key, ttl: ttl) if !Rails.env.test?
end
MessageBus.publish(readonly_channel, true)
true
end
def self.keep_readonly_mode(key)
# extend the expiry by 1 minute every 30 seconds
def self.keep_readonly_mode(key, ttl:)
# extend the expiry by ttl minute every ttl/2 seconds
@mutex ||= Mutex.new
@mutex.synchronize do
@ -465,12 +474,12 @@ module Discourse
unless @threads[key]&.alive?
@threads[key] = Thread.new do
while @dbs.size > 0 do
sleep 30
sleep ttl / 2
@mutex.synchronize do
@dbs.each do |db|
RailsMultisite::ConnectionManagement.with_connection(db) do
if !Discourse.redis.expire(key, READONLY_MODE_KEY_TTL)
if !Discourse.redis.expire(key, ttl)
@dbs.delete(db)
end
end

View File

@ -235,7 +235,8 @@ describe Discourse do
describe ".disable_readonly_mode" do
context 'user disabled readonly mode' do
it "removes readonly key in redis and publish a message through the message bus" do
Discourse.enable_readonly_mode(user_enabled: true)
message = MessageBus.track_publish { Discourse.disable_readonly_mode(user_readonly_mode_key) }.first
assert_readonly_mode_disabled(message, user_readonly_mode_key)
end
end
end