| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using Elsa.Workflows.Helpers; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 528 | 10 | | public class ActivityRegistry(IActivityDescriber activityDescriber, IEnumerable<IActivityDescriptorModifier> modifiers, |
| | | 11 | | { |
| | 528 | 12 | | private readonly ISet<ActivityDescriptor> _manualActivityDescriptors = new HashSet<ActivityDescriptor>(); |
| | 528 | 13 | | private ConcurrentDictionary<Type, ICollection<ActivityDescriptor>> _providedActivityDescriptors = new(); |
| | 528 | 14 | | private ConcurrentDictionary<(string Type, int Version), ActivityDescriptor> _activityDescriptors = new(); |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | 64 | 17 | | public void Add(Type providerType, ActivityDescriptor descriptor) => Add(descriptor, GetOrCreateDescriptors(provider |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public void Remove(Type providerType, ActivityDescriptor descriptor) |
| | | 21 | | { |
| | 6 | 22 | | _providedActivityDescriptors[providerType].Remove(descriptor); |
| | 6 | 23 | | _activityDescriptors.Remove((descriptor.TypeName, descriptor.Version), out _); |
| | 6 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 0 | 27 | | public IEnumerable<ActivityDescriptor> ListAll() => _activityDescriptors.Values; |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | 381 | 30 | | public IEnumerable<ActivityDescriptor> ListByProvider(Type providerType) => _providedActivityDescriptors.TryGetValue |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 3137 | 33 | | public ActivityDescriptor? Find(string type) => _activityDescriptors.Values.Where(x => x.TypeName == type).MaxBy(x = |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | 25388 | 36 | | public ActivityDescriptor? Find(string type, int version) => _activityDescriptors.TryGetValue((type, version), out v |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | 527 | 39 | | public ActivityDescriptor? Find(Func<ActivityDescriptor, bool> predicate) => _activityDescriptors.Values.FirstOrDefa |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 0 | 42 | | public IEnumerable<ActivityDescriptor> FindMany(Func<ActivityDescriptor, bool> predicate) => _activityDescriptors.Va |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public void Register(ActivityDescriptor descriptor) |
| | | 46 | | { |
| | 0 | 47 | | Add(GetType(), descriptor); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public async Task RegisterAsync([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type a |
| | | 52 | | { |
| | 6922 | 53 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName(activityType); |
| | | 54 | | |
| | 167711 | 55 | | if (_activityDescriptors.Values.Any(x => x.TypeName == activityTypeName)) |
| | 5912 | 56 | | return; |
| | | 57 | | |
| | 1010 | 58 | | var activityDescriptor = await activityDescriber.DescribeActivityAsync(activityType, cancellationToken); |
| | | 59 | | |
| | 1010 | 60 | | Add(activityDescriptor, _activityDescriptors, _manualActivityDescriptors); |
| | 1010 | 61 | | _manualActivityDescriptors.Add(activityDescriptor); |
| | 6922 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public async Task RegisterAsync(IEnumerable<Type> activityTypes, CancellationToken cancellationToken = default) |
| | | 66 | | { |
| | 16790 | 67 | | foreach (var activityType in activityTypes) |
| | 6508 | 68 | | await RegisterAsync(activityType, cancellationToken); |
| | 1887 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | 0 | 72 | | public ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(CancellationToken cancellationToken = default) |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | public async Task RefreshDescriptorsAsync(IEnumerable<IActivityProvider> activityProviders, CancellationToken cancel |
| | | 76 | | { |
| | 870 | 77 | | var providersDictionary = new ConcurrentDictionary<Type, ICollection<ActivityDescriptor>>(); |
| | 870 | 78 | | var activityDescriptors = new ConcurrentDictionary<(string Type, int Version), ActivityDescriptor>(_activityDesc |
| | 2724 | 79 | | foreach (var activityProvider in activityProviders) |
| | | 80 | | { |
| | 492 | 81 | | var descriptors = (await activityProvider.GetDescriptorsAsync(cancellationToken)).ToList(); |
| | 492 | 82 | | var providerDescriptors = new List<ActivityDescriptor>(); |
| | 492 | 83 | | providersDictionary[activityProvider.GetType()] = providerDescriptors; |
| | 22934 | 84 | | foreach (var descriptor in descriptors) |
| | | 85 | | { |
| | 10975 | 86 | | Add(descriptor, activityDescriptors, providerDescriptors); |
| | | 87 | | } |
| | 492 | 88 | | } |
| | | 89 | | |
| | 870 | 90 | | Interlocked.Exchange(ref _activityDescriptors, activityDescriptors); |
| | 870 | 91 | | Interlocked.Exchange(ref _providedActivityDescriptors, providersDictionary); |
| | 870 | 92 | | } |
| | | 93 | | |
| | | 94 | | public async Task RefreshDescriptorsAsync(IActivityProvider activityProvider, CancellationToken cancellationToken = |
| | | 95 | | { |
| | 0 | 96 | | var providersDictionary = new ConcurrentDictionary<Type, ICollection<ActivityDescriptor>>(_providedActivityDescr |
| | 0 | 97 | | var activityDescriptors = new ConcurrentDictionary<(string Type, int Version), ActivityDescriptor>(_activityDesc |
| | 0 | 98 | | var descriptors = (await activityProvider.GetDescriptorsAsync(cancellationToken)).ToList(); |
| | 0 | 99 | | var providerDescriptors = new List<ActivityDescriptor>(); |
| | 0 | 100 | | providersDictionary[activityProvider.GetType()] = providerDescriptors; |
| | | 101 | | |
| | 0 | 102 | | foreach (var descriptor in descriptors) |
| | 0 | 103 | | Add(descriptor, activityDescriptors, providerDescriptors); |
| | | 104 | | |
| | 0 | 105 | | Interlocked.Exchange(ref _activityDescriptors, activityDescriptors); |
| | 0 | 106 | | Interlocked.Exchange(ref _providedActivityDescriptors, providersDictionary); |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | private void Add(ActivityDescriptor descriptor, ICollection<ActivityDescriptor> target) |
| | | 110 | | { |
| | 64 | 111 | | Add(descriptor, _activityDescriptors, target); |
| | 64 | 112 | | } |
| | | 113 | | |
| | | 114 | | private void Add(ActivityDescriptor? descriptor, ConcurrentDictionary<(string Type, int Version), ActivityDescriptor |
| | | 115 | | { |
| | 12049 | 116 | | if (descriptor is null) |
| | | 117 | | { |
| | 0 | 118 | | logger.LogError("Unable to add a null descriptor"); |
| | 0 | 119 | | return; |
| | | 120 | | } |
| | | 121 | | |
| | 48618 | 122 | | foreach (var modifier in modifiers) |
| | 12260 | 123 | | modifier.Modify(descriptor); |
| | | 124 | | |
| | | 125 | | // If the descriptor already exists, replace it. But log a warning. |
| | 12049 | 126 | | if (activityDescriptors.TryGetValue((descriptor.TypeName, descriptor.Version), out var existingDescriptor)) |
| | | 127 | | { |
| | | 128 | | // Remove the existing descriptor from the providerDescriptors collection. |
| | 5936 | 129 | | providerDescriptors.Remove(existingDescriptor); |
| | | 130 | | |
| | | 131 | | // Log a warning. |
| | 5936 | 132 | | logger.LogWarning("Activity descriptor {ActivityType} v{ActivityVersion} was already registered. Replacing w |
| | | 133 | | } |
| | | 134 | | |
| | 12049 | 135 | | activityDescriptors[(descriptor.TypeName, descriptor.Version)] = descriptor; |
| | 12049 | 136 | | providerDescriptors.Add(descriptor); |
| | 12049 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <inheritdoc /> |
| | | 140 | | public void Clear() |
| | | 141 | | { |
| | 0 | 142 | | _activityDescriptors.Clear(); |
| | 0 | 143 | | _providedActivityDescriptors.Clear(); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <inheritdoc /> |
| | | 147 | | public void ClearProvider(Type providerType) |
| | | 148 | | { |
| | 0 | 149 | | var descriptors = ListByProvider(providerType).ToList(); |
| | | 150 | | |
| | 0 | 151 | | foreach (var descriptor in descriptors) |
| | 0 | 152 | | _activityDescriptors.Remove((descriptor.TypeName, descriptor.Version), out _); |
| | | 153 | | |
| | 0 | 154 | | _providedActivityDescriptors.Remove(providerType, out _); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | private ICollection<ActivityDescriptor> GetOrCreateDescriptors(Type provider) |
| | | 158 | | { |
| | 64 | 159 | | if (_providedActivityDescriptors.TryGetValue(provider, out var descriptors)) |
| | 64 | 160 | | return descriptors; |
| | | 161 | | |
| | 0 | 162 | | descriptors = new List<ActivityDescriptor>(); |
| | 0 | 163 | | _providedActivityDescriptors[provider] = descriptors; |
| | | 164 | | |
| | 0 | 165 | | return descriptors; |
| | | 166 | | } |
| | | 167 | | } |