| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Common.Codecs; |
| | | 4 | | using Elsa.Common.Entities; |
| | | 5 | | using Elsa.Common.Models; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using Elsa.Workflows; |
| | | 8 | | using Elsa.Workflows.Management; |
| | | 9 | | using Elsa.Workflows.Management.Entities; |
| | | 10 | | using Elsa.Workflows.Management.Filters; |
| | | 11 | | using Elsa.Workflows.Management.Models; |
| | | 12 | | using Elsa.Workflows.Management.Options; |
| | | 13 | | using JetBrains.Annotations; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | using Microsoft.Extensions.Options; |
| | | 17 | | using Open.Linq.AsyncExtensions; |
| | | 18 | | |
| | | 19 | | namespace Elsa.Persistence.EFCore.Modules.Management; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// An EF Core implementation of <see cref="IWorkflowInstanceStore"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | [UsedImplicitly] |
| | | 25 | | public class EFCoreWorkflowInstanceStore : IWorkflowInstanceStore |
| | | 26 | | { |
| | | 27 | | private readonly EntityStore<ManagementElsaDbContext, WorkflowInstance> _store; |
| | | 28 | | private readonly IWorkflowStateSerializer _workflowStateSerializer; |
| | | 29 | | private readonly ICompressionCodecResolver _compressionCodecResolver; |
| | | 30 | | private readonly IOptions<ManagementOptions> _options; |
| | | 31 | | private readonly ILogger<EFCoreWorkflowInstanceStore> _logger; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Constructor. |
| | | 35 | | /// </summary> |
| | 322 | 36 | | public EFCoreWorkflowInstanceStore( |
| | 322 | 37 | | EntityStore<ManagementElsaDbContext, WorkflowInstance> store, |
| | 322 | 38 | | IWorkflowStateSerializer workflowStateSerializer, |
| | 322 | 39 | | ICompressionCodecResolver compressionCodecResolver, |
| | 322 | 40 | | IOptions<ManagementOptions> options, |
| | 322 | 41 | | ILogger<EFCoreWorkflowInstanceStore> logger) |
| | | 42 | | { |
| | 322 | 43 | | _store = store; |
| | 322 | 44 | | _workflowStateSerializer = workflowStateSerializer; |
| | 322 | 45 | | _compressionCodecResolver = compressionCodecResolver; |
| | 322 | 46 | | _options = options; |
| | 322 | 47 | | _logger = logger; |
| | 322 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public async ValueTask<WorkflowInstance?> FindAsync(WorkflowInstanceFilter filter, CancellationToken cancellationTok |
| | | 52 | | { |
| | 212 | 53 | | return await _store.QueryAsync(query => Filter(query, filter), OnLoadAsync, cancellationToken).FirstOrDefault(); |
| | 106 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public async ValueTask<Page<WorkflowInstance>> FindManyAsync(WorkflowInstanceFilter filter, PageArgs pageArgs, Cance |
| | | 58 | | { |
| | 0 | 59 | | var orderBy = new WorkflowInstanceOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 0 | 60 | | return await FindManyAsync(filter, pageArgs, orderBy, cancellationToken); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public async ValueTask<Page<WorkflowInstance>> FindManyAsync<TOrderBy>(WorkflowInstanceFilter filter, PageArgs pageA |
| | | 65 | | { |
| | 0 | 66 | | var count = await _store.QueryAsync(query => Filter(query, filter), x => x.Id, cancellationToken).LongCount(); |
| | 0 | 67 | | var entities = await _store.QueryAsync(query => Filter(query, filter).OrderBy(order).Paginate(pageArgs), OnLoadA |
| | 0 | 68 | | return Page.Of(entities, count); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | | 72 | | public async ValueTask<IEnumerable<WorkflowInstance>> FindManyAsync(WorkflowInstanceFilter filter, CancellationToken |
| | | 73 | | { |
| | 4 | 74 | | var orderBy = new WorkflowInstanceOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 4 | 75 | | return await FindManyAsync(filter, orderBy, cancellationToken); |
| | 4 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc /> |
| | | 79 | | public async ValueTask<IEnumerable<WorkflowInstance>> FindManyAsync<TOrderBy>(WorkflowInstanceFilter filter, Workflo |
| | | 80 | | { |
| | 8 | 81 | | return await _store.QueryAsync(query => Filter(query, filter).OrderBy(order), OnLoadAsync, cancellationToken).To |
| | 4 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | [RequiresUnreferencedCode("Calls Elsa.Workflows.Contracts.IWorkflowStateSerializer.SerializeAsync(WorkflowState, Can |
| | | 86 | | public async ValueTask<long> CountAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default |
| | | 87 | | { |
| | 0 | 88 | | return await _store.CountAsync(filter.Apply, cancellationToken); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | public async ValueTask<Page<WorkflowInstanceSummary>> SummarizeManyAsync(WorkflowInstanceFilter filter, PageArgs pag |
| | | 93 | | { |
| | 2 | 94 | | var orderBy = new WorkflowInstanceOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 2 | 95 | | return await SummarizeManyAsync(filter, pageArgs, orderBy, cancellationToken); |
| | 2 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | public async ValueTask<Page<WorkflowInstanceSummary>> SummarizeManyAsync<TOrderBy>(WorkflowInstanceFilter filter, Pa |
| | | 100 | | { |
| | 2 | 101 | | await using var dbContext = await _store.CreateDbContextAsync(cancellationToken); |
| | 2 | 102 | | var set = dbContext.WorkflowInstances; |
| | 2 | 103 | | var queryable = Filter(set.AsQueryable(), filter).OrderBy(order); |
| | 2 | 104 | | var count = await queryable.LongCountAsync(cancellationToken); |
| | 2 | 105 | | queryable = queryable.Paginate(pageArgs); |
| | 2 | 106 | | var entities = await queryable.Select(WorkflowInstanceSummary.FromInstanceExpression()).ToListAsync(cancellation |
| | | 107 | | |
| | 2 | 108 | | return Page.Of(entities, count); |
| | 2 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public async ValueTask<IEnumerable<WorkflowInstanceSummary>> SummarizeManyAsync(WorkflowInstanceFilter filter, Cance |
| | | 113 | | { |
| | 6 | 114 | | var orderBy = new WorkflowInstanceOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 6 | 115 | | return await SummarizeManyAsync(filter, orderBy, cancellationToken); |
| | 6 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public async ValueTask<IEnumerable<WorkflowInstanceSummary>> SummarizeManyAsync<TOrderBy>(WorkflowInstanceFilter fil |
| | | 120 | | { |
| | 18 | 121 | | return await _store.QueryAsync(query => Filter(query, filter).OrderBy(order), WorkflowInstanceSummary.FromInstan |
| | 6 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <inheritdoc /> |
| | | 125 | | public async ValueTask<IEnumerable<string>> FindManyIdsAsync(WorkflowInstanceFilter filter, CancellationToken cancel |
| | | 126 | | { |
| | 6 | 127 | | var entities = await _store.QueryAsync(query => Filter(query, filter).OrderBy(x => x.CreatedAt), WorkflowInstanc |
| | 4 | 128 | | return entities.Select(x => x.Id).ToList(); |
| | 2 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | public async ValueTask<Page<string>> FindManyIdsAsync(WorkflowInstanceFilter filter, PageArgs pageArgs, Cancellation |
| | | 133 | | { |
| | 0 | 134 | | var orderBy = new WorkflowInstanceOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 0 | 135 | | return await FindManyIdsAsync(filter, pageArgs, orderBy, cancellationToken); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc /> |
| | | 139 | | public async ValueTask<Page<string>> FindManyIdsAsync<TOrderBy>(WorkflowInstanceFilter filter, PageArgs pageArgs, Wo |
| | | 140 | | { |
| | 0 | 141 | | await using var dbContext = await _store.CreateDbContextAsync(cancellationToken); |
| | 0 | 142 | | var set = dbContext.WorkflowInstances; |
| | 0 | 143 | | var queryable = Filter(set.AsQueryable(), filter).OrderBy(order); |
| | 0 | 144 | | var count = await queryable.LongCountAsync(cancellationToken); |
| | 0 | 145 | | queryable = queryable.Paginate(pageArgs); |
| | 0 | 146 | | var entities = await queryable.Select(WorkflowInstanceId.FromInstanceExpression()).ToListAsync(cancellationToken |
| | 0 | 147 | | var ids = entities.Select(x => x.Id).ToList(); |
| | | 148 | | |
| | 0 | 149 | | return Page.Of(ids, count); |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <inheritdoc /> |
| | | 153 | | public async ValueTask<long> DeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = defaul |
| | | 154 | | { |
| | 26 | 155 | | return await _store.DeleteWhereAsync(query => Filter(query, filter), cancellationToken); |
| | 13 | 156 | | } |
| | | 157 | | |
| | | 158 | | public async Task UpdateUpdatedTimestampAsync(string workflowInstanceId, DateTimeOffset value, CancellationToken can |
| | | 159 | | { |
| | 0 | 160 | | var entity = new WorkflowInstance |
| | 0 | 161 | | { |
| | 0 | 162 | | Id = workflowInstanceId, |
| | 0 | 163 | | UpdatedAt = value |
| | 0 | 164 | | }; |
| | | 165 | | |
| | 0 | 166 | | await using var dbContext = await _store.CreateDbContextAsync(cancellationToken); |
| | 0 | 167 | | dbContext.Attach(entity); |
| | 0 | 168 | | dbContext.Entry(entity).Property(x => x.UpdatedAt).IsModified = true; |
| | | 169 | | |
| | | 170 | | try |
| | | 171 | | { |
| | 0 | 172 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 173 | | } |
| | 0 | 174 | | catch (DbUpdateConcurrencyException e) |
| | | 175 | | { |
| | 0 | 176 | | foreach (var entry in e.Entries) |
| | | 177 | | { |
| | 0 | 178 | | var proposedValues = entry.CurrentValues; |
| | 0 | 179 | | var databaseValues = await entry.GetDatabaseValuesAsync(cancellationToken); |
| | | 180 | | |
| | 0 | 181 | | if(databaseValues == null) |
| | | 182 | | continue; |
| | | 183 | | |
| | 0 | 184 | | var updatedAtProperty = entry.Metadata.GetProperty(nameof(WorkflowInstance.UpdatedAt)); |
| | 0 | 185 | | var proposedValue = (DateTimeOffset)proposedValues[updatedAtProperty]!; |
| | 0 | 186 | | var databaseValue = (DateTimeOffset)databaseValues[updatedAtProperty]!; |
| | | 187 | | |
| | 0 | 188 | | if (proposedValue > databaseValue) |
| | 0 | 189 | | proposedValues[updatedAtProperty] = proposedValue; |
| | | 190 | | |
| | 0 | 191 | | entry.OriginalValues.SetValues(databaseValues); |
| | 0 | 192 | | } |
| | | 193 | | } |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <inheritdoc /> |
| | | 197 | | [RequiresUnreferencedCode("Calls Elsa.Workflows.Contracts.IWorkflowStateSerializer.SerializeAsync(WorkflowState, Can |
| | | 198 | | public async ValueTask SaveAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 199 | | { |
| | 349 | 200 | | await _store.SaveAsync(instance, OnSaveAsync, cancellationToken); |
| | 349 | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <inheritdoc /> |
| | | 204 | | public async ValueTask AddAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 205 | | { |
| | 0 | 206 | | await _store.AddAsync(instance, OnSaveAsync, cancellationToken); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <inheritdoc /> |
| | | 210 | | public async ValueTask UpdateAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 211 | | { |
| | 0 | 212 | | await _store.UpdateAsync(instance, OnSaveAsync, cancellationToken); |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <inheritdoc /> |
| | | 216 | | public async ValueTask SaveManyAsync(IEnumerable<WorkflowInstance> instances, CancellationToken cancellationToken = |
| | | 217 | | { |
| | 0 | 218 | | await _store.SaveManyAsync(instances, OnSaveAsync, cancellationToken); |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | [RequiresUnreferencedCode("Calls Elsa.Workflows.Contracts.IWorkflowStateSerializer.SerializeAsync(WorkflowState, Can |
| | | 222 | | private async ValueTask OnSaveAsync(ManagementElsaDbContext managementElsaDbContext, WorkflowInstance entity, Cancel |
| | | 223 | | { |
| | 349 | 224 | | var data = entity.WorkflowState; |
| | 349 | 225 | | var json = _workflowStateSerializer.Serialize(data); |
| | 349 | 226 | | var compressionAlgorithm = _options.Value.CompressionAlgorithm ?? nameof(None); |
| | 349 | 227 | | var compressionCodec = _compressionCodecResolver.Resolve(compressionAlgorithm); |
| | 349 | 228 | | var compressedJson = await compressionCodec.CompressAsync(json, cancellationToken); |
| | | 229 | | |
| | 349 | 230 | | managementElsaDbContext.Entry(entity).Property("Data").CurrentValue = compressedJson; |
| | 349 | 231 | | managementElsaDbContext.Entry(entity).Property("DataCompressionAlgorithm").CurrentValue = compressionAlgorithm; |
| | 349 | 232 | | } |
| | | 233 | | |
| | | 234 | | private async ValueTask OnLoadAsync(ManagementElsaDbContext managementElsaDbContext, WorkflowInstance? entity, Cance |
| | | 235 | | { |
| | 95 | 236 | | if (entity == null) |
| | 0 | 237 | | return; |
| | | 238 | | |
| | 95 | 239 | | var data = entity.WorkflowState; |
| | 95 | 240 | | var json = (string?)managementElsaDbContext.Entry(entity).Property("Data").CurrentValue; |
| | 95 | 241 | | var compressionAlgorithm = (string?)managementElsaDbContext.Entry(entity).Property("DataCompressionAlgorithm").C |
| | 95 | 242 | | var compressionStrategy = _compressionCodecResolver.Resolve(compressionAlgorithm); |
| | | 243 | | |
| | | 244 | | try |
| | | 245 | | { |
| | 95 | 246 | | if (!string.IsNullOrWhiteSpace(json)) |
| | | 247 | | { |
| | 95 | 248 | | json = await compressionStrategy.DecompressAsync(json, cancellationToken); |
| | 95 | 249 | | data = _workflowStateSerializer.Deserialize(json); |
| | | 250 | | } |
| | 95 | 251 | | } |
| | 0 | 252 | | catch (Exception exp) |
| | | 253 | | { |
| | 0 | 254 | | _logger.LogWarning(exp, "Exception while deserializing workflow instance state: {InstanceId}. Reverting to d |
| | 0 | 255 | | } |
| | 95 | 256 | | entity.WorkflowState = data; |
| | 95 | 257 | | } |
| | | 258 | | |
| | | 259 | | private static IQueryable<WorkflowInstance> Filter(IQueryable<WorkflowInstance> query, WorkflowInstanceFilter filter |
| | | 260 | | { |
| | 141 | 261 | | return filter.Apply(query); |
| | | 262 | | } |
| | | 263 | | } |