Class: ConfCtl::ConfCache
- Inherits:
-
Object
- Object
- ConfCtl::ConfCache
- Defined in:
- lib/confctl/conf_cache.rb
Overview
Cache the list of configuration files to detect changes
The assumption is that if there hasn’t been any changes in configuration directory, we can reuse previously built artefacts, such as the list of machines, etc. This is much faster than invoking nix-build to query the machines.
Instance Method Summary collapse
-
#initialize(conf_dir) ⇒ ConfCache
constructor
A new instance of ConfCache.
-
#load_cache ⇒ Object
Load file list from cache file.
- #mtime ⇒ Time?
-
#update ⇒ Object
Update cache file with the current state of the configuration directory.
-
#uptodate?(force: false) ⇒ Boolean
Check if cached file list differs from files on disk.
Constructor Details
#initialize(conf_dir) ⇒ ConfCache
Returns a new instance of ConfCache.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/confctl/conf_cache.rb', line 13 def initialize(conf_dir) @conf_dir = conf_dir @cmd = SystemCommand.new @cache_dir = File.join(conf_dir.cache_dir, 'build') @cache_file = File.join(@cache_dir, 'git-files.json') @files = {} @loaded = false @uptodate = nil @checked_at = nil end |
Instance Method Details
#load_cache ⇒ Object
Load file list from cache file
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/confctl/conf_cache.rb', line 25 def load_cache begin data = File.read(@cache_file) rescue Errno::ENOENT return end @files = JSON.parse(data)['files'] @loaded = true end |
#mtime ⇒ Time?
72 73 74 75 76 |
# File 'lib/confctl/conf_cache.rb', line 72 def mtime File.lstat(@cache_file).mtime rescue Errno::ENOENT nil end |
#update ⇒ Object
Update cache file with the current state of the configuration directory
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/confctl/conf_cache.rb', line 46 def update @files.clear list_files.each do |file| begin st = File.lstat(file) rescue Errno::ENOENT next end @files[file] = { 'mtime' => st.mtime.to_i, 'size' => st.size } end tmp = "#{@cache_file}.new" FileUtils.mkpath(@cache_dir) File.write(tmp, { 'files' => @files }.to_json) File.rename(tmp, @cache_file) @uptodate = true end |
#uptodate?(force: false) ⇒ Boolean
Check if cached file list differs from files on disk
38 39 40 41 42 43 |
# File 'lib/confctl/conf_cache.rb', line 38 def uptodate?(force: false) return @uptodate if !@uptodate.nil? && !force @uptodate = check_uptodate @uptodate end |