Class: Odania::Consul::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/odania/consul.rb

Instance Method Summary collapse

Instance Method Details

#build_config(plugin_name, plugin_instance_name, ip, tags = [], port = 80) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/odania/consul.rb', line 101

def build_config(plugin_name, plugin_instance_name, ip, tags=[], port=80)
	{
		'id' => plugin_instance_name,
		'name' => plugin_name,
		'tags' => tags,
		'address' => ip,
		'port' => port,
		'token' => plugin_instance_name,
		'checks' => [
			{
				'id' => plugin_name,
				'name' => "HTTP on port #{port}",
				'http' => "http://#{ip}:#{port}/health",
				'interval' => '10s',
				'timeout' => '1s'
			}
		]
	}
end

#deregister(name) ⇒ Object



97
98
99
# File 'lib/odania/consul.rb', line 97

def deregister(name)
	Diplomat::Service.deregister name
end

#get(key, scope = :first) ⇒ Object



85
86
87
# File 'lib/odania/consul.rb', line 85

def get(key, scope=:first)
	Diplomat::Service.get(key, scope)
end

#get_allObject



57
58
59
60
61
62
63
64
# File 'lib/odania/consul.rb', line 57

def get_all
	services = {}
	Diplomat::Service.get_all.each_pair do |key, _value|
		services[key.to_s] = get_all_for(key)
	end
	$logger.info "SERVICES: #{JSON.pretty_generate services}" if $debug
	services
end

#get_all_for(plugin_name) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/odania/consul.rb', line 66

def get_all_for(plugin_name)
	begin
		instances = get(plugin_name, :all)
	rescue
		instances = []
	end
	instances.is_a?(Array) ? instances : [instances]
end

#get_core_serviceObject

TODO Is there an easier way to get the first service tagges with “core-backend”?



76
77
78
79
80
81
82
83
# File 'lib/odania/consul.rb', line 76

def get_core_service
	core_backends = []
	Diplomat::Service.get_all.each_pair do |key, tags|
		core_backends << key if tags.include? 'core-backend'
	end

	get(core_backends.shuffle.first)
end

#register(consul_config) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/odania/consul.rb', line 89

def register(consul_config)
	if Diplomat::Service.register consul_config
		$logger.info 'Service registered' if $debug
	else
		$logger.error 'Error registering service'
	end
end