1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 # Please don't edit this version. Instead, edit
6 # services/api/config/initializers/reload_config.rb and update this
7 # copy. Or find a more reasonable way to share the code.
9 def start_reload_thread
11 lockfile = Rails.root.join('tmp', 'reload_config.lock')
12 File.open(lockfile, File::WRONLY|File::CREAT, 0600) do |f|
13 # Note we don't use LOCK_NB here. If we did, each time passenger
14 # kills the lock-holder process, we would be left with nobody
15 # checking for updates until passenger starts a new worker,
16 # which could be a long time.
17 Rails.logger.debug("reload_config: waiting for lock on #{lockfile}")
18 f.flock(File::LOCK_EX)
20 t_lastload = Rails.configuration.SourceTimestamp
21 hash_lastload = Rails.configuration.SourceSHA256
22 conffile = ENV['ARVADOS_CONFIG'] || "/etc/arvados/config.yml"
23 Rails.logger.info("reload_config: polling for updated mtime on #{conffile} with threshold #{t_lastload}")
26 t = File.mtime(conffile)
27 # If the file is newer than 5s, re-read it even if the
28 # timestamp matches the previously loaded file. This enables
29 # us to detect changes even if the filesystem's timestamp
30 # precision cannot represent multiple updates per second.
31 if t.to_f != t_lastload.to_f || Time.now.to_f - t.to_f < 5
32 Open3.popen2("arvados-server", "config-dump", "-skip-legacy") do |stdin, stdout, status_thread|
33 confs = YAML.load(stdout, deserialize_symbols: false)
34 hash = confs["SourceSHA256"]
36 Rails.logger.info("reload_config: config file updated but could not be loaded: #{e}")
40 if hash == hash_lastload
41 # If we reloaded a new or updated file, but the content is
42 # identical, keep polling instead of restarting.
47 restartfile = Rails.root.join('tmp', 'restart.txt')
49 Rails.logger.info("reload_config: mtime on #{conffile} changed to #{t}, touching #{restartfile} to #{touchtime}")
51 File.utime(touchtime, touchtime, restartfile)
53 # remove + re-create works even if the existing file is
54 # owned by root, provided the tempdir is writable.
55 File.unlink(restartfile) rescue nil
56 File.open(restartfile, 'w') {}
58 # Even if passenger doesn't notice that we hit restart.txt
59 # and kill our process, there's no point waiting around to
68 if !File.owned?(Rails.root.join('tmp'))
69 Rails.logger.debug("reload_config: not owner of #{Rails.root}/tmp, skipping")
70 elsif ENV["ARVADOS_CONFIG"] == "none"
71 Rails.logger.debug("reload_config: no config in use, skipping")
72 elsif defined?(PhusionPassenger)
73 PhusionPassenger.on_event(:starting_worker_process) do |forked|