Module: Legion::Tenants

Defined in:
lib/legion/tenants.rb

Class Method Summary collapse

Class Method Details

.check_quota(tenant_id:, resource:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/tenants.rb', line 39

def check_quota(tenant_id:, resource:, **)
  tenant = find(tenant_id)
  return { allowed: true } unless tenant

  case resource
  when :workers
    count = Legion::Data.connection[:digital_workers].where(tenant_id: tenant_id).count
    { allowed: count < tenant[:max_workers], current: count, limit: tenant[:max_workers] }
  else
    { allowed: true }
  end
end

.create(tenant_id:, name: nil, max_workers: 10, max_queue_depth: 10_000) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/legion/tenants.rb', line 6

def create(tenant_id:, name: nil, max_workers: 10, max_queue_depth: 10_000, **)
  return { error: 'tenant_exists' } if find(tenant_id)

  Legion::Data.connection[:tenants].insert(
    tenant_id:       tenant_id,
    name:            name || tenant_id,
    max_workers:     max_workers,
    max_queue_depth: max_queue_depth,
    status:          'active',
    created_at:      Time.now.utc,
    updated_at:      Time.now.utc
  )
  { created: true, tenant_id: tenant_id }
end

.find(tenant_id) ⇒ Object



21
22
23
24
25
26
# File 'lib/legion/tenants.rb', line 21

def find(tenant_id)
  Legion::Data.connection[:tenants].where(tenant_id: tenant_id).first
rescue StandardError => e
  Legion::Logging.debug("Tenants#find failed: #{e.message}") if defined?(Legion::Logging)
  nil
end

.listObject



35
36
37
# File 'lib/legion/tenants.rb', line 35

def list(**)
  Legion::Data.connection[:tenants].all
end

.suspend(tenant_id:) ⇒ Object



28
29
30
31
32
33
# File 'lib/legion/tenants.rb', line 28

def suspend(tenant_id:, **)
  Legion::Data.connection[:tenants]
              .where(tenant_id: tenant_id)
              .update(status: 'suspended', updated_at: Time.now.utc)
  { suspended: true, tenant_id: tenant_id }
end