| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Common.Services; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Runtime.Entities; |
| | | 7 | | using Elsa.Workflows.Runtime.Filters; |
| | | 8 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Runtime.Stores; |
| | | 12 | | |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | [UsedImplicitly] |
| | | 15 | | public class MemoryTriggerStore : ITriggerStore |
| | | 16 | | { |
| | | 17 | | private readonly MemoryStore<StoredTrigger> _store; |
| | | 18 | | private readonly ITenantAccessor? _tenantAccessor; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="MemoryTriggerStore"/> class. |
| | | 22 | | /// </summary> |
| | 364 | 23 | | public MemoryTriggerStore(MemoryStore<StoredTrigger> store, ITenantAccessor? tenantAccessor = null) |
| | | 24 | | { |
| | 364 | 25 | | _store = store; |
| | 364 | 26 | | _tenantAccessor = tenantAccessor; |
| | 364 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public ValueTask SaveAsync(StoredTrigger record, CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 42 | 32 | | lock (_store.Sync) |
| | | 33 | | { |
| | 42 | 34 | | ApplyCurrentTenant(record); |
| | 42 | 35 | | EnsureLogicalKeyAvailable(record); |
| | 80 | 36 | | _store.Save(record, x => x.Id); |
| | 40 | 37 | | } |
| | | 38 | | |
| | 40 | 39 | | return new(); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public ValueTask SaveManyAsync(IEnumerable<StoredTrigger> records, CancellationToken cancellationToken = default) |
| | | 44 | | { |
| | 2 | 45 | | lock (_store.Sync) |
| | | 46 | | { |
| | 2 | 47 | | var recordList = records.ToList(); |
| | | 48 | | |
| | 10 | 49 | | foreach (var record in recordList) |
| | 3 | 50 | | ApplyCurrentTenant(record); |
| | | 51 | | |
| | 2 | 52 | | var uniqueRecords = DistinctByLogicalKey(recordList).ToList(); |
| | | 53 | | |
| | 7 | 54 | | foreach (var record in uniqueRecords) |
| | 2 | 55 | | EnsureLogicalKeyAvailable(record); |
| | | 56 | | |
| | 2 | 57 | | _store.SaveMany(uniqueRecords, x => x.Id); |
| | 1 | 58 | | } |
| | | 59 | | |
| | 1 | 60 | | return new(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public ValueTask<StoredTrigger?> FindAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 65 | | { |
| | 18 | 66 | | var entity = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 9 | 67 | | return new(entity); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public ValueTask<IEnumerable<StoredTrigger>> FindManyAsync(TriggerFilter filter, CancellationToken cancellationToken |
| | | 72 | | { |
| | 680 | 73 | | var entities = _store.Query(query => Filter(query, filter)); |
| | 340 | 74 | | return new(entities); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public ValueTask<Page<StoredTrigger>> FindManyAsync(TriggerFilter filter, PageArgs pageArgs, CancellationToken cance |
| | | 78 | | { |
| | 79 | 79 | | return FindManyAsync(filter, pageArgs, new StoredTriggerOrder<string>(x => x.Id, OrderDirection.Ascending), canc |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public ValueTask<Page<StoredTrigger>> FindManyAsync<TOrderBy>(TriggerFilter filter, PageArgs pageArgs, StoredTrigger |
| | | 83 | | { |
| | 158 | 84 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 158 | 85 | | var result = _store.Query(query => Filter(query, filter).OrderBy(order).Paginate(pageArgs)).ToList(); |
| | 79 | 86 | | return ValueTask.FromResult(Page.Of(result, count)); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | | 90 | | public ValueTask ReplaceAsync(IEnumerable<StoredTrigger> removed, IEnumerable<StoredTrigger> added, CancellationToke |
| | | 91 | | { |
| | 310 | 92 | | lock (_store.Sync) |
| | | 93 | | { |
| | 310 | 94 | | var removedList = removed.ToList(); |
| | 310 | 95 | | var addedList = added.ToList(); |
| | | 96 | | |
| | 670 | 97 | | foreach (var trigger in addedList) |
| | 25 | 98 | | ApplyCurrentTenant(trigger); |
| | | 99 | | |
| | 310 | 100 | | addedList = DistinctByLogicalKey(addedList).ToList(); |
| | | 101 | | |
| | 310 | 102 | | if (removedList.Count > 0) |
| | 10 | 103 | | _store.DeleteMany(removedList, x => x.Id); |
| | | 104 | | |
| | 310 | 105 | | if (addedList.Count == 0) |
| | 289 | 106 | | return new(); |
| | | 107 | | |
| | 21 | 108 | | var newTriggers = GetMissingLogicalTriggers(addedList); |
| | | 109 | | |
| | 21 | 110 | | if (newTriggers.Count == 0) |
| | 2 | 111 | | return new(); |
| | | 112 | | |
| | 40 | 113 | | _store.SaveMany(newTriggers, x => x.Id); |
| | 19 | 114 | | } |
| | | 115 | | |
| | 19 | 116 | | return new(); |
| | 291 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public ValueTask<long> DeleteManyAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 121 | | { |
| | 2 | 122 | | lock (_store.Sync) |
| | | 123 | | { |
| | 8 | 124 | | var ids = _store.Query(query => Filter(query, filter)).Select(x => x.Id).ToList(); |
| | 2 | 125 | | return new(_store.DeleteMany(ids)); |
| | | 126 | | } |
| | 2 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <remarks> |
| | | 130 | | /// Ambient tenant is applied here rather than in <see cref="TriggerFilter.Apply"/>. |
| | | 131 | | /// EF owns that via <c>SetTenantIdFilter</c> / <c>IgnoreQueryFilters</c>; Memory must compensate. |
| | | 132 | | /// </remarks> |
| | | 133 | | private IQueryable<StoredTrigger> Filter(IQueryable<StoredTrigger> queryable, TriggerFilter filter) => |
| | 509 | 134 | | filter.Apply(queryable.WhereVisibleToTenant(CurrentTenantId, filter.TenantAgnostic)); |
| | | 135 | | |
| | 509 | 136 | | private string CurrentTenantId => _tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | | 137 | | |
| | | 138 | | private void EnsureLogicalKeyAvailable(StoredTrigger record) |
| | | 139 | | { |
| | 44 | 140 | | var logicalKey = GetLogicalKey(record); |
| | 77 | 141 | | var existing = _store.Find(x => x.Id != record.Id && GetLogicalKey(x) == logicalKey); |
| | | 142 | | |
| | 44 | 143 | | if (existing is not null) |
| | | 144 | | { |
| | 3 | 145 | | throw new InvalidOperationException( |
| | 3 | 146 | | $"A stored trigger already exists for workflow '{record.WorkflowDefinitionId}', hash '{record.Hash}', ac |
| | | 147 | | } |
| | 41 | 148 | | } |
| | | 149 | | |
| | | 150 | | private List<StoredTrigger> GetMissingLogicalTriggers(ICollection<StoredTrigger> triggers) |
| | | 151 | | { |
| | 21 | 152 | | var existingKeys = GetExistingLogicalKeys(triggers); |
| | 21 | 153 | | return triggers |
| | 23 | 154 | | .Where(trigger => !existingKeys.Contains(GetLogicalKey(trigger))) |
| | 21 | 155 | | .ToList(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private HashSet<TriggerLogicalKey> GetExistingLogicalKeys(ICollection<StoredTrigger> triggers) |
| | | 159 | | { |
| | 44 | 160 | | var workflowDefinitionIds = triggers.Select(x => x.WorkflowDefinitionId).Distinct().ToHashSet(); |
| | 21 | 161 | | return _store |
| | 8 | 162 | | .FindMany(trigger => workflowDefinitionIds.Contains(trigger.WorkflowDefinitionId)) |
| | 21 | 163 | | .Select(GetLogicalKey) |
| | 21 | 164 | | .ToHashSet(); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | private void ApplyCurrentTenant(StoredTrigger trigger) |
| | | 168 | | { |
| | 70 | 169 | | if (trigger.TenantId == Tenant.AgnosticTenantId || _tenantAccessor is null) |
| | 8 | 170 | | return; |
| | | 171 | | |
| | 62 | 172 | | trigger.TenantId ??= _tenantAccessor.TenantId; |
| | 62 | 173 | | } |
| | | 174 | | |
| | | 175 | | private static IEnumerable<StoredTrigger> DistinctByLogicalKey(IEnumerable<StoredTrigger> triggers) |
| | | 176 | | { |
| | 312 | 177 | | var seen = new HashSet<TriggerLogicalKey>(); |
| | | 178 | | |
| | 680 | 179 | | foreach (var trigger in triggers) |
| | | 180 | | { |
| | 28 | 181 | | if (seen.Add(GetLogicalKey(trigger))) |
| | 25 | 182 | | yield return trigger; |
| | | 183 | | } |
| | 312 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static TriggerLogicalKey GetLogicalKey(StoredTrigger trigger) => |
| | 133 | 187 | | new(trigger.WorkflowDefinitionId, trigger.Hash, trigger.ActivityId, trigger.TenantId); |
| | | 188 | | |
| | 0 | 189 | | private readonly record struct TriggerLogicalKey(string WorkflowDefinitionId, string? Hash, string ActivityId, strin |
| | | 190 | | } |