| | | 1 | | using Elsa.Workflows.Management.Models; |
| | | 2 | | using Elsa.Workflows.Management.Options; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Management.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Default implementation of <see cref="IWorkflowReferenceGraphBuilder"/> that uses <see cref="IWorkflowReferenceQuery" |
| | | 9 | | /// to recursively build a complete graph of workflow references. |
| | | 10 | | /// </summary> |
| | 424 | 11 | | public class WorkflowReferenceGraphBuilder(IWorkflowReferenceQuery workflowReferenceQuery, IOptions<WorkflowReferenceGra |
| | | 12 | | { |
| | 424 | 13 | | private readonly WorkflowReferenceGraphOptions _options = options.Value; |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public async Task<WorkflowReferenceGraph> BuildGraphAsync(string definitionId, CancellationToken cancellationToken = |
| | | 16 | | { |
| | 13 | 17 | | var edges = await BuildEdgesAsync(definitionId, cancellationToken).ToListAsync(cancellationToken); |
| | 13 | 18 | | return new([definitionId], edges); |
| | 13 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public async Task<WorkflowReferenceGraph> BuildGraphAsync(IEnumerable<string> definitionIds, CancellationToken cance |
| | | 23 | | { |
| | 5 | 24 | | var allEdges = new List<WorkflowReferenceEdge>(); |
| | 5 | 25 | | var visitedIds = new HashSet<string>(); |
| | 5 | 26 | | var rootIds = definitionIds.ToList(); |
| | | 27 | | |
| | 22 | 28 | | foreach (var definitionId in rootIds) |
| | | 29 | | { |
| | 30 | 30 | | await foreach (var edge in BuildEdgesAsync(definitionId, cancellationToken, visitedIds)) |
| | | 31 | | { |
| | 9 | 32 | | allEdges.Add(edge); |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // Deduplicate edges |
| | 5 | 37 | | var distinctEdges = allEdges.Distinct().ToList(); |
| | | 38 | | |
| | 5 | 39 | | return new(rootIds, distinctEdges); |
| | 5 | 40 | | } |
| | | 41 | | |
| | | 42 | | private async IAsyncEnumerable<WorkflowReferenceEdge> BuildEdgesAsync( |
| | | 43 | | string definitionId, |
| | | 44 | | [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken, |
| | | 45 | | HashSet<string>? visitedIds = null, |
| | | 46 | | int currentDepth = 0) |
| | | 47 | | { |
| | 52 | 48 | | visitedIds ??= new(); |
| | | 49 | | |
| | | 50 | | // Check depth limit |
| | 52 | 51 | | if (_options.MaxDepth > 0 && currentDepth >= _options.MaxDepth) |
| | 1 | 52 | | yield break; |
| | | 53 | | |
| | | 54 | | // Check max definitions limit |
| | 51 | 55 | | if (_options.MaxDefinitions > 0 && visitedIds.Count >= _options.MaxDefinitions) |
| | 1 | 56 | | yield break; |
| | | 57 | | |
| | | 58 | | // If we've already processed this definition ID, skip it to prevent infinite recursion. |
| | 50 | 59 | | if (!visitedIds.Add(definitionId)) |
| | 6 | 60 | | yield break; |
| | | 61 | | |
| | 44 | 62 | | var consumerIds = (await workflowReferenceQuery.ExecuteAsync(definitionId, cancellationToken)).ToList(); |
| | | 63 | | |
| | | 64 | | // For each consumer, create an edge: Consumer (Source) → definitionId (Target) |
| | 154 | 65 | | foreach (var consumerId in consumerIds) |
| | | 66 | | { |
| | 33 | 67 | | yield return new(Source: consumerId, Target: definitionId); |
| | | 68 | | |
| | | 69 | | // Recursively process the consumer |
| | 98 | 70 | | await foreach (var childEdge in BuildEdgesAsync(consumerId, cancellationToken, visitedIds, currentDepth + 1) |
| | 16 | 71 | | yield return childEdge; |
| | 33 | 72 | | } |
| | 52 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |