Sha256: 32e2166f96f5d07c5de38042ea0f551b1fa5966783b9f84daad4df41b4a06def

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'singleton'
require 'concurrent'

module Dryad
  module Consul
    class ConfigProvider < Dryad::Core::ConfigProvider
      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
            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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dryad-consul-0.3.2 lib/dryad/consul/config_provider.rb
dryad-consul-0.3.0 lib/dryad/consul/config_provider.rb