Module: ConsulWatcher
- Defined in:
- lib/consul_watcher.rb,
lib/consul_watcher/diff.rb,
lib/consul_watcher/filters.rb,
lib/consul_watcher/storage/disk.rb,
lib/consul_watcher/destination/jq.rb,
lib/consul_watcher/storage/consul.rb,
lib/consul_watcher/watch_type/key.rb,
lib/consul_watcher/destination/amqp.rb,
lib/consul_watcher/watch_type/checks.rb
Overview
This will be a module to store previous consul watch json to compare with previous watch data
Defined Under Namespace
Modules: Destination, Storage, WatchType
Classes: Diff, Filters
Class Method Summary
collapse
Class Method Details
.assemble(config) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/consul_watcher.rb', line 27
def self.assemble(config)
@storage = get_storage(config['storage'])
@watch_type = get_watch_type(config['watch_type'])
@destination = get_destination(config['destination'])
@watch_type.filters = ConsulWatcher::Filters.new(config['watch_type'] || {})
@watch_type.filters.add_filters(@storage.get_filters)
end
|
.classname_to_file(classname) ⇒ Object
Dynamically require handler class from passed in handler class
55
56
57
|
# File 'lib/consul_watcher.rb', line 55
def self.classname_to_file(classname)
classname.gsub('::', '/').gsub(/([a-zA-Z])([A-Z])/, '\1_\2').downcase
end
|
.get_destination(destination_config) ⇒ Object
48
49
50
51
52
|
# File 'lib/consul_watcher.rb', line 48
def self.get_destination(destination_config)
classname = destination_config['classname']
require classname_to_file(classname)
Object.const_get(classname).new(destination_config)
end
|
.get_storage(storage_config) ⇒ Object
36
37
38
39
40
|
# File 'lib/consul_watcher.rb', line 36
def self.get_storage(storage_config)
classname = storage_config['classname']
require classname_to_file(classname)
Object.const_get(classname).new(storage_config)
end
|
.get_watch_type(watch_type_config) ⇒ Object
42
43
44
45
46
|
# File 'lib/consul_watcher.rb', line 42
def self.get_watch_type(watch_type_config)
classname = watch_type_config['classname']
require classname_to_file(classname)
Object.const_get(classname).new(watch_type_config)
end
|
.watch(config) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/consul_watcher.rb', line 11
def self.watch(config)
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
assemble(config)
current_watch_json = $stdin.read
previous_watch_json = @storage.fetch
changes = @watch_type.get_changes(previous_watch_json, current_watch_json)
changes.each do |change|
@destination.send(change)
end
logger.info("Processed #{changes.size} change#{'s' unless changes.size == 1}")
@storage.push(current_watch_json) unless changes.empty?
end
|