FIX: Forking prevented notifications from being sent after backup

FIX: Forking prevented notifications from being sent after backup

This is a workaround for https://github.com/rubyjs/mini_racer/issues/175

diff --git a/lib/backup_restore.rb b/lib/backup_restore.rb
index 5b18ef6..3643cce 100644
--- a/lib/backup_restore.rb
+++ b/lib/backup_restore.rb
@@ -12,29 +12,18 @@ module BackupRestore
     if opts[:fork] == false
       BackupRestore::Backuper.new(user_id, opts).run
     else
-      start! BackupRestore::Backuper.new(user_id, opts)
+      spawn_process!(:backup, user_id, opts)
     end
   end
 
   def self.restore!(user_id, opts = {})
-    restorer = BackupRestore::Restorer.new(
-      user_id: user_id,
-      filename: opts[:filename],
-      factory: BackupRestore::Factory.new(
-        user_id: user_id,
-        client_id: opts[:client_id]
-      ),
-      disable_emails: opts.fetch(:disable_emails, true)
-    )
-
-    start! restorer
+    spawn_process!(:restore, user_id, opts)
   end
 
   def self.rollback!
     raise BackupRestore::OperationRunningError if BackupRestore.is_operation_running?
     if can_rollback?
       move_tables_between_schemas("backup", "public")
-      after_fork
     end
   end
 
@@ -181,39 +170,12 @@ module BackupRestore
     "start_logs_message_id"
   end
 
-  def self.start!(runner)
-    child = fork do
-      begin
-        after_fork
-        runner.run
-      rescue Exception => e
-        puts "--------------------------------------------"
-        puts "---------------- EXCEPTION -----------------"
-        puts e.message
-        puts e.backtrace.join("\n")
-        puts "--------------------------------------------"
-      ensure
-        begin
-          clear_shutdown_signal!
-        rescue Exception => e
-          puts "============================================"
-          puts "================ EXCEPTION ================="
-          puts e.message
-          puts e.backtrace.join("\n")
-          puts "============================================"
-        ensure
-          exit!(0)
-        end
-      end
-    end
-
-    Process.detach(child)
-
-    true
-  end
+  def self.spawn_process!(type, user_id, opts)
+    script = File.join(Rails.root, "script", "spawn_backup_restore.rb")
+    command = ["bundle", "exec", "ruby", script, type, user_id, opts.to_json].shelljoin
 
-  def self.after_fork
-    Discourse.after_fork
+    pid = spawn(command)
+    Process.detach(pid)
   end
 
   def self.backup_tables_count
diff --git a/script/spawn_backup_restore.rb b/script/spawn_backup_restore.rb
new file mode 100644
index 0000000..57e0de5
--- /dev/null
+++ b/script/spawn_backup_restore.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+# This script is used by BackupRestore.backup! and BackupRestore.restore!
+
+require File.expand_path("../../config/environment", __FILE__)
+
+def backup
+  user_id, opts = parse_params
+  BackupRestore::Backuper.new(user_id, opts).run
+end
+
+def restore
+  user_id, opts = parse_params
+
+  BackupRestore::Restorer.new(
+    user_id: user_id,
+    filename: opts[:filename],
+    factory: BackupRestore::Factory.new(
+      user_id: user_id,
+      client_id: opts[:client_id]
+    ),
+    disable_emails: opts.fetch(:disable_emails, true)
+  ).run
+end
+
+def parse_params
+  user_id = ARGV[1].to_i
+  opts = JSON.parse(ARGV[2], symbolize_names: true)
+  [user_id, opts]
+end
+
+case ARGV[0]
+when "backup"
+  backup
+when "restore"
+  restore
+else
+  raise "Unknown argument: #{ARGV[0]}"
+end

GitHub sha: 76477a1c

1 Like