| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Common.Multitenancy; |
| | | 5 | | |
| | 1 | 6 | | public class DefaultTenantService(IServiceScopeFactory scopeFactory, ITenantScopeFactory tenantScopeFactory, TenantEvent |
| | | 7 | | { |
| | 1 | 8 | | private readonly AsyncServiceScope _serviceScope = scopeFactory.CreateAsyncScope(); |
| | 1 | 9 | | private readonly SemaphoreSlim _initializationLock = new(1, 1); |
| | 1 | 10 | | private readonly SemaphoreSlim _refreshLock = new(1, 1); |
| | | 11 | | private IDictionary<string, Tenant>? _tenantsDictionary; |
| | | 12 | | private IDictionary<Tenant, TenantScope>? _tenantScopesDictionary; |
| | | 13 | | |
| | | 14 | | public async ValueTask DisposeAsync() |
| | | 15 | | { |
| | 1 | 16 | | await _serviceScope.DisposeAsync(); |
| | 1 | 17 | | _initializationLock.Dispose(); |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<Tenant?> FindAsync(string id, CancellationToken cancellationToken = default) |
| | | 21 | | { |
| | 0 | 22 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 0 | 23 | | return dictionary.TryGetValue(id.EmptyIfNull(), out var tenant) ? tenant : null; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<Tenant?> FindAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 27 | | { |
| | 0 | 28 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 0 | 29 | | return filter.Apply(dictionary.Values.AsQueryable()).FirstOrDefault(); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task<Tenant> GetAsync(string id, CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 0 | 34 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 0 | 35 | | return dictionary[id.EmptyIfNull()]; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<Tenant> GetAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 39 | | { |
| | 0 | 40 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 0 | 41 | | return filter.Apply(dictionary.Values.AsQueryable()).First(); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public async Task<IEnumerable<Tenant>> ListAsync(CancellationToken cancellationToken = default) |
| | | 45 | | { |
| | 0 | 46 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 0 | 47 | | return dictionary.Values; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | public async Task<IEnumerable<Tenant>> ListAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 51 | | { |
| | 0 | 52 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 0 | 53 | | return filter.Apply(dictionary.Values.AsQueryable()); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task ActivateTenantsAsync(CancellationToken cancellationToken = default) |
| | | 57 | | { |
| | 1 | 58 | | await RefreshAsync(cancellationToken); |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task DeactivateTenantsAsync(CancellationToken cancellationToken = default) |
| | | 62 | | { |
| | 2 | 63 | | var dictionary = await GetTenantsDictionaryAsync(cancellationToken); |
| | 2 | 64 | | var tenants = dictionary.Values.ToArray(); |
| | | 65 | | |
| | 6 | 66 | | foreach (var tenant in tenants) |
| | 1 | 67 | | await UnregisterTenantAsync(tenant, false, cancellationToken); |
| | 2 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task RefreshAsync(CancellationToken cancellationToken = default) |
| | | 71 | | { |
| | 1 | 72 | | await _refreshLock.WaitAsync(cancellationToken); |
| | | 73 | | |
| | | 74 | | try |
| | | 75 | | { |
| | 1 | 76 | | await using var scope = scopeFactory.CreateAsyncScope(); |
| | 1 | 77 | | var tenantsProvider = scope.ServiceProvider.GetRequiredService<ITenantsProvider>(); |
| | 1 | 78 | | var currentTenants = await GetTenantsDictionaryAsync(cancellationToken); |
| | 1 | 79 | | var currentTenantIds = currentTenants.Keys; |
| | 2 | 80 | | var newTenants = (await tenantsProvider.ListAsync(cancellationToken)).ToDictionary(x => x.Id.EmptyIfNull()); |
| | 1 | 81 | | var newTenantIds = newTenants.Keys; |
| | 1 | 82 | | var removedTenantIds = currentTenantIds.Except(newTenantIds).ToArray(); |
| | 1 | 83 | | var addedTenantIds = newTenantIds.Except(currentTenantIds).ToArray(); |
| | | 84 | | |
| | 2 | 85 | | foreach (var removedTenantId in removedTenantIds) |
| | | 86 | | { |
| | 0 | 87 | | var removedTenant = currentTenants[removedTenantId]; |
| | 0 | 88 | | await UnregisterTenantAsync(removedTenant, true, cancellationToken); |
| | | 89 | | } |
| | | 90 | | |
| | 2 | 91 | | foreach (var addedTenantId in addedTenantIds) |
| | | 92 | | { |
| | 0 | 93 | | var addedTenant = newTenants[addedTenantId]; |
| | 0 | 94 | | await RegisterTenantAsync(addedTenant, cancellationToken); |
| | | 95 | | } |
| | 1 | 96 | | } |
| | | 97 | | finally |
| | | 98 | | { |
| | 1 | 99 | | _refreshLock.Release(); |
| | | 100 | | } |
| | 1 | 101 | | } |
| | | 102 | | |
| | | 103 | | private async Task<IDictionary<string, Tenant>> GetTenantsDictionaryAsync(CancellationToken cancellationToken) |
| | | 104 | | { |
| | 3 | 105 | | if (_tenantsDictionary == null) |
| | | 106 | | { |
| | 1 | 107 | | await _initializationLock.WaitAsync(cancellationToken); // Lock to ensure single-threaded initialization |
| | | 108 | | try |
| | | 109 | | { |
| | 1 | 110 | | if (_tenantsDictionary == null) // Double-check locking |
| | | 111 | | { |
| | 1 | 112 | | _tenantsDictionary = new Dictionary<string, Tenant>(); |
| | 1 | 113 | | _tenantScopesDictionary = new Dictionary<Tenant, TenantScope>(); |
| | 1 | 114 | | var tenantsProvider = _serviceScope.ServiceProvider.GetRequiredService<ITenantsProvider>(); |
| | 1 | 115 | | var tenants = await tenantsProvider.ListAsync(cancellationToken); |
| | | 116 | | |
| | 4 | 117 | | foreach (var tenant in tenants) |
| | 1 | 118 | | await RegisterTenantAsync(tenant, cancellationToken); |
| | | 119 | | } |
| | 1 | 120 | | } |
| | | 121 | | finally |
| | | 122 | | { |
| | 1 | 123 | | _initializationLock.Release(); |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | 3 | 127 | | return _tenantsDictionary; |
| | 3 | 128 | | } |
| | | 129 | | |
| | | 130 | | private async Task RegisterTenantAsync(Tenant tenant, CancellationToken cancellationToken = default) |
| | | 131 | | { |
| | 1 | 132 | | var scope = tenantScopeFactory.CreateScope(tenant); |
| | 1 | 133 | | _tenantsDictionary![tenant.Id.EmptyIfNull()] = tenant; |
| | 1 | 134 | | _tenantScopesDictionary![tenant] = scope; |
| | | 135 | | |
| | 1 | 136 | | using (tenantAccessor.PushContext(tenant)) |
| | 1 | 137 | | await tenantEvents.TenantActivatedAsync(new(tenant, scope, cancellationToken)); |
| | 1 | 138 | | } |
| | | 139 | | |
| | | 140 | | private async Task UnregisterTenantAsync(Tenant tenant, bool isDeleted, CancellationToken cancellationToken = defaul |
| | | 141 | | { |
| | 1 | 142 | | if (_tenantScopesDictionary!.Remove(tenant, out var scope)) |
| | | 143 | | { |
| | 1 | 144 | | _tenantsDictionary!.Remove(tenant.Id.EmptyIfNull(), out _); |
| | | 145 | | |
| | 1 | 146 | | using (tenantAccessor.PushContext(tenant)) |
| | | 147 | | { |
| | 1 | 148 | | await tenantEvents.TenantDeactivatedAsync(new(tenant, scope, cancellationToken)); |
| | | 149 | | |
| | 1 | 150 | | if (isDeleted) |
| | 0 | 151 | | await tenantEvents.TenantDeletedAsync(new(tenant, scope, cancellationToken)); |
| | 1 | 152 | | } |
| | | 153 | | } |
| | 1 | 154 | | } |
| | | 155 | | } |