#!/usr/bin/env ruby
# frozen_string_literal: true

# check if the bundle installed gems somewhere besides the system, and if it
# did manually add that path to the GEM_PATH. all without actually loading the
# bundle, because it's huge
require "pathname"
expanded_path = Pathname.new(__FILE__).realpath
rails_root = File.expand_path("../..", expanded_path)
bundle_config_file = "#{rails_root}/.bundle/config"
if File.exist?(bundle_config_file)
  require "yaml"
  bundle_config = YAML.load_file(bundle_config_file)
  if bundle_config["BUNDLE_PATH"]
    bundle_gem_path = File.expand_path("#{bundle_config["BUNDLE_PATH"]}/#{RUBY_ENGINE}/#{RbConfig::CONFIG["ruby_version"]}", rails_root)
    Gem::Specification.dirs = (Array(Gem.path) + [bundle_gem_path]).uniq
  end
end

gem "redis"

require "base64"
require "json"
require "redis"

SCAN_BATCH_SIZE = 1000

# this purposely has no config so that it can spin up quickly;
# just copy this script elsewhere and modify if you have a
# non-default configuration
cache = Redis.new

data = JSON.parse($stdin.read)
data.each do |event|
  id = event["ID"]
  # only invalidate events we haven't seen yet
  next unless cache.zadd("consul_events", Time.now.utc.to_f, id, nx: true)

  key = Base64.decode64(event["Payload"])
  # Special-case flushall
  if key == "FLUSHDB"
    cache.flushdb
    # Readd this event so that it doesn't get forgotten about in the future
    cache.zadd("consul_events", Time.now.utc.to_f, id, nx: true)
  elsif key.start_with?("DELETE_MATCHED|")
    pattern = key.split("|", 2)[1]
    # Adapted from rails redis_cache_store.rb
    cursor = "0"
    loop do
      cursor, keys = cache.scan(cursor, match: pattern, count: SCAN_BATCH_SIZE)
      cache.del(*keys) unless keys.empty?
      break if cursor == "0"
    end
  elsif key.start_with?("DELETE|")
    target = key.split("|", 2)[1]
    cache.del(target)
  else
    # deprecated
    cache.del(key)
  end
end
# limit the set to the 256 newest elements
cache.zremrangebyrank("consul_events", 0, -257)
