af266cae36f2e46b30cfa1110c76a21000ef437a
[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 else
8   Thread.new do
9     lockfile = Rails.root.join('tmp', 'reload_config.lock')
10     File.open(lockfile, File::WRONLY|File::CREAT, 0600) do |f|
11       # Note we don't use LOCK_NB here. If we did, each time passenger
12       # kills the lock-holder process, we would be left with nobody
13       # checking for updates until passenger starts a new worker,
14       # which could be a long time.
15       Rails.logger.debug("reload_config: waiting for lock on #{lockfile}")
16       f.flock(File::LOCK_EX)
17       conffile = ENV['ARVADOS_CONFIG'] || "/etc/arvados/config.yml"
18       Rails.logger.info("reload_config: polling for updated mtime on #{conffile} with threshold #{Rails.configuration.SourceTimestamp}")
19       while true
20         sleep 1
21         t = File.mtime(conffile)
22         if t.to_f > Rails.configuration.SourceTimestamp.to_f
23           restartfile = Rails.root.join('tmp', 'restart.txt')
24           touchtime = Time.now
25           Rails.logger.info("reload_config: mtime on #{conffile} changed to #{t}, touching #{restartfile} to #{touchtime}")
26           File.utime(touchtime, touchtime, restartfile)
27           # Even if passenger doesn't notice that we hit restart.txt
28           # and kill our process, there's no point waiting around to
29           # hit it again.
30           break
31         end
32       end
33     end
34   end
35 end