Module: Legion::Transport::TenantTopology

Defined in:
lib/legion/transport/tenant_topology.rb

Constant Summary collapse

SHARED_EXCHANGES =
%w[legion.control legion.health legion.audit].freeze

Class Method Summary collapse

Class Method Details

.current_tenant_idObject



35
36
37
38
39
40
41
# File 'lib/legion/transport/tenant_topology.rb', line 35

def self.current_tenant_id
  return nil unless defined?(Legion::TenantContext)

  Legion::TenantContext.current_tenant_id
rescue StandardError
  nil
end

.enabled?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/legion/transport/tenant_topology.rb', line 30

def self.enabled?
  settings = transport_settings
  settings.is_a?(Hash) && settings.dig(:tenant_topology, :enabled) == true
end

.exchange_name(base_name, tenant_id: nil) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/legion/transport/tenant_topology.rb', line 8

def self.exchange_name(base_name, tenant_id: nil)
  return base_name unless enabled?

  tid = tenant_id || current_tenant_id
  return base_name if tid.nil? || tid == 'default' || shared?(base_name)

  "t.#{tid}.#{base_name}"
end

.queue_name(base_name, tenant_id: nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/legion/transport/tenant_topology.rb', line 17

def self.queue_name(base_name, tenant_id: nil)
  return base_name unless enabled?

  tid = tenant_id || current_tenant_id
  return base_name if tid.nil? || tid == 'default'

  "t.#{tid}.#{base_name}"
end

.shared?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/legion/transport/tenant_topology.rb', line 26

def self.shared?(name)
  SHARED_EXCHANGES.any? { |prefix| name.start_with?(prefix) }
end