18874: Add 'services/workbench2/' from commit 'f6f88d9ca9cdeeeebfadcfe999789bfb9f69e5c6'
[arvados.git] / services / api / config / initializers / reload_config.rb
index 65f02e07a4e8bf942657358c1fd9538c7b7813af..b54e3bcf87e764aec3508aec0ff5444cc7de9c37 100644 (file)
@@ -2,11 +2,10 @@
 #
 # SPDX-License-Identifier: AGPL-3.0
 
-if !File.owned?(Rails.root.join('tmp'))
-  Rails.logger.debug("reload_config: not owner of #{Rails.root}/tmp, skipping")
-elsif ENV["ARVADOS_CONFIG"] == "none"
-  Rails.logger.debug("reload_config: no config in use, skipping")
-else
+# When updating this, please make the same changes in
+# apps/workbench/config/initializers/reload_config.rb as well.
+
+def start_reload_thread
   Thread.new do
     lockfile = Rails.root.join('tmp', 'reload_config.lock')
     File.open(lockfile, File::WRONLY|File::CREAT, 0600) do |f|
@@ -35,13 +34,13 @@ else
           rescue => e
             Rails.logger.info("reload_config: config file updated but could not be loaded: #{e}")
             t_lastload = t
-            continue
+            next
           end
           if hash == hash_lastload
             # If we reloaded a new or updated file, but the content is
             # identical, keep polling instead of restarting.
             t_lastload = t
-            continue
+            next
           end
 
           restartfile = Rails.root.join('tmp', 'restart.txt')
@@ -50,6 +49,9 @@ else
           begin
             File.utime(touchtime, touchtime, restartfile)
           rescue
+            # remove + re-create works even if the existing file is
+            # owned by root, provided the tempdir is writable.
+            File.unlink(restartfile) rescue nil
             File.open(restartfile, 'w') {}
           end
           # Even if passenger doesn't notice that we hit restart.txt
@@ -61,3 +63,15 @@ else
     end
   end
 end
+
+if !File.owned?(Rails.root.join('tmp'))
+  Rails.logger.debug("reload_config: not owner of #{Rails.root}/tmp, skipping")
+elsif ENV["ARVADOS_CONFIG"] == "none"
+  Rails.logger.debug("reload_config: no config in use, skipping")
+elsif defined?(PhusionPassenger)
+  PhusionPassenger.on_event(:starting_worker_process) do |forked|
+    start_reload_thread
+  end
+else
+  start_reload_thread
+end