DEV: Prevent race condition when keeping site in readonly mode.

This commit is contained in:
Guo Xiang Tan 2019-02-20 10:01:18 +08:00
parent 615a22a579
commit 0e0daa2c91

View File

@ -311,7 +311,7 @@ module Discourse
$redis.set(key, 1)
else
$redis.setex(key, READONLY_MODE_KEY_TTL, 1)
keep_readonly_mode(key)
keep_readonly_mode(key) if !Rails.env.test?
end
MessageBus.publish(readonly_channel, true)
@ -321,16 +321,19 @@ module Discourse
def self.keep_readonly_mode(key)
# extend the expiry by 1 minute every 30 seconds
unless Rails.env.test?
@mutex ||= Mutex.new
@mutex.synchronize do
@dbs ||= Set.new
@dbs << RailsMultisite::ConnectionManagement.current_db
@threads ||= {}
unless @threads[key]&.alive?
@threads[key] = Thread.new do
while @dbs.size > 0
while @dbs.size > 0 do
sleep 30
@mutex.synchronize do
@dbs.each do |db|
RailsMultisite::ConnectionManagement.with_connection(db) do
if !$redis.expire(key, READONLY_MODE_KEY_TTL)
@ -343,6 +346,7 @@ module Discourse
end
end
end
end
def self.disable_readonly_mode(key = READONLY_MODE_KEY)
$redis.del(key)