| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Elsa.Workflows.Runtime.Entities; |
| | | 8 | | using Elsa.Workflows.Runtime.Filters; |
| | | 9 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | using Open.Linq.AsyncExtensions; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Persistence.EFCore.Modules.Runtime; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | [UsedImplicitly] |
| | 423 | 17 | | public class EFCoreTriggerStore( |
| | 423 | 18 | | EntityStore<RuntimeElsaDbContext, StoredTrigger> store, |
| | 423 | 19 | | ITenantAccessor tenantAccessor, |
| | 423 | 20 | | IPayloadSerializer serializer) : ITriggerStore |
| | | 21 | | { |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public async ValueTask SaveAsync(StoredTrigger record, CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 1 | 25 | | await store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async ValueTask SaveManyAsync(IEnumerable<StoredTrigger> records, CancellationToken cancellationToken = defau |
| | | 30 | | { |
| | 0 | 31 | | await store.SaveManyAsync(records, OnSaveAsync, cancellationToken); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async ValueTask<StoredTrigger?> FindAsync(TriggerFilter filter, CancellationToken cancellationToken = default |
| | | 36 | | { |
| | 2 | 37 | | return await store.FindAsync(filter.Apply, OnLoadAsync, filter.TenantAgnostic, cancellationToken); |
| | 2 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public async ValueTask<IEnumerable<StoredTrigger>> FindManyAsync(TriggerFilter filter, CancellationToken cancellatio |
| | | 42 | | { |
| | 1469 | 43 | | return await store.QueryAsync(filter.Apply, OnLoadAsync, filter.TenantAgnostic, cancellationToken); |
| | 1469 | 44 | | } |
| | | 45 | | |
| | | 46 | | public ValueTask<Page<StoredTrigger>> FindManyAsync(TriggerFilter filter, PageArgs pageArgs, CancellationToken cance |
| | | 47 | | { |
| | 0 | 48 | | return FindManyAsync(filter, pageArgs, new StoredTriggerOrder<string>(x => x.Id, OrderDirection.Ascending), canc |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public async ValueTask<Page<StoredTrigger>> FindManyAsync<TProp>(TriggerFilter filter, PageArgs pageArgs, StoredTrig |
| | | 52 | | { |
| | 0 | 53 | | var count = await store.QueryAsync(filter.Apply, OnLoadAsync, cancellationToken).LongCount(); |
| | 0 | 54 | | var results = await store.QueryAsync(queryable => filter.Apply(queryable).OrderBy(order).Paginate(pageArgs).Orde |
| | 0 | 55 | | return new(results, count); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public async ValueTask ReplaceAsync(IEnumerable<StoredTrigger> removed, IEnumerable<StoredTrigger> added, Cancellati |
| | | 60 | | { |
| | 1351 | 61 | | var removedList = removed.ToList(); |
| | 1351 | 62 | | var addedList = added.ToList(); |
| | | 63 | | |
| | 2894 | 64 | | foreach (var trigger in addedList) |
| | 96 | 65 | | ApplyCurrentTenant(trigger); |
| | | 66 | | |
| | 1351 | 67 | | addedList = DistinctByLogicalKey(addedList).ToList(); |
| | | 68 | | |
| | 1351 | 69 | | if (removedList.Count > 0) |
| | | 70 | | { |
| | 36 | 71 | | var filter = new TriggerFilter { Ids = removedList.Select(r => r.Id).ToList() }; |
| | 18 | 72 | | await DeleteManyAsync(filter, cancellationToken); |
| | | 73 | | } |
| | | 74 | | |
| | 1351 | 75 | | if (addedList.Count == 0) |
| | 1267 | 76 | | return; |
| | | 77 | | |
| | 84 | 78 | | var newTriggers = await GetMissingLogicalTriggersAsync(addedList, cancellationToken); |
| | | 79 | | |
| | 84 | 80 | | if (newTriggers.Count == 0) |
| | 0 | 81 | | return; |
| | | 82 | | |
| | | 83 | | try |
| | | 84 | | { |
| | 84 | 85 | | await store.SaveManyAsync(newTriggers, OnSaveAsync, cancellationToken); |
| | 84 | 86 | | } |
| | 0 | 87 | | catch (Exception ex) when (DbExceptionClassifier.IsDuplicateKey(ex)) |
| | | 88 | | { |
| | 0 | 89 | | var remainingTriggers = await GetMissingLogicalTriggersAsync(newTriggers, cancellationToken); |
| | | 90 | | |
| | 0 | 91 | | if (remainingTriggers.Count == 0) |
| | 0 | 92 | | return; |
| | | 93 | | |
| | 0 | 94 | | await store.SaveManyAsync(remainingTriggers, OnSaveAsync, cancellationToken); |
| | | 95 | | } |
| | 1351 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | public async ValueTask<long> DeleteManyAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 100 | | { |
| | 18 | 101 | | return await store.DeleteWhereAsync(filter.Apply, cancellationToken); |
| | 18 | 102 | | } |
| | | 103 | | |
| | | 104 | | private ValueTask OnSaveAsync(RuntimeElsaDbContext dbContext, StoredTrigger entity, CancellationToken cancellationTo |
| | | 105 | | { |
| | 97 | 106 | | dbContext.Entry(entity).Property("SerializedPayload").CurrentValue = entity.Payload != null ? serializer.Seriali |
| | 97 | 107 | | return default; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | private ValueTask OnLoadAsync(RuntimeElsaDbContext dbContext, StoredTrigger? entity, CancellationToken cancellationT |
| | | 111 | | { |
| | 609 | 112 | | if (entity is null) |
| | 0 | 113 | | return ValueTask.CompletedTask; |
| | | 114 | | |
| | 609 | 115 | | var json = dbContext.Entry(entity).Property<string>("SerializedPayload").CurrentValue; |
| | 609 | 116 | | entity.Payload = !string.IsNullOrEmpty(json) ? serializer.Deserialize(json) : null; |
| | | 117 | | |
| | 609 | 118 | | return ValueTask.CompletedTask; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | private async Task<HashSet<string>> GetExistingLogicalKeysAsync(ICollection<StoredTrigger> triggers, CancellationTok |
| | | 122 | | { |
| | 180 | 123 | | var workflowDefinitionIds = triggers.Select(x => x.WorkflowDefinitionId).Distinct().ToList(); |
| | 84 | 124 | | var existingTriggers = await store.QueryAsync( |
| | 84 | 125 | | queryable => queryable.Where(trigger => workflowDefinitionIds.Contains(trigger.WorkflowDefinitionId)), |
| | 84 | 126 | | cancellationToken); |
| | | 127 | | |
| | 84 | 128 | | return existingTriggers |
| | 84 | 129 | | .Select(GetLogicalKey) |
| | 84 | 130 | | .ToHashSet(StringComparer.Ordinal); |
| | 84 | 131 | | } |
| | | 132 | | |
| | | 133 | | private async Task<List<StoredTrigger>> GetMissingLogicalTriggersAsync(ICollection<StoredTrigger> triggers, Cancella |
| | | 134 | | { |
| | 84 | 135 | | var existingKeys = await GetExistingLogicalKeysAsync(triggers, cancellationToken); |
| | 84 | 136 | | return triggers |
| | 96 | 137 | | .Where(trigger => !existingKeys.Contains(GetLogicalKey(trigger))) |
| | 84 | 138 | | .ToList(); |
| | 84 | 139 | | } |
| | | 140 | | |
| | | 141 | | private void ApplyCurrentTenant(StoredTrigger trigger) |
| | | 142 | | { |
| | 96 | 143 | | if (trigger.TenantId == Tenant.AgnosticTenantId) |
| | 0 | 144 | | return; |
| | | 145 | | |
| | 96 | 146 | | trigger.TenantId ??= tenantAccessor.TenantId; |
| | 96 | 147 | | } |
| | | 148 | | |
| | | 149 | | private static IEnumerable<StoredTrigger> DistinctByLogicalKey(IEnumerable<StoredTrigger> triggers) |
| | | 150 | | { |
| | 1351 | 151 | | var seen = new HashSet<string>(StringComparer.Ordinal); |
| | | 152 | | |
| | 2894 | 153 | | foreach (var trigger in triggers) |
| | | 154 | | { |
| | 96 | 155 | | if (seen.Add(GetLogicalKey(trigger))) |
| | 96 | 156 | | yield return trigger; |
| | | 157 | | } |
| | 1351 | 158 | | } |
| | | 159 | | |
| | | 160 | | private static string GetLogicalKey(StoredTrigger trigger) => |
| | 192 | 161 | | string.Join( |
| | 192 | 162 | | '\u001f', |
| | 192 | 163 | | trigger.WorkflowDefinitionId, |
| | 192 | 164 | | trigger.Hash, |
| | 192 | 165 | | trigger.ActivityId, |
| | 192 | 166 | | trigger.TenantId); |
| | | 167 | | } |