| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.Management.Filters; |
| | | 4 | | using Elsa.Workflows.Management.Models; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.Caching.Memory; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Management.Services; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Decorates an <see cref="IWorkflowDefinitionService"/> with caching capabilities. |
| | | 13 | | /// </summary> |
| | | 14 | | [UsedImplicitly] |
| | 454 | 15 | | public class CachingWorkflowDefinitionService( |
| | 454 | 16 | | IWorkflowDefinitionService decoratedService, |
| | 454 | 17 | | IWorkflowDefinitionCacheManager cacheManager, |
| | 454 | 18 | | IWorkflowDefinitionStore workflowDefinitionStore, |
| | 454 | 19 | | IMaterializerRegistry materializerRegistry) : IWorkflowDefinitionService |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public Task<WorkflowGraph> MaterializeWorkflowAsync(WorkflowDefinition definition, CancellationToken cancellationTok |
| | | 23 | | { |
| | 2269 | 24 | | return decoratedService.MaterializeWorkflowAsync(definition, cancellationToken); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(string definitionId, VersionOptions versionOption |
| | | 29 | | { |
| | 4 | 30 | | var cacheKey = cacheManager.CreateWorkflowDefinitionVersionCacheKey(definitionId, versionOptions); |
| | | 31 | | |
| | 4 | 32 | | return await FindFromCacheAsync(cacheKey, |
| | 4 | 33 | | () => decoratedService.FindWorkflowDefinitionAsync(definitionId, versionOptions, cancellationToken), |
| | 8 | 34 | | x => x.DefinitionId); |
| | 4 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(string definitionVersionId, CancellationToken can |
| | | 39 | | { |
| | 2 | 40 | | var cacheKey = cacheManager.CreateWorkflowDefinitionVersionCacheKey(definitionVersionId); |
| | 2 | 41 | | return await FindFromCacheAsync(cacheKey, |
| | 2 | 42 | | () => decoratedService.FindWorkflowDefinitionAsync(definitionVersionId, cancellationToken), |
| | 4 | 43 | | x => x.DefinitionId); |
| | 2 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(WorkflowDefinitionHandle handle, CancellationToken canc |
| | | 48 | | { |
| | 1 | 49 | | var filter = handle.ToFilter(); |
| | 1 | 50 | | return FindWorkflowDefinitionAsync(filter, cancellationToken); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(WorkflowDefinitionFilter filter, CancellationToke |
| | | 55 | | { |
| | 1103 | 56 | | var cacheKey = cacheManager.CreateWorkflowDefinitionFilterCacheKey(filter); |
| | 1103 | 57 | | return await FindFromCacheAsync(cacheKey, |
| | 383 | 58 | | () => decoratedService.FindWorkflowDefinitionAsync(filter, cancellationToken), |
| | 1486 | 59 | | x => x.DefinitionId); |
| | 1103 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public async Task<WorkflowGraph?> FindWorkflowGraphAsync(string definitionId, VersionOptions versionOptions, Cancell |
| | | 64 | | { |
| | 36 | 65 | | var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definitionId, versionOptions); |
| | 36 | 66 | | return await FindFromCacheAsync(cacheKey, |
| | 19 | 67 | | () => decoratedService.FindWorkflowGraphAsync(definitionId, versionOptions, cancellationToken), |
| | 54 | 68 | | x => x.Workflow.Identity.DefinitionId); |
| | 36 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | | 72 | | public async Task<WorkflowGraph?> FindWorkflowGraphAsync(string definitionVersionId, CancellationToken cancellationT |
| | | 73 | | { |
| | 1225 | 74 | | var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definitionVersionId); |
| | 1225 | 75 | | return await FindFromCacheAsync(cacheKey, |
| | 125 | 76 | | () => decoratedService.FindWorkflowGraphAsync(definitionVersionId, cancellationToken), |
| | 1350 | 77 | | x => x.Workflow.Identity.DefinitionId); |
| | 1225 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | | 81 | | public Task<WorkflowGraph?> FindWorkflowGraphAsync(WorkflowDefinitionHandle definitionHandle, CancellationToken canc |
| | | 82 | | { |
| | 1 | 83 | | var filter = definitionHandle.ToFilter(); |
| | 1 | 84 | | return FindWorkflowGraphAsync(filter, cancellationToken); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <inheritdoc /> |
| | | 88 | | public async Task<WorkflowGraph?> FindWorkflowGraphAsync(WorkflowDefinitionFilter filter, CancellationToken cancella |
| | | 89 | | { |
| | | 90 | | // Resolve the definition first (already cached by FindWorkflowDefinitionAsync) so the |
| | | 91 | | // WorkflowGraph ends up under the stable per-version-ID key rather than a filter-hash key |
| | | 92 | | // that can never be shared with the other FindWorkflowGraphAsync overloads. |
| | 221 | 93 | | var definition = await FindWorkflowDefinitionAsync(filter, cancellationToken); |
| | 221 | 94 | | return await FindWorkflowGraphForDefinitionAsync(definition, cancellationToken); |
| | 221 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc /> |
| | | 98 | | public async Task<IEnumerable<WorkflowGraph>> FindWorkflowGraphsAsync(WorkflowDefinitionFilter filter, CancellationT |
| | | 99 | | { |
| | 3 | 100 | | var workflowDefinitions = await workflowDefinitionStore.FindManyAsync(filter, cancellationToken); |
| | 3 | 101 | | var workflowGraphs = new List<WorkflowGraph>(); |
| | 16 | 102 | | foreach (var workflowDefinition in workflowDefinitions) |
| | | 103 | | { |
| | 5 | 104 | | var workflowGraph = await FindWorkflowGraphForDefinitionAsync(workflowDefinition, cancellationToken); |
| | 5 | 105 | | if (workflowGraph != null) |
| | 4 | 106 | | workflowGraphs.Add(workflowGraph); |
| | | 107 | | } |
| | | 108 | | |
| | 3 | 109 | | return workflowGraphs; |
| | 3 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | | 113 | | public async Task<WorkflowGraphFindResult> TryFindWorkflowGraphAsync(string definitionId, VersionOptions versionOpti |
| | | 114 | | { |
| | 1 | 115 | | var definition = await FindWorkflowDefinitionAsync(definitionId, versionOptions, cancellationToken); |
| | 1 | 116 | | var workflowGraph = await FindWorkflowGraphForDefinitionAsync(definition, cancellationToken); |
| | 1 | 117 | | return new(definition, workflowGraph); |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc /> |
| | | 121 | | public async Task<WorkflowGraphFindResult> TryFindWorkflowGraphAsync(string definitionVersionId, CancellationToken c |
| | | 122 | | { |
| | 1 | 123 | | var definition = await FindWorkflowDefinitionAsync(definitionVersionId, cancellationToken); |
| | 1 | 124 | | var workflowGraph = await FindWorkflowGraphForDefinitionAsync(definition, cancellationToken); |
| | 1 | 125 | | return new(definition, workflowGraph); |
| | 1 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | | 129 | | public Task<WorkflowGraphFindResult> TryFindWorkflowGraphAsync(WorkflowDefinitionHandle definitionHandle, Cancellati |
| | | 130 | | { |
| | 207 | 131 | | var filter = definitionHandle.ToFilter(); |
| | 207 | 132 | | return TryFindWorkflowGraphAsync(filter, cancellationToken); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc /> |
| | | 136 | | public async Task<WorkflowGraphFindResult> TryFindWorkflowGraphAsync(WorkflowDefinitionFilter filter, CancellationTo |
| | | 137 | | { |
| | 208 | 138 | | var definition = await FindWorkflowDefinitionAsync(filter, cancellationToken); |
| | 208 | 139 | | var workflowGraph = await FindWorkflowGraphForDefinitionAsync(definition, cancellationToken); |
| | 208 | 140 | | return new(definition, workflowGraph); |
| | 208 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <inheritdoc /> |
| | | 144 | | public async Task<IEnumerable<WorkflowGraphFindResult>> TryFindWorkflowGraphsAsync(WorkflowDefinitionFilter filter, |
| | | 145 | | { |
| | 3 | 146 | | var workflowDefinitions = await workflowDefinitionStore.FindManyAsync(filter, cancellationToken); |
| | 3 | 147 | | var results = new List<WorkflowGraphFindResult>(); |
| | 14 | 148 | | foreach (var workflowDefinition in workflowDefinitions) |
| | | 149 | | { |
| | 4 | 150 | | var workflowGraph = await FindWorkflowGraphForDefinitionAsync(workflowDefinition, cancellationToken); |
| | 4 | 151 | | results.Add(new WorkflowGraphFindResult(workflowDefinition, workflowGraph)); |
| | 4 | 152 | | } |
| | | 153 | | |
| | 3 | 154 | | return results; |
| | 3 | 155 | | } |
| | | 156 | | |
| | | 157 | | private async Task<T?> FindFromCacheAsync<T>(string cacheKey, Func<Task<T?>> getObjectFunc, Func<T, string> getChang |
| | | 158 | | { |
| | 2370 | 159 | | var cache = cacheManager.Cache; |
| | 2370 | 160 | | return await cache.FindOrCreateAsync(cacheKey, async entry => |
| | 2370 | 161 | | { |
| | 533 | 162 | | entry.SetAbsoluteExpiration(cache.CachingOptions.Value.CacheDuration); |
| | 533 | 163 | | var obj = await getObjectFunc(); |
| | 2370 | 164 | | |
| | 533 | 165 | | if (obj != null) |
| | 2370 | 166 | | { |
| | 532 | 167 | | var changeTokenKey = cacheManager.CreateWorkflowDefinitionChangeTokenKey(getChangeTokenKeyFunc(obj)); |
| | 532 | 168 | | entry.AddExpirationToken(cache.GetToken(changeTokenKey)); |
| | 2370 | 169 | | } |
| | 2370 | 170 | | |
| | 533 | 171 | | return obj; |
| | 2903 | 172 | | }); |
| | 2370 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Returns the <see cref="WorkflowGraph"/> for the given definition, using the per-version-ID cache entry |
| | | 177 | | /// that is shared with <see cref="FindWorkflowGraphAsync(string,CancellationToken)"/>. |
| | | 178 | | /// Returns <c>null</c> when the definition is <c>null</c> or its materializer is unavailable. |
| | | 179 | | /// </summary> |
| | | 180 | | private async Task<WorkflowGraph?> FindWorkflowGraphForDefinitionAsync(WorkflowDefinition? definition, CancellationT |
| | | 181 | | { |
| | 440 | 182 | | if (definition == null) |
| | 0 | 183 | | return null; |
| | | 184 | | |
| | 440 | 185 | | if (!materializerRegistry.IsMaterializerAvailable(definition.MaterializerName)) |
| | 4 | 186 | | return null; |
| | | 187 | | |
| | 436 | 188 | | var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definition.Id); |
| | 436 | 189 | | var cache = cacheManager.Cache; |
| | 436 | 190 | | return await cache.GetOrCreateAsync(cacheKey, async entry => |
| | 436 | 191 | | { |
| | 248 | 192 | | entry.SetAbsoluteExpiration(cache.CachingOptions.Value.CacheDuration); |
| | 248 | 193 | | var graph = await MaterializeWorkflowAsync(definition, cancellationToken); |
| | 248 | 194 | | var changeTokenKey = cacheManager.CreateWorkflowDefinitionChangeTokenKey(graph.Workflow.Identity.DefinitionI |
| | 248 | 195 | | entry.AddExpirationToken(cache.GetToken(changeTokenKey)); |
| | 248 | 196 | | return graph; |
| | 684 | 197 | | }); |
| | 440 | 198 | | } |
| | | 199 | | |
| | | 200 | | } |