Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / services / api / config / initializers / reload_config.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 if !File.owned?(Rails.root.join('tmp'))
6   Rails.logger.debug("reload_config: not owner of #{Rails.root}/tmp, skipping")
7 elsif ENV["ARVADOS_CONFIG"] == "none"
8   Rails.logger.debug("reload_config: no config in use, skipping")
9 else
10   Thread.new do
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)
19
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}")
24       while true
25         sleep 1
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"]
35           rescue => e
36             Rails.logger.info("reload_config: config file updated but could not be loaded: #{e}")
37             t_lastload = t
38             continue
39           end
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.
43             t_lastload = t
44             continue
45           end
46
47           restartfile = Rails.root.join('tmp', 'restart.txt')
48           touchtime = Time.now
49           Rails.logger.info("reload_config: mtime on #{conffile} changed to #{t}, touching #{restartfile} to #{touchtime}")
50           begin
51             File.utime(touchtime, touchtime, restartfile)
52           rescue
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') {}
57           end
58           # Even if passenger doesn't notice that we hit restart.txt
59           # and kill our process, there's no point waiting around to
60           # hit it again.
61           break
62         end
63       end
64     end
65   end
66 end