forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsume_consul_events
executable file
·47 lines (42 loc) · 1.51 KB
/
consume_consul_events
1
2
3
4
5
6
7
8
9
10
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
#!/usr/bin/env ruby
# 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'
# 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.load(STDIN.read)
data.each do |event|
id = event['ID']
# only invalidate events we haven't seen yet
if 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)
else
cache.del(key)
end
end
end
# limit the set to the 256 newest elements
cache.zremrangebyrank("consul_events", 0, -257)