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
1 changed files with 11 additions and 7 deletions

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,20 +321,24 @@ 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
@dbs.each do |db|
RailsMultisite::ConnectionManagement.with_connection(db) do
if !$redis.expire(key, READONLY_MODE_KEY_TTL)
@dbs.delete(db)
@mutex.synchronize do
@dbs.each do |db|
RailsMultisite::ConnectionManagement.with_connection(db) do
if !$redis.expire(key, READONLY_MODE_KEY_TTL)
@dbs.delete(db)
end
end
end
end