6a7eac7a539833d470ec16f1161a705a6969129b
[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       conffile = ENV['ARVADOS_CONFIG'] || "/etc/arvados/config.yml"
20       Rails.logger.info("reload_config: polling for updated mtime on #{conffile} with threshold #{Rails.configuration.SourceTimestamp}")
21       while true
22         sleep 1
23         t = File.mtime(conffile)
24         if t.to_f > Rails.configuration.SourceTimestamp.to_f
25           restartfile = Rails.root.join('tmp', 'restart.txt')
26           touchtime = Time.now
27           Rails.logger.info("reload_config: mtime on #{conffile} changed to #{t}, touching #{restartfile} to #{touchtime}")
28           File.utime(touchtime, touchtime, restartfile)
29           # Even if passenger doesn't notice that we hit restart.txt
30           # and kill our process, there's no point waiting around to
31           # hit it again.
32           break
33         end
34       end
35     end
36   end
37 end