DEV: Improve auto-restart parent process detection logic (#13068)

DEV: Improve auto-restart parent process detection logic (#13068)

The auto restart logic was sending a USR2 to the parent process without checking what the parent process actually was. In some situations, it might not be the bin/unicorn supervisor.

This commit switches to use a global variable for the supervisor PID. This will be much less prone to unexpected behavior.

diff --git a/bin/unicorn b/bin/unicorn
index 106649c..cc24169 100755
--- a/bin/unicorn
+++ b/bin/unicorn
@@ -87,6 +87,8 @@ end
 # This is handy if you want to bind a key to restarting unicorn in dev
 
 if dev_mode
+  $unicorn_dev_supervisor_pid = Process.pid # rubocop:disable Style/GlobalVars
+
   restart = true
   while restart
     restart = false
diff --git a/config/initializers/000-development_reload_warnings.rb b/config/initializers/000-development_reload_warnings.rb
index bedc2c1..374bb74 100644
--- a/config/initializers/000-development_reload_warnings.rb
+++ b/config/initializers/000-development_reload_warnings.rb
@@ -12,7 +12,8 @@ if Rails.env.development? && !Rails.configuration.cache_classes && Discourse.run
   ]
 
   Listen.to(*paths, only: /\.rb$/) do |modified, added, removed|
-    auto_restart = ENV["AUTO_RESTART"] != "0"
+    supervisor_pid = $unicorn_dev_supervisor_pid # rubocop:disable Style/GlobalVars
+    auto_restart = supervisor_pid && ENV["AUTO_RESTART"] != "0"
 
     files = modified + added + removed
 
@@ -25,7 +26,7 @@ if Rails.env.development? && !Rails.configuration.cache_classes && Discourse.run
       message = auto_restart ? "Restarting server..." : "Server restart required. Automate this by setting AUTO_RESTART=1."
       STDERR.puts "[DEV]: Edited files which are not autoloaded. #{message}"
       STDERR.puts not_autoloaded.map { |path| "- #{path}".indent(7) }.join("\n")
-      Process.kill("USR2", Process.ppid) if auto_restart
+      Process.kill("USR2", supervisor_pid) if auto_restart
     end
   end.start
 end

GitHub sha: b6b27bc3

This commit appears in #13068 which was approved by gschlager. It was merged by davidtaylorhq.