Class: Boutons::URI::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/boutons/uri.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Registry

Returns a new instance of Registry.



7
8
9
# File 'lib/boutons/uri.rb', line 7

def initialize uri
  @uri  = uri
end

Instance Method Details

#methodObject



13
14
15
# File 'lib/boutons/uri.rb', line 13

def method
  @uri.scheme.split("+")[0]
end

#serviceObject



10
11
12
# File 'lib/boutons/uri.rb', line 10

def service
  @uri.scheme
end

#zkObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/boutons/uri.rb', line 36

def zk
  begin
    # return existing, working zookeeper connection
    Resource.zk
  rescue
    queue   = Queue.new
    threads = []
    zk_hosts.each do |zk_host|
      threads << Thread.new do
        # connect, push connection to queue
        queue << [zk_host,ZK.new(zk_host)]
      end
    end
    host,zk = queue.pop
    # set first working host as host arrays
    Config.registry.zookeeper.hosts = [host]
    # register connection
    Resource.add :zk, zk
    # Add later connections if there are any
    Thread.new do
      threads.each{|t|t.join}
      host,zk = queue.pop
      begin
        zk.ping?
        Config.registry.zookeeper.hosts << host
      rescue Zookeeper::Exceptions::ContinuationTimeoutError
        nil
      end
    end
    retry
  end
end

#zk_hostsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/boutons/uri.rb', line 16

def zk_hosts
  @hosts = []
  if @uri.host
    if ::URI.unescape(@uri.host)=~/\{(.*)\}/
      uris = ::URI.unescape(@uri.host).split(",").select do |x|
        x=~/\{(.*)\}/
      end.collect do |x|
        URI "#{@uri.scheme}://#{x[1..-2].reverse.sub(';',':').reverse}/"
      end
      uris.each do |uri|
        @hosts << "#{uri.host}:#{uri.port || 2181 }"
      end
    else
      @hosts << "#{@uri.host}:#{@uri.port || 2181 }"
    end
  else
    @hosts = Config.registry.send(method).hosts
  end
  @hosts
end

#zk_pathObject



68
69
70
# File 'lib/boutons/uri.rb', line 68

def zk_path
  ::URI.unescape(@uri.path).split("#",2)[0]
end