| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Workflows.Helpers; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows; |
| | | 9 | | |
| | | 10 | | /// <inheritdoc /> |
| | 651 | 11 | | public class ActivityRegistry(IActivityDescriber activityDescriber, IEnumerable<IActivityDescriptorModifier> modifiers, |
| | | 12 | | { |
| | | 13 | | // Legacy support for manually registered activities |
| | 651 | 14 | | private readonly ISet<ActivityDescriptor> _manualActivityDescriptors = new HashSet<ActivityDescriptor>(); |
| | | 15 | | |
| | | 16 | | // Per-tenant activity descriptors (workflow-as-activities, tenant-specific providers, etc.) |
| | 651 | 17 | | private readonly ConcurrentDictionary<string, TenantRegistryData> _tenantRegistries = new(); |
| | | 18 | | |
| | | 19 | | // Tenant-agnostic activity descriptors (built-in activities, manually registered, etc.) |
| | 651 | 20 | | private readonly TenantRegistryData _agnosticRegistry = new(); |
| | | 21 | | |
| | | 22 | | // Tracks tenant-agnostic providers initialized for this registry instance. |
| | 651 | 23 | | private readonly ConcurrentDictionary<Type, byte> _initializedProviders = new(); |
| | 651 | 24 | | private readonly ConcurrentDictionary<Type, SemaphoreSlim> _providerInitializationLocks = new(); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public void Add(Type providerType, ActivityDescriptor descriptor) |
| | | 28 | | { |
| | 131 | 29 | | var registry = GetOrCreateRegistry(descriptor.TenantId); |
| | 131 | 30 | | var providerDescriptors = GetOrCreateProviderDescriptors(registry, providerType); |
| | 131 | 31 | | Add(descriptor, registry, providerDescriptors); |
| | 131 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public void Remove(Type providerType, ActivityDescriptor descriptor) |
| | | 36 | | { |
| | 8 | 37 | | var registry = GetOrCreateRegistry(descriptor.TenantId); |
| | 8 | 38 | | if (registry.ProvidedActivityDescriptors.TryGetValue(providerType, out var providerDescriptors)) |
| | | 39 | | { |
| | 8 | 40 | | providerDescriptors.Remove(descriptor); |
| | 8 | 41 | | RemoveDescriptor(registry, descriptor); |
| | | 42 | | } |
| | 8 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public IEnumerable<ActivityDescriptor> ListAll() |
| | | 47 | | { |
| | 18051 | 48 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 49 | | |
| | | 50 | | // Get descriptors from current tenant's registry |
| | 18051 | 51 | | var tenantDescriptors = _tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry) |
| | 18051 | 52 | | ? tenantRegistry.ActivityDescriptors.Values |
| | 18051 | 53 | | : Enumerable.Empty<ActivityDescriptor>(); |
| | | 54 | | |
| | | 55 | | // Get descriptors from agnostic registry |
| | 18051 | 56 | | var agnosticDescriptors = _agnosticRegistry.ActivityDescriptors.Values; |
| | | 57 | | |
| | 18051 | 58 | | return tenantDescriptors.Concat(agnosticDescriptors); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public IEnumerable<ActivityDescriptor> ListByProvider(Type providerType) |
| | | 63 | | { |
| | 1282 | 64 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 65 | | |
| | | 66 | | // Get descriptors from current tenant's registry |
| | 1282 | 67 | | var tenantDescriptors = _tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry) && |
| | 1282 | 68 | | tenantRegistry.ProvidedActivityDescriptors.TryGetValue(providerType, out var tenantProvi |
| | 1282 | 69 | | ? tenantProviderDescriptors |
| | 1282 | 70 | | : Enumerable.Empty<ActivityDescriptor>(); |
| | | 71 | | |
| | | 72 | | // Get descriptors from agnostic registry |
| | 1282 | 73 | | var agnosticDescriptors = _agnosticRegistry.ProvidedActivityDescriptors.TryGetValue(providerType, out var agnost |
| | 1282 | 74 | | ? agnosticProviderDescriptors |
| | 1282 | 75 | | : Enumerable.Empty<ActivityDescriptor>(); |
| | | 76 | | |
| | 1282 | 77 | | return tenantDescriptors.Concat(agnosticDescriptors); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | | 81 | | public ActivityDescriptor? Find(string type) |
| | | 82 | | { |
| | 513 | 83 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 84 | | |
| | | 85 | | // Always prefer tenant-specific descriptors over tenant-agnostic ones |
| | | 86 | | // Get highest version from current tenant's registry |
| | 513 | 87 | | if (_tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry)) |
| | | 88 | | { |
| | 96 | 89 | | if (tenantRegistry.LatestActivityDescriptors.TryGetValue(type, out var tenantDescriptor)) |
| | 13 | 90 | | return tenantDescriptor; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Fall back to agnostic registry only if no tenant-specific descriptor exists |
| | 500 | 94 | | return _agnosticRegistry.LatestActivityDescriptors.TryGetValue(type, out var agnosticDescriptor) |
| | 500 | 95 | | ? agnosticDescriptor |
| | 500 | 96 | | : null; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | public ActivityDescriptor? Find(string type, int version) |
| | | 101 | | { |
| | 62096 | 102 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 103 | | |
| | | 104 | | // Check current tenant's registry first |
| | 62096 | 105 | | if (_tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry) && |
| | 62096 | 106 | | tenantRegistry.ActivityDescriptors.TryGetValue((type, version), out var tenantDescriptor)) |
| | | 107 | | { |
| | 3011 | 108 | | return tenantDescriptor; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | // Fall back to agnostic registry |
| | 59085 | 112 | | return _agnosticRegistry.ActivityDescriptors.TryGetValue((type, version), out var agnosticDescriptor) |
| | 59085 | 113 | | ? agnosticDescriptor |
| | 59085 | 114 | | : null; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | | 118 | | public ActivityDescriptor? Find(Func<ActivityDescriptor, bool> predicate) |
| | | 119 | | { |
| | 745 | 120 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 121 | | |
| | | 122 | | // Check current tenant's registry first |
| | 745 | 123 | | if (_tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry)) |
| | | 124 | | { |
| | 574 | 125 | | var tenantMatch = tenantRegistry.ActivityDescriptors.Values.FirstOrDefault(predicate); |
| | 1120 | 126 | | if (tenantMatch != null) return tenantMatch; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | // Fall back to agnostic registry |
| | 199 | 130 | | return _agnosticRegistry.ActivityDescriptors.Values.FirstOrDefault(predicate); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <inheritdoc /> |
| | | 134 | | public IEnumerable<ActivityDescriptor> FindMany(Func<ActivityDescriptor, bool> predicate) |
| | | 135 | | { |
| | 0 | 136 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 137 | | |
| | | 138 | | // Get descriptors from current tenant's registry |
| | 0 | 139 | | var tenantDescriptors = _tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry) |
| | 0 | 140 | | ? tenantRegistry.ActivityDescriptors.Values.Where(predicate) |
| | 0 | 141 | | : Enumerable.Empty<ActivityDescriptor>(); |
| | | 142 | | |
| | | 143 | | // Get descriptors from agnostic registry |
| | 0 | 144 | | var agnosticDescriptors = _agnosticRegistry.ActivityDescriptors.Values.Where(predicate); |
| | | 145 | | |
| | 0 | 146 | | return tenantDescriptors.Concat(agnosticDescriptors); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <inheritdoc /> |
| | | 150 | | public void Register(ActivityDescriptor descriptor) |
| | | 151 | | { |
| | 33 | 152 | | var registry = GetOrCreateRegistry(descriptor.TenantId); |
| | 33 | 153 | | var providerDescriptors = GetOrCreateProviderDescriptors(registry, GetType()); |
| | 33 | 154 | | Add(descriptor, registry, providerDescriptors); |
| | 33 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <inheritdoc /> |
| | | 158 | | public async Task RegisterAsync([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type a |
| | | 159 | | { |
| | 18047 | 160 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName(activityType); |
| | | 161 | | |
| | | 162 | | // Check if already registered in any registry |
| | 971831 | 163 | | if (ListAll().Any(x => x.TypeName == activityTypeName)) |
| | 16806 | 164 | | return; |
| | | 165 | | |
| | 1241 | 166 | | var activityDescriptor = await activityDescriber.DescribeActivityAsync(activityType, cancellationToken); |
| | | 167 | | |
| | 1241 | 168 | | var registry = GetOrCreateRegistry(activityDescriptor.TenantId); |
| | 1241 | 169 | | Add(activityDescriptor, registry, _manualActivityDescriptors); |
| | 18047 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <inheritdoc /> |
| | | 173 | | public async Task RegisterAsync(IEnumerable<Type> activityTypes, CancellationToken cancellationToken = default) |
| | | 174 | | { |
| | 44650 | 175 | | foreach (var activityType in activityTypes) |
| | 17550 | 176 | | await RegisterAsync(activityType, cancellationToken); |
| | 4775 | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <inheritdoc /> |
| | 1 | 180 | | public ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(CancellationToken cancellationToken = default) |
| | | 181 | | |
| | | 182 | | /// <inheritdoc /> |
| | | 183 | | public async Task RefreshDescriptorsAsync(IEnumerable<IActivityProvider> activityProviders, CancellationToken cancel |
| | | 184 | | { |
| | 1890 | 185 | | foreach (var activityProvider in activityProviders) |
| | 62 | 186 | | await RefreshDescriptorsAsync(activityProvider, cancellationToken); |
| | 883 | 187 | | } |
| | | 188 | | |
| | | 189 | | public async Task RefreshDescriptorsAsync(IActivityProvider activityProvider, CancellationToken cancellationToken = |
| | | 190 | | { |
| | 617 | 191 | | var providerType = activityProvider.GetType(); |
| | | 192 | | |
| | | 193 | | // Get new descriptors from provider |
| | 617 | 194 | | var descriptors = (await activityProvider.GetDescriptorsAsync(cancellationToken)).ToList(); |
| | | 195 | | |
| | | 196 | | // Group descriptors by normalized tenant ID |
| | | 197 | | // Normalize null to "*" so both map to the same agnostic group, avoiding redundant processing |
| | 7771 | 198 | | var descriptorsByTenant = descriptors.GroupBy(d => NormalizeTenantIdForGrouping(d.TenantId)); |
| | | 199 | | |
| | 1672 | 200 | | foreach (var group in descriptorsByTenant) |
| | | 201 | | { |
| | 221 | 202 | | var tenantId = group.Key; |
| | 221 | 203 | | var registry = GetOrCreateRegistry(tenantId); |
| | | 204 | | |
| | | 205 | | // Remove old descriptors for this provider from this tenant's registry |
| | 221 | 206 | | if (registry.ProvidedActivityDescriptors.TryGetValue(providerType, out var oldDescriptors)) |
| | | 207 | | { |
| | 2352 | 208 | | foreach (var oldDescriptor in oldDescriptors.ToList()) |
| | | 209 | | { |
| | 1106 | 210 | | RemoveDescriptor(registry, oldDescriptor); |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | // Add new descriptors for this tenant |
| | 221 | 215 | | var providerDescriptors = new List<ActivityDescriptor>(); |
| | 14754 | 216 | | foreach (var descriptor in group) |
| | | 217 | | { |
| | 7156 | 218 | | Add(descriptor, registry, providerDescriptors); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | // Update the provider's descriptor list in this registry |
| | 221 | 222 | | registry.ProvidedActivityDescriptors[providerType] = providerDescriptors; |
| | | 223 | | } |
| | 615 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <inheritdoc /> |
| | | 227 | | public async Task EnsureDescriptorsAsync(IActivityProvider activityProvider, CancellationToken cancellationToken = d |
| | | 228 | | { |
| | 872 | 229 | | if (activityProvider is not ITenantAgnosticActivityProvider) |
| | | 230 | | { |
| | 286 | 231 | | await RefreshDescriptorsAsync(activityProvider, cancellationToken); |
| | 286 | 232 | | return; |
| | | 233 | | } |
| | | 234 | | |
| | 586 | 235 | | var providerType = activityProvider.GetType(); |
| | 586 | 236 | | if (_initializedProviders.ContainsKey(providerType)) |
| | 315 | 237 | | return; |
| | | 238 | | |
| | 531 | 239 | | var initializationLock = _providerInitializationLocks.GetOrAdd(providerType, _ => new(1, 1)); |
| | 271 | 240 | | await initializationLock.WaitAsync(cancellationToken); |
| | | 241 | | |
| | | 242 | | try |
| | | 243 | | { |
| | 271 | 244 | | if (_initializedProviders.ContainsKey(providerType)) |
| | 7 | 245 | | return; |
| | | 246 | | |
| | 264 | 247 | | await RefreshDescriptorsAsync(activityProvider, cancellationToken); |
| | 262 | 248 | | _initializedProviders.TryAdd(providerType, 0); |
| | 262 | 249 | | } |
| | | 250 | | finally |
| | | 251 | | { |
| | 271 | 252 | | initializationLock.Release(); |
| | | 253 | | } |
| | 870 | 254 | | } |
| | | 255 | | |
| | | 256 | | private void Add(ActivityDescriptor? descriptor, TenantRegistryData registry, ICollection<ActivityDescriptor> provid |
| | | 257 | | { |
| | 8561 | 258 | | if (descriptor is null) |
| | | 259 | | { |
| | 0 | 260 | | logger.LogError("Unable to add a null descriptor"); |
| | 0 | 261 | | return; |
| | | 262 | | } |
| | | 263 | | |
| | 50070 | 264 | | foreach (var modifier in modifiers) |
| | 16474 | 265 | | modifier.Modify(descriptor); |
| | | 266 | | |
| | 8561 | 267 | | var activityDescriptors = registry.ActivityDescriptors; |
| | 8561 | 268 | | var descriptorKey = (descriptor.TypeName, descriptor.Version); |
| | | 269 | | |
| | | 270 | | // If the descriptor already exists, replace it. But log a warning. |
| | 8561 | 271 | | if (activityDescriptors.TryGetValue(descriptorKey, out var existingDescriptor)) |
| | | 272 | | { |
| | | 273 | | // Remove the existing descriptor from the providerDescriptors collection. |
| | 130 | 274 | | providerDescriptors.Remove(existingDescriptor); |
| | | 275 | | |
| | | 276 | | // Log a warning. |
| | 130 | 277 | | logger.LogWarning("Activity descriptor {ActivityType} v{ActivityVersion} was already registered for tenant { |
| | | 278 | | } |
| | | 279 | | |
| | 8561 | 280 | | activityDescriptors[descriptorKey] = descriptor; |
| | 8561 | 281 | | UpdateLatestDescriptor(registry, descriptor); |
| | 8561 | 282 | | providerDescriptors.Add(descriptor); |
| | 8561 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <inheritdoc /> |
| | | 286 | | public void Clear() |
| | | 287 | | { |
| | 3 | 288 | | _manualActivityDescriptors.Clear(); |
| | 3 | 289 | | _tenantRegistries.Clear(); |
| | 3 | 290 | | _agnosticRegistry.ActivityDescriptors.Clear(); |
| | 3 | 291 | | _agnosticRegistry.LatestActivityDescriptors.Clear(); |
| | 3 | 292 | | _agnosticRegistry.ProvidedActivityDescriptors.Clear(); |
| | 3 | 293 | | _initializedProviders.Clear(); |
| | 3 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <inheritdoc /> |
| | | 297 | | public void ClearProvider(Type providerType) |
| | | 298 | | { |
| | 3 | 299 | | _initializedProviders.TryRemove(providerType, out _); |
| | 3 | 300 | | var currentTenantId = tenantAccessor.TenantId; |
| | | 301 | | |
| | | 302 | | // Clear from current tenant's registry |
| | 3 | 303 | | if (_tenantRegistries.TryGetValue(currentTenantId, out var tenantRegistry) |
| | 3 | 304 | | && tenantRegistry.ProvidedActivityDescriptors.TryGetValue(providerType, out var descriptors)) |
| | | 305 | | { |
| | 10 | 306 | | foreach (var descriptor in descriptors.ToList()) |
| | 3 | 307 | | RemoveDescriptor(tenantRegistry, descriptor); |
| | | 308 | | |
| | 2 | 309 | | tenantRegistry.ProvidedActivityDescriptors.TryRemove(providerType, out _); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | // Clear from agnostic registry |
| | 3 | 313 | | if (_agnosticRegistry.ProvidedActivityDescriptors.TryGetValue(providerType, out var agnosticDescriptors)) |
| | | 314 | | { |
| | 4 | 315 | | foreach (var descriptor in agnosticDescriptors.ToList()) |
| | 1 | 316 | | RemoveDescriptor(_agnosticRegistry, descriptor); |
| | | 317 | | |
| | 1 | 318 | | _agnosticRegistry.ProvidedActivityDescriptors.TryRemove(providerType, out _); |
| | | 319 | | } |
| | 3 | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Clears all activity descriptors for a specific tenant. Useful when a tenant is deactivated. |
| | | 324 | | /// </summary> |
| | | 325 | | internal void ClearTenant(string tenantId) |
| | | 326 | | { |
| | 0 | 327 | | _tenantRegistries.TryRemove(tenantId, out _); |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | private TenantRegistryData GetOrCreateRegistry(string? tenantId) |
| | | 331 | | { |
| | | 332 | | // Null or agnostic tenant ID goes to agnostic registry |
| | 1634 | 333 | | if (tenantId is null or Tenant.AgnosticTenantId) |
| | 1488 | 334 | | return _agnosticRegistry; |
| | | 335 | | |
| | | 336 | | // Get or create tenant-specific registry |
| | 165 | 337 | | return _tenantRegistries.GetOrAdd(tenantId, _ => new()); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private ICollection<ActivityDescriptor> GetOrCreateProviderDescriptors(TenantRegistryData registry, Type providerTyp |
| | | 341 | | { |
| | 184 | 342 | | return registry.ProvidedActivityDescriptors.GetOrAdd(providerType, _ => new List<ActivityDescriptor>()); |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | private static void UpdateLatestDescriptor(TenantRegistryData registry, ActivityDescriptor descriptor) |
| | | 346 | | { |
| | 8561 | 347 | | registry.LatestActivityDescriptors.AddOrUpdate( |
| | 8561 | 348 | | descriptor.TypeName, |
| | 8561 | 349 | | descriptor, |
| | 8726 | 350 | | (_, latestDescriptor) => descriptor.Version >= latestDescriptor.Version ? descriptor : latestDescriptor); |
| | 8561 | 351 | | } |
| | | 352 | | |
| | | 353 | | private static void RemoveDescriptor(TenantRegistryData registry, ActivityDescriptor descriptor) |
| | | 354 | | { |
| | 1118 | 355 | | if (!registry.ActivityDescriptors.TryRemove((descriptor.TypeName, descriptor.Version), out var removedDescriptor |
| | 0 | 356 | | return; |
| | | 357 | | |
| | 1118 | 358 | | if (registry.LatestActivityDescriptors.TryGetValue(removedDescriptor.TypeName, out var latestDescriptor) && late |
| | 1104 | 359 | | RecomputeLatestDescriptor(registry, removedDescriptor.TypeName); |
| | 1118 | 360 | | } |
| | | 361 | | |
| | | 362 | | private static void RecomputeLatestDescriptor(TenantRegistryData registry, string typeName) |
| | | 363 | | { |
| | 1104 | 364 | | ActivityDescriptor? latestDescriptor = null; |
| | 53798 | 365 | | foreach (var descriptor in registry.ActivityDescriptors.Values) |
| | | 366 | | { |
| | 25795 | 367 | | if (descriptor.TypeName != typeName) |
| | | 368 | | continue; |
| | | 369 | | |
| | 9 | 370 | | if (latestDescriptor == null || descriptor.Version > latestDescriptor.Version) |
| | 9 | 371 | | latestDescriptor = descriptor; |
| | | 372 | | } |
| | | 373 | | |
| | 1104 | 374 | | if (latestDescriptor == null) |
| | 1096 | 375 | | registry.LatestActivityDescriptors.TryRemove(typeName, out _); |
| | | 376 | | else |
| | 8 | 377 | | registry.LatestActivityDescriptors[typeName] = latestDescriptor; |
| | 8 | 378 | | } |
| | | 379 | | |
| | | 380 | | /// <summary> |
| | | 381 | | /// Normalizes tenant ID for grouping purposes. |
| | | 382 | | /// Converts null to "*" so that both null and "*" descriptors are grouped together, |
| | | 383 | | /// avoiding redundant processing of the agnostic registry. |
| | | 384 | | /// </summary> |
| | | 385 | | private static string? NormalizeTenantIdForGrouping(string? tenantId) |
| | | 386 | | { |
| | | 387 | | // Normalize null to "*" so both map to the same group |
| | 7156 | 388 | | return tenantId ?? Tenant.AgnosticTenantId; |
| | | 389 | | } |
| | | 390 | | } |