Sha256: c0eddb88021658bdfc511799ce3824da18c90b89b30342eaa3185a14544d15ee

Contents?: true

Size: 1.55 KB

Versions: 11

Compression:

Stored size: 1.55 KB

Contents

class Jason::ConsistencyChecker
  attr_reader :subscription
  attr_reader :inconsistent

  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

  def self.fix_all
    check_all(fix: true)
  end

  def wipe_all_subs

  end

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

  def inconsistent?
    inconsistent
  end

  # Take a subscription, get the current cached payload, and compare it to the data retrieved from the database
  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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
jason-rails-0.8.7 lib/jason/consistency_checker.rb
jason-rails-0.8.6 lib/jason/consistency_checker.rb
jason-rails-0.8.4 lib/jason/consistency_checker.rb
jason-rails-0.8.3 lib/jason/consistency_checker.rb
jason-rails-0.8.2 lib/jason/consistency_checker.rb
jason-rails-0.8.1 lib/jason/consistency_checker.rb
jason-rails-0.7.5.1 lib/jason/consistency_checker.rb
jason-rails-0.8.0 lib/jason/consistency_checker.rb
jason-rails-0.7.5 lib/jason/consistency_checker.rb
jason-rails-0.7.3 lib/jason/consistency_checker.rb
jason-rails-0.7.2 lib/jason/consistency_checker.rb