Class: EtcdDiscovery::Registrar
- Inherits:
-
Object
- Object
- EtcdDiscovery::Registrar
- Defined in:
- lib/etcd-discovery/registrar.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#watcher ⇒ Object
readonly
Returns the value of attribute watcher.
Instance Method Summary collapse
- #client ⇒ Object
- #config ⇒ Object
- #host_key ⇒ Object
-
#initialize(service, host) ⇒ Registrar
constructor
A new instance of Registrar.
- #register ⇒ Object
- #service_key ⇒ Object
- #service_params ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(service, host) ⇒ Registrar
Returns a new instance of Registrar.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/etcd-discovery/registrar.rb', line 24 def initialize(service, host) @logger = Logger.new($stdout) if host.is_a? Hash if host.has_key?("uuid") host_uuid = "#{SecureRandom.uuid}-#{host["private_hostname"]}" host = host.merge("uuid" => host_uuid) end @host = Host.new host elsif host.is_a? EtcdDiscovery::Host if host.attributes.has_key?("uuid") host.attributes["uuid"] = "#{SecureRandom.uuid}-#{host.attributes["private_hostname"]}" end @host = host else raise TypeError, "host should be a Hash or a Etcd::Host, is a #{host.class}" end # This attribute is later used when instantiating EtcdDiscovery::Service. We want this value to be the content of `service`, always. @host.attributes["service_name"] = service @service = EtcdDiscovery::Service.new service_params @state = :new @user = @host.attributes["user"] @password = @host.attributes["password"] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
19 20 21 |
# File 'lib/etcd-discovery/registrar.rb', line 19 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
22 23 24 |
# File 'lib/etcd-discovery/registrar.rb', line 22 def password @password end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
20 21 22 |
# File 'lib/etcd-discovery/registrar.rb', line 20 def service @service end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
16 17 18 |
# File 'lib/etcd-discovery/registrar.rb', line 16 def state @state end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
17 18 19 |
# File 'lib/etcd-discovery/registrar.rb', line 17 def thread @thread end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
21 22 23 |
# File 'lib/etcd-discovery/registrar.rb', line 21 def user @user end |
#watcher ⇒ Object (readonly)
Returns the value of attribute watcher.
18 19 20 |
# File 'lib/etcd-discovery/registrar.rb', line 18 def watcher @watcher end |
Instance Method Details
#client ⇒ Object
111 112 113 |
# File 'lib/etcd-discovery/registrar.rb', line 111 def client config.client end |
#config ⇒ Object
115 116 117 |
# File 'lib/etcd-discovery/registrar.rb', line 115 def config EtcdDiscovery.config end |
#host_key ⇒ Object
119 120 121 |
# File 'lib/etcd-discovery/registrar.rb', line 119 def host_key "/services/#{@service.attributes["name"]}/#{@host.attributes["uuid"]}" end |
#register ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/etcd-discovery/registrar.rb', line 51 def register if @state == :started @logger.warn "#{@service} is already registered" return end @state = :started service_value = @service.to_json # Do not start credentials synchro if the service is not public or has no credentials if @service.attributes["public"] && (@service.attributes["user"].present? || @service.attributes["password"].present?) @watcher = Thread.new do @logger.warn "Watcher #{@service.attributes["name"]} started" index = 0 while @state == :started begin resp = client.watch service_key, index: index rescue => e @logger.warn "Fail to watch #{service_key}: #{e}, #{e.}, #{e.class}" index = 0 sleep(config.register_ttl / 2) next end value = JSON.parse resp.node.value @user = value["user"] @password = value["password"] @host.set_credentials user, password @service.set_credentials user, password index = resp.etcd_index end end end client.set(service_key, value: service_value) @thread = Thread.new do @logger.warn "Register '#{@service}' started" while @state == :started value = @host.to_json begin client.set(host_key, value: value, ttl: config.register_ttl) rescue => e @logger.warn "Fail to set #{service_key}: #{e}, #{e.}, #{e.class}" end sleep config.register_renew end @logger.warn "Register '#{@service}' stopped" end self end |
#service_key ⇒ Object
123 124 125 |
# File 'lib/etcd-discovery/registrar.rb', line 123 def service_key "/services_infos/#{@service.attributes["name"]}" end |
#service_params ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/etcd-discovery/registrar.rb', line 127 def service_params params = { "name" => @host.attributes["service_name"], "critical" => @host.attributes["critical"], "user" => @host.attributes["user"], "password" => @host.attributes["password"], "public" => @host.attributes["public"] } params["hostname"] = @host.attributes["name"] if params["public"] params["ports"] = @host.attributes["ports"] if params["public"] params end |
#stop ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/etcd-discovery/registrar.rb', line 103 def stop raise InvalidStateError.new(@state, :started) if @state != :started @logger.debug "Set state to :stopped" @state = :stopped @logger.debug "Delete #{host_key}" client.delete(host_key) end |