Class: Jason::ConsistencyChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/jason/consistency_checker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subscription) ⇒ ConsistencyChecker

Returns a new instance of ConsistencyChecker.



28
29
30
31
# File 'lib/jason/consistency_checker.rb', line 28

def initialize(subscription)
  @subscription = subscription
  @inconsistent = false
end

Instance Attribute Details

#inconsistentObject (readonly)

Returns the value of attribute inconsistent.



3
4
5
# File 'lib/jason/consistency_checker.rb', line 3

def inconsistent
  @inconsistent
end

#subscriptionObject (readonly)

Returns the value of attribute subscription.



2
3
4
# File 'lib/jason/consistency_checker.rb', line 2

def subscription
  @subscription
end

Class Method Details

.check_all(fix: false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jason/consistency_checker.rb', line 5

def self.check_all(fix: false)
  Jason::Subscription.all.each do |sub|
    next if sub.consumer_count == 0
    checker = Jason::ConsistencyChecker.new(sub)
    result = checker.check
    if checker.inconsistent?
      pp sub.config
      pp result
      if fix
        sub.reset!(hard: true)
      end
    end
  end
end

.fix_allObject



20
21
22
# File 'lib/jason/consistency_checker.rb', line 20

def self.fix_all
  check_all(fix: true)
end

Instance Method Details

#checkObject

Take a subscription, get the current cached payload, and compare it to the data retrieved from the database



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
# File 'lib/jason/consistency_checker.rb', line 38

def check
  cached_payload = subscription.get
  edge_set = subscription.load_ids_for_sub_models(subscription.model, nil)

  result = cached_payload.map do |model_name, data|
    cached_payload_instance_ids = data[:payload].map { |row| row['id'] }

    model_idx = edge_set[:model_names].index(model_name)
    if model_idx.present?
      edge_set_instance_ids = edge_set[:instance_ids].map { |row| row[model_idx] }
    else
      next
    end

    missing = edge_set_instance_ids - cached_payload_instance_ids
    intruding = cached_payload_instance_ids - edge_set_instance_ids

    if missing.present? || intruding.present?
      @inconsistent = true
    end

    [model_name, {
      'missing' => missing,
      'intruding' => intruding
    }]
  end.compact.to_h
end

#inconsistent?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/jason/consistency_checker.rb', line 33

def inconsistent?
  inconsistent
end

#wipe_all_subsObject



24
25
26
# File 'lib/jason/consistency_checker.rb', line 24

def wipe_all_subs

end