Class: Etcd::Node

Inherits:
Object
  • Object
show all
Includes:
Constants, Requestable
Defined in:
lib/etcd/node.rb

Constant Summary

Constants included from Constants

Constants::S_ACTION, Constants::S_AND, Constants::S_DIR, Constants::S_EXPIRATION, Constants::S_INDEX, Constants::S_KEY, Constants::S_KEYS, Constants::S_LOCATION, Constants::S_NEW_KEY, Constants::S_PREV_VALUE, Constants::S_SLASH, Constants::S_TTL, Constants::S_VALUE, Constants::S_WATCH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Requestable

#http_client, #request, #request_data, #reset_http_client!

Methods included from Loggable

#logger, #reset_logger!

Constructor Details

#initialize(opts = {}) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
16
# File 'lib/etcd/node.rb', line 10

def initialize(opts={})
  check_required(opts)
  @name   = opts[:name]
  @etcd   = URI.decode(opts[:etcd])
  @raft   = URI.decode(opts[:raft])
  @status = :unknown
end

Instance Attribute Details

#etcdObject

Returns the value of attribute etcd.



5
6
7
# File 'lib/etcd/node.rb', line 5

def etcd
  @etcd
end

#is_leaderObject

Returns the value of attribute is_leader.



8
9
10
# File 'lib/etcd/node.rb', line 8

def is_leader
  @is_leader
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/etcd/node.rb', line 5

def name
  @name
end

#raftObject

Returns the value of attribute raft.



5
6
7
# File 'lib/etcd/node.rb', line 5

def raft
  @raft
end

#statusObject

possible values: :unknown, :running, :down



7
8
9
# File 'lib/etcd/node.rb', line 7

def status
  @status
end

Instance Method Details

#check_required(opts) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
# File 'lib/etcd/node.rb', line 18

def check_required(opts)
  raise ArgumentError, "etcd URL is required!" unless opts[:etcd]
end

#inspectObject



36
37
38
# File 'lib/etcd/node.rb', line 36

def inspect
  %Q(<#{self.class} - #{name_with_status} - #{etcd}>)
end

#leader_uriObject



32
33
34
# File 'lib/etcd/node.rb', line 32

def leader_uri
  "#{@etcd}/v1/leader"
end

#name_with_statusObject



40
41
42
43
# File 'lib/etcd/node.rb', line 40

def name_with_status
  print_status = @is_leader ? "leader" : status
  "#{name} (#{print_status})"
end

#update_statusObject



22
23
24
25
26
27
28
29
30
# File 'lib/etcd/node.rb', line 22

def update_status
  begin
    response   = request(:get, leader_uri)
    @status    = :running
    @is_leader = (response.body == @raft)
  rescue HTTPClient::TimeoutError, Errno::ECONNREFUSED => e
    @status = :down
  end
end