lib/dryad/consul/config_provider.rb in dryad-consul-0.2.7 vs lib/dryad/consul/config_provider.rb in dryad-consul-0.3.0
- old
+ new
@@ -1,14 +1,44 @@
+require 'singleton'
+require 'concurrent'
+
module Dryad
module Consul
class ConfigProvider < Dryad::Core::ConfigProvider
- class << self
- def load(path, listener = nil)
- config = Dryad::Consul::KeyValueClient.get(path)
- if config.nil?
- raise Dryad::Core::ConfigurationNotFound, path
+ include Singleton
+ WATCHER_EXECUTION_INTERVAL = 5 * 60
+
+ def initialize
+ @timers = {}
+ end
+
+ def load(path, observer = nil)
+ config = Dryad::Consul::KeyValueClient.get(path)
+ if config.nil?
+ nil
+ else
+ add_observer(observer, path)
+ Dryad::Core::ConfigDesc.new(path, config.Value, config.ModifyIndex)
+ end
+ end
+
+ private
+ def add_observer(observer, path)
+ if observer && observer.is_a?(Dryad::Core::Observer)
+ if @timers.has_key?(path)
+ @timers[path].add_observer(observer)
else
- Dryad::Core::ConfigDesc.new(path, config.Value, config.ModifyIndex)
+ timer = Concurrent::TimerTask.new(execution_interval: WATCHER_EXECUTION_INTERVAL) do
+ config = Dryad::Consul::KeyValueClient.get(path)
+ if config.nil?
+ nil
+ else
+ Dryad::Core::ConfigDesc.new(path, config.Value, config.ModifyIndex)
+ end
+ end
+ timer.add_observer(observer)
+ timer.execute
+ @timers[path] = timer
end
end
end
end
end