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