| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Common.Services; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Management.Entities; |
| | | 7 | | using Elsa.Workflows.Management.Filters; |
| | | 8 | | using Elsa.Workflows.Management.Models; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Management.Stores; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// A non-persistent memory store for saving and loading <see cref="WorkflowInstance"/> entities. |
| | | 14 | | /// </summary> |
| | | 15 | | public class MemoryWorkflowInstanceStore : IWorkflowInstanceStore |
| | | 16 | | { |
| | | 17 | | private readonly MemoryStore<WorkflowInstance> _store; |
| | | 18 | | private readonly ITenantAccessor? _tenantAccessor; |
| | 205 | 19 | | private readonly object _sync = new(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Constructor. |
| | | 23 | | /// </summary> |
| | 205 | 24 | | public MemoryWorkflowInstanceStore(MemoryStore<WorkflowInstance> store, ITenantAccessor? tenantAccessor = null) |
| | | 25 | | { |
| | 205 | 26 | | _store = store; |
| | 205 | 27 | | _tenantAccessor = tenantAccessor; |
| | 205 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public ValueTask<WorkflowInstance?> FindAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = d |
| | | 32 | | { |
| | 412 | 33 | | var entity = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 206 | 34 | | return ValueTask.FromResult(entity); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public ValueTask<Page<WorkflowInstance>> FindManyAsync(WorkflowInstanceFilter filter, PageArgs pageArgs, Cancellatio |
| | | 39 | | { |
| | 0 | 40 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 41 | | var entities = _store.Query(query => Filter(query, filter).Paginate(pageArgs)).ToList(); |
| | 0 | 42 | | var page = Page.Of(entities, count); |
| | 0 | 43 | | return ValueTask.FromResult(page); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public ValueTask<Page<WorkflowInstance>> FindManyAsync<TOrderBy>(WorkflowInstanceFilter filter, PageArgs pageArgs, W |
| | | 48 | | { |
| | 0 | 49 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 50 | | var entities = _store.Query(query => Filter(query, filter).OrderBy(order).Paginate(pageArgs)).ToList(); |
| | 0 | 51 | | var page = Page.Of(entities, count); |
| | 0 | 52 | | return ValueTask.FromResult(page); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public ValueTask<IEnumerable<WorkflowInstance>> FindManyAsync(WorkflowInstanceFilter filter, CancellationToken cance |
| | | 57 | | { |
| | 0 | 58 | | var entities = _store.Query(query => Filter(query, filter)).ToList().AsEnumerable(); |
| | 0 | 59 | | return ValueTask.FromResult(entities); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public ValueTask<IEnumerable<WorkflowInstance>> FindManyAsync<TOrderBy>(WorkflowInstanceFilter filter, WorkflowInsta |
| | | 64 | | { |
| | 0 | 65 | | var entities = _store.Query(query => Filter(query, filter).OrderBy(order)).ToList().AsEnumerable(); |
| | 0 | 66 | | return ValueTask.FromResult(entities); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public ValueTask<long> CountAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default) |
| | | 71 | | { |
| | 0 | 72 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 73 | | return new(count); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public async ValueTask<Page<WorkflowInstanceSummary>> SummarizeManyAsync(WorkflowInstanceFilter filter, PageArgs pag |
| | | 78 | | { |
| | 0 | 79 | | var page = await FindManyAsync(filter, pageArgs, cancellationToken); |
| | 0 | 80 | | return new(page.Items.Select(WorkflowInstanceSummary.FromInstance).ToList(), page.TotalCount); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public async ValueTask<Page<WorkflowInstanceSummary>> SummarizeManyAsync<TOrderBy>(WorkflowInstanceFilter filter, Pa |
| | | 85 | | { |
| | 0 | 86 | | var page = await FindManyAsync(filter, pageArgs, order, cancellationToken); |
| | 0 | 87 | | return new(page.Items.Select(WorkflowInstanceSummary.FromInstance).ToList(), page.TotalCount); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | public async ValueTask<IEnumerable<WorkflowInstanceSummary>> SummarizeManyAsync(WorkflowInstanceFilter filter, Cance |
| | | 92 | | { |
| | 0 | 93 | | var entities = await FindManyAsync(filter, cancellationToken); |
| | 0 | 94 | | return entities.Select(WorkflowInstanceSummary.FromInstance); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc /> |
| | | 98 | | public async ValueTask<IEnumerable<WorkflowInstanceSummary>> SummarizeManyAsync<TOrderBy>(WorkflowInstanceFilter fil |
| | | 99 | | { |
| | 0 | 100 | | var entities = await FindManyAsync(filter, order, cancellationToken); |
| | 0 | 101 | | return entities.Select(WorkflowInstanceSummary.FromInstance); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | | 105 | | public ValueTask<IEnumerable<string>> FindManyIdsAsync(WorkflowInstanceFilter filter, CancellationToken cancellation |
| | | 106 | | { |
| | 0 | 107 | | var entities = _store.Query(query => Filter(query, filter)).Select(x => x.Id).ToList().AsEnumerable(); |
| | 0 | 108 | | return ValueTask.FromResult(entities); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public async ValueTask<Page<string>> FindManyIdsAsync(WorkflowInstanceFilter filter, PageArgs pageArgs, Cancellation |
| | | 113 | | { |
| | 0 | 114 | | var page = await FindManyAsync(filter, pageArgs, cancellationToken); |
| | 0 | 115 | | var ids = page.Items.Select(x => x.Id).ToList(); |
| | 0 | 116 | | return new(ids, page.TotalCount); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public async ValueTask<Page<string>> FindManyIdsAsync<TOrderBy>(WorkflowInstanceFilter filter, PageArgs pageArgs, Wo |
| | | 121 | | { |
| | 0 | 122 | | var page = await FindManyAsync(filter, pageArgs, order, cancellationToken); |
| | 0 | 123 | | var ids = page.Items.Select(x => x.Id).ToList(); |
| | 0 | 124 | | return new(ids, page.TotalCount); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <inheritdoc /> |
| | | 128 | | public ValueTask SaveAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 129 | | { |
| | 406 | 130 | | lock (_sync) |
| | | 131 | | { |
| | 406 | 132 | | ApplyCurrentTenant(instance); |
| | 812 | 133 | | _store.Save(instance, x => x.Id); |
| | 406 | 134 | | } |
| | 406 | 135 | | return ValueTask.CompletedTask; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | public ValueTask AddAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 139 | | { |
| | 0 | 140 | | lock (_sync) |
| | | 141 | | { |
| | 0 | 142 | | ApplyCurrentTenant(instance); |
| | 0 | 143 | | _store.Add(instance, GetId); |
| | 0 | 144 | | } |
| | 0 | 145 | | return ValueTask.CompletedTask; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public ValueTask UpdateAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 149 | | { |
| | 0 | 150 | | lock (_sync) |
| | 0 | 151 | | _store.Update(instance, GetId); |
| | 0 | 152 | | return ValueTask.CompletedTask; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <inheritdoc /> |
| | | 156 | | public ValueTask SaveManyAsync(IEnumerable<WorkflowInstance> instances, CancellationToken cancellationToken = defaul |
| | | 157 | | { |
| | 0 | 158 | | lock (_sync) |
| | | 159 | | { |
| | 0 | 160 | | var instanceList = instances.ToList(); |
| | 0 | 161 | | foreach (var instance in instanceList) |
| | 0 | 162 | | ApplyCurrentTenant(instance); |
| | 0 | 163 | | _store.SaveMany(instanceList, GetId); |
| | 0 | 164 | | } |
| | 0 | 165 | | return ValueTask.CompletedTask; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <inheritdoc /> |
| | | 169 | | public ValueTask<long> DeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default) |
| | | 170 | | { |
| | 0 | 171 | | var query = Filter(_store.List().AsQueryable(), filter); |
| | 0 | 172 | | var count = _store.DeleteMany(query, x => x.Id); |
| | 0 | 173 | | return ValueTask.FromResult(count); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <inheritdoc /> |
| | | 177 | | public async Task UpdateUpdatedTimestampAsync(string workflowInstanceId, DateTimeOffset value, CancellationToken can |
| | | 178 | | { |
| | 0 | 179 | | var workflowInstance = await FindAsync(new() |
| | 0 | 180 | | { |
| | 0 | 181 | | Id = workflowInstanceId |
| | 0 | 182 | | }, cancellationToken); |
| | | 183 | | |
| | 0 | 184 | | if (workflowInstance == null) |
| | 0 | 185 | | throw new InvalidOperationException($"Workflow instance with ID '{workflowInstanceId}' does not exist."); |
| | | 186 | | |
| | 0 | 187 | | workflowInstance.UpdatedAt = value; |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <inheritdoc /> |
| | | 191 | | /// <remarks> |
| | | 192 | | /// <paramref name="allowFinishedCancelled"/> is unused: drain no longer promotes Finished/Cancelled (#8419). |
| | | 193 | | /// The parameter remains so the 3.8.4 signature stays binary-compatible. |
| | | 194 | | /// </remarks> |
| | | 195 | | public ValueTask<bool> TryMarkInterruptedAsync(string workflowInstanceId, CancellationToken cancellationToken = defa |
| | | 196 | | { |
| | | 197 | | // Same lock as Save/Update so a runner's terminal persist cannot land between the |
| | | 198 | | // tenant/status check and the Interrupted mutations. Every condition is evaluated |
| | | 199 | | // once inside this lock — then mutate, with nothing after. |
| | 206 | 200 | | lock (_sync) |
| | | 201 | | { |
| | 412 | 202 | | var instance = _store.Find(x => x.Id == workflowInstanceId && TenantVisibility.IsVisible(x.TenantId, Current |
| | 206 | 203 | | if (instance is null || instance.Status == WorkflowStatus.Finished) |
| | 204 | 204 | | return ValueTask.FromResult(false); |
| | | 205 | | |
| | 2 | 206 | | instance.Status = WorkflowStatus.Running; |
| | 2 | 207 | | instance.SubStatus = WorkflowSubStatus.Interrupted; |
| | 2 | 208 | | instance.IsExecuting = false; |
| | 2 | 209 | | return ValueTask.FromResult(true); |
| | | 210 | | } |
| | 206 | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | private static string GetId(WorkflowInstance workflowInstance) => workflowInstance.Id; |
| | | 214 | | |
| | 818 | 215 | | private string CurrentTenantId => _tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | | 216 | | |
| | | 217 | | [RequiresUnreferencedCode("Calls Elsa.Workflows.Management.Filters.WorkflowInstanceFilter.Apply(IQueryable<WorkflowI |
| | | 218 | | private IQueryable<WorkflowInstance> Filter(IQueryable<WorkflowInstance> query, WorkflowInstanceFilter filter) => |
| | 206 | 219 | | filter.Apply(query.WhereVisibleToTenant(CurrentTenantId)); |
| | | 220 | | |
| | | 221 | | private void ApplyCurrentTenant(WorkflowInstance instance) |
| | | 222 | | { |
| | 406 | 223 | | if (instance.TenantId != Tenant.AgnosticTenantId) |
| | 406 | 224 | | instance.TenantId ??= CurrentTenantId; |
| | 406 | 225 | | } |
| | | 226 | | } |