Module: ZkService::Publisher

Extended by:
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

Instance Attribute Details

#provider_pathObject

Returns the value of attribute provider_path.



44
45
46
# File 'lib/zk_service.rb', line 44

def provider_path
  @provider_path
end

#zkObject (readonly)

Returns the value of attribute zk.



43
44
45
# File 'lib/zk_service.rb', line 43

def zk
  @zk
end

#zookeeper_namespaceObject

Returns the value of attribute zookeeper_namespace.



44
45
46
# File 'lib/zk_service.rb', line 44

def zookeeper_namespace
  @zookeeper_namespace
end

#zookeeper_server_addressesObject

Returns the value of attribute zookeeper_server_addresses.



44
45
46
# File 'lib/zk_service.rb', line 44

def zookeeper_server_addresses
  @zookeeper_server_addresses
end

Instance Method Details

#zk_path_exists(path) ⇒ Object



56
57
58
# File 'lib/zk_service.rb', line 56

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

#zk_publish(data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zk_service.rb', line 60

def zk_publish( data )
  #register service if not already registered
  if !zk_path_exists(@zookeeper_namespace)
    response = @zk.create(:path => @zookeeper_namespace)
    raise ZkServicePublisherError.new("ZK ERROR: Could not create #{service}'") if response[:rc] != Zookeeper::ZOK
  end
        
  path = @zookeeper_namespace + '/provider-'
  response = (!zk_path_exists(path)) ? @zk.create(:path => path, :data => data.to_json, :sequence => true, :ephemeral => true) : false
  raise ZkServicePublisherError.new("ZK ERROR: Could not publish path '#{path}'") if response[:rc] != Zookeeper::ZOK

  @provider_path = response[:path]
  response
end

#zk_reset_host(host, port) ⇒ Object



80
81
82
83
# File 'lib/zk_service.rb', line 80

def zk_reset_host( host, port )
  @zookeeper_server_addresses = []
  @zookeeper_server_addresses << "#{host}:#{port}"
end

#zk_service_details(host, port, service_name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/zk_service.rb', line 46

def zk_service_details(host, port, service_name)
  @zookeeper_server_addresses ||= []
  @provider_path ||= ""
  @zookeeper_server_addresses = !@zookeeper_server_addresses.empty? || ZkService.configure( host, port )
  @zk = ZkService.create_connection(@zookeeper_server_addresses)

  @zookeeper_namespace = ZkService.check_namespaces(@zookeeper_namespace)
  @zookeeper_namespace += service_name
end

#zk_unpublishObject



75
76
77
78
# File 'lib/zk_service.rb', line 75

def zk_unpublish
  @zk.delete(:path => @provider_path)
  raise ZkServicePublisherError.new("ZK ERROR: Could not unpublish from path '#{@provider_path}'") if zk_path_exists(@provider_path)
end