Class: Villein::Client
- Inherits:
-
Object
- Object
- Villein::Client
- Defined in:
- lib/villein/client.rb
Overview
Villein::Client allows you to order existing serf agent. You will need RPC address and agent name to command.
Direct Known Subclasses
Defined Under Namespace
Classes: InsufficientVersionError, LengthExceedsLimitError, SerfCommandNotFound, SerfConnectionError, SerfError
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rpc_addr ⇒ Object
readonly
Returns the value of attribute rpc_addr.
-
#serf ⇒ Object
readonly
Returns the value of attribute serf.
-
#silence ⇒ Object
writeonly
Sets the attribute silence.
Instance Method Summary collapse
-
#delete_tag(key) ⇒ Object
Remove tag from the agent.
- #event(name, payload, coalesce: true) ⇒ Object
- #force_leave(node) ⇒ Object
-
#get_tags ⇒ Object
Get tag from the agent.
-
#info ⇒ Object
Returns a result of ‘serf info`.
-
#initialize(rpc_addr, name: nil, serf: 'serf', silence: true) ⇒ Client
constructor
A new instance of Client.
- #join(addr, replay: false) ⇒ Object
- #leave ⇒ Object
- #members(status: nil, name: nil, tags: {}) ⇒ Object
- #query(name, payload, node: nil, tag: nil, timeout: nil, no_ack: false) ⇒ Object
-
#set_tag(key, val) ⇒ Object
Set tag to the agent.
- #silence? ⇒ Boolean
-
#tags ⇒ Object
Returns Villein::Tags object for the current agent.
Constructor Details
#initialize(rpc_addr, name: nil, serf: 'serf', silence: true) ⇒ Client
Returns a new instance of Client.
29 30 31 32 33 34 35 36 |
# File 'lib/villein/client.rb', line 29 def initialize(rpc_addr, name: nil, serf: 'serf', silence: true) @rpc_addr = rpc_addr @name = name @serf = serf @silence = true retrieve_name unless @name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
41 42 43 |
# File 'lib/villein/client.rb', line 41 def name @name end |
#rpc_addr ⇒ Object (readonly)
Returns the value of attribute rpc_addr.
41 42 43 |
# File 'lib/villein/client.rb', line 41 def rpc_addr @rpc_addr end |
#serf ⇒ Object (readonly)
Returns the value of attribute serf.
41 42 43 |
# File 'lib/villein/client.rb', line 41 def serf @serf end |
#silence=(value) ⇒ Object (writeonly)
Sets the attribute silence
39 40 41 |
# File 'lib/villein/client.rb', line 39 def silence=(value) @silence = value end |
Instance Method Details
#delete_tag(key) ⇒ Object
Remove tag from the agent. Using Villein::Client#tags method is recommended. It provides high-level API via Villein::Tags
.
144 145 146 |
# File 'lib/villein/client.rb', line 144 def delete_tag(key) call_serf 'tags', '-delete', key end |
#event(name, payload, coalesce: true) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/villein/client.rb', line 52 def event(name, payload, coalesce: true) = [] unless coalesce << '-coalesce=false' end call_serf 'event', *, name, payload end |
#force_leave(node) ⇒ Object
106 107 108 |
# File 'lib/villein/client.rb', line 106 def force_leave(node) call_serf 'force-leave', node end |
#get_tags ⇒ Object
Get tag from the agent. Using Villein::Client#tags method is recommended. It provides high-level API via Villein::Tags
.
136 137 138 139 |
# File 'lib/villein/client.rb', line 136 def me = members(name: self.name)[0] me["tags"] end |
#info ⇒ Object
Returns a result of ‘serf info`. This may raise InsufficientVersionError when `serf info` is not supported.
46 47 48 49 50 |
# File 'lib/villein/client.rb', line 46 def info JSON.parse call_serf('info', '-format', 'json') rescue SerfCommandNotFound raise InsufficientVersionError, 'serf v0.6.0 or later is required to run `serf info`.' end |
#join(addr, replay: false) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/villein/client.rb', line 92 def join(addr, replay: false) = [] if replay << '-replay' end call_serf 'join', *, addr end |
#leave ⇒ Object
102 103 104 |
# File 'lib/villein/client.rb', line 102 def leave call_serf 'leave' end |
#members(status: nil, name: nil, tags: {}) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/villein/client.rb', line 110 def members(status: nil, name: nil, tags: {}) = ['-format', 'json'] .push('-status', status.to_s) if status .push('-name', name.to_s) if name .each do |tag, val| .push('-tag', "#{tag}=#{val}") end json = call_serf('members', *) response = JSON.parse(json) response["members"] end |
#query(name, payload, node: nil, tag: nil, timeout: nil, no_ack: false) ⇒ Object
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 |
# File 'lib/villein/client.rb', line 62 def query(name, payload, node: nil, tag: nil, timeout: nil, no_ack: false) # TODO: version check = ['-format', 'json'] if node node = [node] unless node.respond_to?(:each) node.each do |n| << "-node=#{n}" end end if tag tag = [tag] unless tag.respond_to?(:each) tag.each do |t| << "-tag=#{t}" end end if timeout << "-timeout=#{timeout}" end if no_ack << "-no-ack" end out = call_serf('query', *, name, payload) JSON.parse(out) end |
#set_tag(key, val) ⇒ Object
Set tag to the agent. Using Villein::Client#tags method is recommended. It provides high-level API via Villein::Tags
.
151 152 153 |
# File 'lib/villein/client.rb', line 151 def set_tag(key, val) call_serf 'tags', '-set', "#{key}=#{val}" end |
#silence? ⇒ Boolean
38 |
# File 'lib/villein/client.rb', line 38 def silence?() !!@silence; end |