Class: Consul::Client::Agent
- Inherits:
-
Object
- Object
- Consul::Client::Agent
- Includes:
- Base
- Defined in:
- lib/consul/client/agent.rb
Defined Under Namespace
Modules: HealthCheck, Service
Instance Method Summary collapse
-
#check(id) ⇒ Object
Public: Returns a check by a given id.
- #checks ⇒ Object
-
#deregister(entity) ⇒ Object
Public: deregisters an existing ConsulHealthCheck or ConsulService.
-
#fail(check) ⇒ Object
Public: Fails a health check.
-
#maintenance(enable, service = nil, reason = nil) ⇒ Object
Public: Enter maintenance mode.
-
#pass(check) ⇒ Object
Public: Pass a health check.
-
#register(entity) ⇒ Object
Public: Registers either a service or a Health check with configured consul agent.
-
#service(id) ⇒ Object
Public: Returns the service that has the associated name.
-
#service_check(service_id) ⇒ Object
Public: Returns a Health Check for a specific service.
-
#services ⇒ Object
Public: Returns all the services registered with this Agent.
-
#warn(check) ⇒ Object
Public: Warn a health check.
Methods included from Base
Instance Method Details
#check(id) ⇒ Object
Public: Returns a check by a given id. Returns: nil if no such check exists, or the check instance with the correct id
49 50 51 52 |
# File 'lib/consul/client/agent.rb', line 49 def check(id) c = checks c.keep_if {|c| c.check_id == id}.first unless c.nil? end |
#checks ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/consul/client/agent.rb', line 35 def checks begin resp = _get build_agent_url('checks') rescue @logger.warn('Unable to request all the checks on this through the HTTP API') return nil end # Consul returns id => ConsulServiceObjects. s_hash = JSON.parse(resp) s_hash.keys.map { |n| Consul::Model::HealthCheck.new.extend(Consul::Model::HealthCheck::Representer).from_hash(s_hash[n]) } end |
#deregister(entity) ⇒ Object
Public: deregisters an existing ConsulHealthCheck or ConsulService
entity - ConsulHealthCheck or ConsulService to unregister from this
Returns - the HTTP Response
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/consul/client/agent.rb', line 101 def deregister(entity) unless entity.nil? raise TypeError unless entity.kind_of? Consul::Model::HealthCheck or entity.kind_of? Consul::Model::Service case entity when Consul::Model::HealthCheck url = build_check_url('deregister') else url = build_service_url('deregister') end _get "#{url}/#{entity.id}" end end |
#fail(check) ⇒ Object
Public: Fails a health check
check - Consul::Model::HealthCheck to pass. Cannot be nil or wrong type
134 135 136 |
# File 'lib/consul/client/agent.rb', line 134 def fail(check) update_check_status(check, 'fail') end |
#maintenance(enable, service = nil, reason = nil) ⇒ Object
Public: Enter maintenance mode.
enable - Flag to indicate to enable maintanence mode or not service - Set maintanence for a particular service is set. reason - Optional reason.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/consul/client/agent.rb', line 143 def maintenance(enable, service = nil, reason = nil) if service.nil? url = build_agent_url('maintenance') else if service.instance_of?(Consul::Model::Service) service = service.id end raise ArgumentError.new "Unable to create request for #{service}" unless service.respond_to?(:to_str) url = build_service_url("maintenance/#{service}") end params = {:enable => enable} params[:reason] = reason unless reason.nil? _get url, params end |
#pass(check) ⇒ Object
Public: Pass a health check.
check - Consul::Model::HealthCheck to pass. Cannot be nil or wrong type
118 119 120 |
# File 'lib/consul/client/agent.rb', line 118 def pass(check) update_check_status(check, 'pass') end |
#register(entity) ⇒ Object
Public: Registers either a service or a Health check with configured consul agent.
entity - Consul::Model::Service or Consul::Model::HealthCheck instance.
Example
agent = Consul::Client::Agent.new('dc1')
# Register a service
agent.register(Consul::Client::Agent::Service.for_name('cat'))
# Register a HealthCheck
agent.register(Consul::Client::HealthCheck.ttl('my_check_name', '15m'))
# Register a service with a Consul Health Check
agent.register(Consul::Client::Agent::Service.for_name('cat', Consul::Client::Agent::Service.ttl_health_check('15m')))
Returns true upon success, false upon failure
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/consul/client/agent.rb', line 75 def register(entity) raise TypeError unless entity.kind_of? Consul::Model::HealthCheck or entity.kind_of? Consul::Model::Service case entity when Consul::Model::HealthCheck return register_with_backoff(build_check_url('register'), entity.extend(Consul::Model::HealthCheck::Representer), 0, 3) else entity = entity.extend(Consul::Model::Service::Representer) success = register_with_backoff(build_service_url('register'), entity, 0, 3) if success @logger.info("Successfully registered service #{entity.name}.") unless entity.check.nil? # Pass the first health check c = check("service:#{entity.name}") @logger.info("Updating status for health check #{c.check_id} to \"pass\".") _get build_check_status_url(c.check_id, 'pass') end end return success end end |
#service(id) ⇒ Object
Public: Returns the service that has the associated name.
Returns: ConsulService if exists, nil if no service by this name exists.
30 31 32 33 |
# File 'lib/consul/client/agent.rb', line 30 def service(id) ss = services ss.keep_if {|s| s.id == id}.first unless ss.nil? end |
#service_check(service_id) ⇒ Object
Public: Returns a Health Check for a specific service.
service_id - The ID of the service.
57 58 59 |
# File 'lib/consul/client/agent.rb', line 57 def service_check(service_id) check("service:#{service_id}") end |
#services ⇒ Object
Public: Returns all the services registered with this Agent.
Returns: An array of all the ConsulService(s) registered on this agent.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/consul/client/agent.rb', line 15 def services begin resp = _get build_agent_url('services') rescue @logger.warn('Unable to request all the services on this through the HTTP API') return nil end # Consul returns id => ConsulServiceObjects. s_hash = JSON.parse(resp) s_hash.keys.map { |n| Consul::Model::Service.new.extend(Consul::Model::Service::Representer).from_hash(s_hash[n]) } end |
#warn(check) ⇒ Object
Public: Warn a health check
check - Consul::Model::HealthCheck to pass. Cannot be nil or wrong type
126 127 128 |
# File 'lib/consul/client/agent.rb', line 126 def warn(check) update_check_status(check, 'warn') end |