Class: Kennel::Syncer::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/syncer/resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected:, filter:) ⇒ Resolver

Returns a new instance of Resolver.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kennel/syncer/resolver.rb', line 8

def initialize(expected:, filter:)
  @id_map = IdMap.new
  @filter = filter

  # mark everything as new
  expected.each do |e|
    id_map.set(e.class.api_resource, e.tracking_id, IdMap::NEW)
    if e.class.api_resource == "synthetics/tests"
      id_map.set(Kennel::Models::Monitor.api_resource, e.tracking_id, IdMap::NEW)
    end
  end
end

Instance Method Details

#add_actual(actual) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kennel/syncer/resolver.rb', line 21

def add_actual(actual)
  # override resources that exist with their id
  actual.each do |a|
    # ignore when not managed by kennel
    next unless tracking_id = a.fetch(:tracking_id)

    # ignore when deleted from the codebase
    # (when running with filters we cannot see the other resources in the codebase)
    api_resource = a.fetch(:klass).api_resource
    next if !id_map.get(api_resource, tracking_id) && filter.matches_tracking_id?(tracking_id)

    id_map.set(api_resource, tracking_id, a.fetch(:id))
    if a.fetch(:klass).api_resource == "synthetics/tests"
      id_map.set(Kennel::Models::Monitor.api_resource, tracking_id, a.fetch(:monitor_id))
    end
  end
end

#each_resolved(list) ⇒ Object

loop over items until everything is resolved or crash when we get stuck this solves cases like composite monitors depending on each other or monitor->monitor slo->slo monitor chains



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kennel/syncer/resolver.rb', line 47

def each_resolved(list)
  list = list.dup
  loop do
    return if list.empty?
    list.reject! do |item|
      if resolved?(item.expected)
        yield item
        true
      else
        false
      end
    end ||
      assert_resolved(list[0].expected) # resolve something or show a circular dependency error
  end
end

#resolve_as_much_as_possible(expected) ⇒ Object



39
40
41
42
43
# File 'lib/kennel/syncer/resolver.rb', line 39

def resolve_as_much_as_possible(expected)
  expected.each do |e|
    e.resolve_linked_tracking_ids!(id_map, force: false)
  end
end