Class: Shatter::Service::Discovery

Inherits:
Object
  • Object
show all
Defined in:
lib/shatter/service/discovery.rb

Class Method Summary collapse

Class Method Details

.connection_poolObject



8
9
10
# File 'lib/shatter/service/discovery.rb', line 8

def connection_pool
  @connection_pool ||= ZK::Pool::Bounded.new(Shatter::Config.zookeeper_host)
end

.deregister_service(service_url) ⇒ Object



12
13
14
15
16
17
# File 'lib/shatter/service/discovery.rb', line 12

def deregister_service(service_url)
  zk = connection_pool.checkout
  key = Shatter::Util.instances_key
  zk.delete("#{key}/#{service_url}") if zk.exists?("#{key}/#{service_url}")
  connection_pool.checkin(zk)
end

.my_hostObject



47
48
49
50
# File 'lib/shatter/service/discovery.rb', line 47

def my_host
  my_ip = ENV["HOST_NAME"] || "localhost"
  "#{my_ip}:#{Shatter::Config.service_port}"
end

.populate_result_location(uuid) ⇒ Object



41
42
43
44
45
# File 'lib/shatter/service/discovery.rb', line 41

def populate_result_location(uuid)
  zk = connection_pool.checkout
  zk.create(Util.zookeeper_response_key(uuid), my_host)
  connection_pool.checkin(zk)
end

.register_service(service_url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/shatter/service/discovery.rb', line 19

def register_service(service_url)
  Shatter.logger.info "Registering #{service_url} to zookeeper"
  zk = connection_pool.checkout
  key = Shatter::Util.instances_key
  unless zk.exists?("#{key}/#{service_url}")
    created = zk.create("#{key}/#{service_url}")
    Shatter.logger.info "Registered #{created}"
  end
  connection_pool.checkin(zk)
end

.service_instance_urlObject



30
31
32
# File 'lib/shatter/service/discovery.rb', line 30

def service_instance_url
  service_instance_urls.sample
end

.service_instance_url_for_uuid(uuid) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/shatter/service/discovery.rb', line 52

def service_instance_url_for_uuid(uuid)
  key = Util.zookeeper_response_key(uuid)
  zk = connection_pool.checkout
  druby_instance_url = zk.get(key)[0] if zk.exists?(key)
  connection_pool.checkin(zk)
  druby_instance_url
end

.service_instance_urlsObject



34
35
36
37
38
39
# File 'lib/shatter/service/discovery.rb', line 34

def service_instance_urls
  zk = connection_pool.checkout
  urls = zk.children(Shatter::Util.instances_key)
  connection_pool.checkin(zk)
  urls
end