Class: Textus::Store::Staleness

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/store/staleness.rb

Overview

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/BlockLength

Instance Method Summary collapse

Constructor Details

#initialize(manifest:) ⇒ Staleness

Returns a new instance of Staleness.



7
8
9
# File 'lib/textus/store/staleness.rb', line 7

def initialize(manifest:)
  @manifest = manifest
end

Instance Method Details

#call(prefix: nil, zone: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/textus/store/staleness.rb', line 11

def call(prefix: nil, zone: nil)
  out = []
  @manifest.entries.each do |mentry|
    next unless mentry.zone == "derived"
    next if zone && mentry.zone != zone

    gen = mentry.generator
    next unless gen
    next if prefix && !(mentry.key == prefix || mentry.key.start_with?("#{prefix}."))

    path = Textus::Key::Path.resolve(@manifest, mentry)

    unless File.exist?(path)
      out << stale_row(mentry, path, "derived entry has never been generated")
      next
    end

    raw = File.binread(path)
    parsed = Entry.for_format(mentry.format).parse(raw, path: path)
    generated_at = parsed["_meta"].dig("generated", "at")
    unless generated_at
      out << stale_row(mentry, path, "missing generated.at frontmatter")
      next
    end
    gen_time = begin
      Time.parse(generated_at.to_s)
    rescue StandardError
      nil
    end
    unless gen_time
      out << stale_row(mentry, path, "unparseable generated.at: #{generated_at.inspect}")
      next
    end

    offender = newest_source_after(gen, gen_time)
    out << stale_row(mentry, path, "source '#{offender}' modified after generated.at") if offender
  end

  @manifest.entries.each do |mentry|
    next unless mentry.fetch
    next if zone && mentry.zone != zone
    next if prefix && !(mentry.key == prefix || mentry.key.start_with?("#{prefix}."))

    ttl = parse_ttl(mentry.ttl)
    next unless ttl

    path = Textus::Key::Path.resolve(@manifest, mentry)

    unless File.exist?(path)
      out << intake_stale_row(mentry, path, "never refreshed")
      next
    end

    meta = Entry.for_format(mentry.format).parse(File.binread(path), path: path)["_meta"]
    last_str = meta["last_refreshed_at"]
    if last_str.nil?
      out << intake_stale_row(mentry, path, "never refreshed (no last_refreshed_at)")
      next
    end

    last = begin
      Time.parse(last_str.to_s)
    rescue StandardError
      nil
    end
    out << intake_stale_row(mentry, path, "ttl exceeded (#{ttl}s)") if last.nil? || (Time.now - last) > ttl
  end

  out
end