Class: BoringServices::ServiceLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/boring_services/service_locator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ ServiceLocator

Returns a new instance of ServiceLocator.



7
8
9
# File 'lib/boring_services/service_locator.rb', line 7

def initialize(config = nil)
  @config = config || Configuration.load
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/boring_services/service_locator.rb', line 5

def config
  @config
end

Instance Method Details

#all_ips(service_name) ⇒ Object

Get all connection IPs for a service (prefers private_ip for each)



38
39
40
# File 'lib/boring_services/service_locator.rb', line 38

def all_ips(service_name)
  hosts_for(service_name).map { |h| connection_ip(h) }
end

#host_by_label(service_name, label) ⇒ Object

Get host by exact label match



21
22
23
24
25
26
27
# File 'lib/boring_services/service_locator.rb', line 21

def host_by_label(service_name, label)
  hosts = hosts_for(service_name)
  host_entry = hosts.find { |h| h[:label] == label.to_s }
  return nil unless host_entry

  connection_ip(host_entry)
end

#hosts_by_label(service_name) ⇒ Object

Get all hosts as a hash keyed by label { "redis-eu-gcp" => "10.8.0.10", "redis-us-aws" => "10.8.0.60" }



31
32
33
34
35
# File 'lib/boring_services/service_locator.rb', line 31

def hosts_by_label(service_name)
  hosts_for(service_name).each_with_object({}) do |h, hash|
    hash[h[:label]] = connection_ip(h) if h[:label]
  end
end

#hosts_for(service_name) ⇒ Object

Get all hosts for a service Returns array of host hashes with :host, :private_ip, :label keys



13
14
15
16
17
18
# File 'lib/boring_services/service_locator.rb', line 13

def hosts_for(service_name)
  services = config.services.select { |s| s['name'] == service_name.to_s }
  return [] if services.empty?

  services.flat_map { |service| normalize_service_hosts(service) }
end

#memcached_servers(label: nil) ⇒ Object

Build memcached connection string (host:port,host:port format)



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/boring_services/service_locator.rb', line 59

def memcached_servers(label: nil)
  hosts = if label
            host = host_by_label('memcached', label)
            host ? [host] : []
          else
            all_ips('memcached')
          end

  return nil if hosts.empty?

  port = port_for('memcached') || 11_211
  hosts.map { |h| "#{h}:#{port}" }.join(',')
end

#port_for(service_name) ⇒ Object

Get port for a service



43
44
45
46
# File 'lib/boring_services/service_locator.rb', line 43

def port_for(service_name)
  service = config.service_config(service_name.to_s)
  service&.dig('port')
end

#redis_url(label: nil, password: nil, db: 0) ⇒ Object

Build a Redis URL for a specific label



49
50
51
52
53
54
55
56
# File 'lib/boring_services/service_locator.rb', line 49

def redis_url(label: nil, password: nil, db: 0)
  host = label ? host_by_label('redis', label) : all_ips('redis').first
  return nil unless host

  port = port_for('redis') || 6379
  auth = password.to_s.empty? ? '' : ":#{password}@"
  "redis://#{auth}#{host}:#{port}/#{db}"
end