Skip to content

Commit

Permalink
FIX: Backup/Restore didn't use correct Redis namespace in multisite (d…
Browse files Browse the repository at this point in the history
…iscourse#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.
  • Loading branch information
gschlager authored Aug 23, 2022
1 parent 9356091 commit 9ff13ce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
14 changes: 10 additions & 4 deletions lib/backup_restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ def self.running_key
end

def self.keep_it_running
db = RailsMultisite::ConnectionManagement.current_db

# extend the expiry by 1 minute every 30 seconds
Thread.new do
# this thread will be killed when the fork dies
while true
Discourse.redis.expire(running_key, 1.minute)
sleep 30.seconds
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)
sleep 30.seconds
end
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/backup_restore/backuper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ def listen_for_shutdown_signal
BackupRestore.clear_shutdown_signal!

Thread.new do
while BackupRestore.is_operation_running?
exit if BackupRestore.should_shutdown?
sleep 0.1
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
Expand Down
10 changes: 7 additions & 3 deletions lib/backup_restore/system_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ def listen_for_shutdown_signal
BackupRestore.clear_shutdown_signal!

Thread.new do
while BackupRestore.is_operation_running?
exit if BackupRestore.should_shutdown?
sleep 0.1
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
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/backup_restore/system_interface_multisite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,21 @@
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

0 comments on commit 9ff13ce

Please sign in to comment.