Class: ZkService::Vendor

Inherits:
Object
  • Object
show all
Includes:
ZkService
Defined in:
lib/zk_service.rb

Constant Summary

Constants included from ZkService

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ZkService

check_namespaces, configure, create_connection

Constructor Details

#initialize(host, port) ⇒ Vendor

Returns a new instance of Vendor.



92
93
94
95
96
# File 'lib/zk_service.rb', line 92

def initialize( host , port )
  @zookeeper_server_addresses ||= []
  @zookeeper_namespace ||= "/"
  @zookeeper_server_addresses << "#{host}:#{port}"
end

Instance Attribute Details

#base_detailsObject (readonly)

Returns the value of attribute base_details.



89
90
91
# File 'lib/zk_service.rb', line 89

def base_details
  @base_details
end

#zkObject (readonly)

Returns the value of attribute zk.



89
90
91
# File 'lib/zk_service.rb', line 89

def zk
  @zk
end

#zookeeper_namespaceObject

Returns the value of attribute zookeeper_namespace.



90
91
92
# File 'lib/zk_service.rb', line 90

def zookeeper_namespace
  @zookeeper_namespace
end

#zookeeper_server_addressesObject

Returns the value of attribute zookeeper_server_addresses.



90
91
92
# File 'lib/zk_service.rb', line 90

def zookeeper_server_addresses
  @zookeeper_server_addresses
end

Instance Method Details

#get_data(child_path) ⇒ Object



119
120
121
122
123
124
# File 'lib/zk_service.rb', line 119

def get_data(child_path)
  #need better error handling
  return false if @service_path.empty?
  path = @service_path + "/" + child_path
  @zk.get(:path => path )[:data] 
end

#vend(service_name) ⇒ Object

return a child ephemeral node



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zk_service.rb', line 103

def vend(service_name)
  @zk = ZkService.create_connection(@zookeeper_server_addresses)      
  @service_path = @zookeeper_namespace + service_name 

  if zk_path_exists(@service_path)
    response = @zk.get_children(:path => @service_path)
    if !response[:children].nil?
      response[:children].choice
    else
      raise ZkServiceVendorError.new("ZK ERROR: No nodes under #{@service_path}'") 
    end 
  else
    raise ZkServiceVendorError.new("ZK ERROR: Could not find #{@service_path}'") 
  end
end

#zk_path_exists(path) ⇒ Object



98
99
100
# File 'lib/zk_service.rb', line 98

def zk_path_exists(path)
  path.nil?? false : @zk.stat(:path => path)[:stat].exists
end