DEV: Prevent race condition when keeping site in readonly mode.
This commit is contained in:
parent
615a22a579
commit
0e0daa2c91
|
@ -311,7 +311,7 @@ module Discourse
|
||||||
$redis.set(key, 1)
|
$redis.set(key, 1)
|
||||||
else
|
else
|
||||||
$redis.setex(key, READONLY_MODE_KEY_TTL, 1)
|
$redis.setex(key, READONLY_MODE_KEY_TTL, 1)
|
||||||
keep_readonly_mode(key)
|
keep_readonly_mode(key) if !Rails.env.test?
|
||||||
end
|
end
|
||||||
|
|
||||||
MessageBus.publish(readonly_channel, true)
|
MessageBus.publish(readonly_channel, true)
|
||||||
|
@ -321,20 +321,24 @@ module Discourse
|
||||||
|
|
||||||
def self.keep_readonly_mode(key)
|
def self.keep_readonly_mode(key)
|
||||||
# extend the expiry by 1 minute every 30 seconds
|
# extend the expiry by 1 minute every 30 seconds
|
||||||
unless Rails.env.test?
|
@mutex ||= Mutex.new
|
||||||
|
|
||||||
|
@mutex.synchronize do
|
||||||
@dbs ||= Set.new
|
@dbs ||= Set.new
|
||||||
@dbs << RailsMultisite::ConnectionManagement.current_db
|
@dbs << RailsMultisite::ConnectionManagement.current_db
|
||||||
@threads ||= {}
|
@threads ||= {}
|
||||||
|
|
||||||
unless @threads[key]&.alive?
|
unless @threads[key]&.alive?
|
||||||
@threads[key] = Thread.new do
|
@threads[key] = Thread.new do
|
||||||
while @dbs.size > 0
|
while @dbs.size > 0 do
|
||||||
sleep 30
|
sleep 30
|
||||||
|
|
||||||
@dbs.each do |db|
|
@mutex.synchronize do
|
||||||
RailsMultisite::ConnectionManagement.with_connection(db) do
|
@dbs.each do |db|
|
||||||
if !$redis.expire(key, READONLY_MODE_KEY_TTL)
|
RailsMultisite::ConnectionManagement.with_connection(db) do
|
||||||
@dbs.delete(db)
|
if !$redis.expire(key, READONLY_MODE_KEY_TTL)
|
||||||
|
@dbs.delete(db)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue