Class: ConfCtl::ConfCache

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(conf_dir) ⇒ ConfCache

Returns a new instance of ConfCache.

Parameters:



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_cacheObject

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

#mtimeTime?

Returns:

  • (Time, nil)


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

#updateObject

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

Parameters:

  • force (Boolean) (defaults to: false)

    force a new check

Returns:

  • (Boolean)


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