FIX: Backup/Restore didn't use correct Redis namespace in multisite (#18060)

In a multisite Discourse reported that no backup is running after 60 seconds because the Redis key expired. Also, the thread that listens for a shutdown signal stopped running immediately because it didn't detect a running operation.
This commit is contained in:
Gerhard Schlager 2022-08-24 01:43:42 +02:00 committed by GitHub
parent 935609172a
commit 9ff13cee14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 10 deletions

View File

@ -144,8 +144,13 @@ module BackupRestore
end
def self.keep_it_running
db = RailsMultisite::ConnectionManagement.current_db
# extend the expiry by 1 minute every 30 seconds
Thread.new do
RailsMultisite::ConnectionManagement.with_connection(db) do
Thread.current.name = "keep_op_running"
# this thread will be killed when the fork dies
while true
Discourse.redis.expire(running_key, 1.minute)
@ -153,6 +158,7 @@ module BackupRestore
end
end
end
end
def self.shutdown_signal_key
"backup_restore_operation_should_shutdown"

View File

@ -98,12 +98,16 @@ module BackupRestore
BackupRestore.clear_shutdown_signal!
Thread.new do
Thread.current.name = "shutdown_wait"
RailsMultisite::ConnectionManagement.with_connection(@current_db) do
while BackupRestore.is_operation_running?
exit if BackupRestore.should_shutdown?
sleep 0.1
end
end
end
end
def mark_backup_as_running
log "Marking backup as running..."

View File

@ -47,12 +47,16 @@ module BackupRestore
BackupRestore.clear_shutdown_signal!
Thread.new do
Thread.current.name = "shutdown_wait"
RailsMultisite::ConnectionManagement.with_connection(@current_db) do
while BackupRestore.is_operation_running?
exit if BackupRestore.should_shutdown?
sleep 0.1
end
end
end
end
def pause_sidekiq(reason)
return if Sidekiq.paused?

View File

@ -36,4 +36,21 @@ RSpec.describe BackupRestore::SystemInterface, type: :multisite do
end
end
end
describe "#listen_for_shutdown_signal" do
it "uses the correct Redis namespace" do
test_multisite_connection("second") do
BackupRestore.mark_as_running!
expect do
thread = subject.listen_for_shutdown_signal
BackupRestore.set_shutdown_signal!
thread.join
end.to raise_error(SystemExit)
BackupRestore.clear_shutdown_signal!
BackupRestore.mark_as_not_running!
end
end
end
end