| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity; |
| | | 3 | | using Elsa.Workflows.Management.Filters; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Management.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents the default implementation of <see cref="IWorkflowReferenceQuery"/> that queries workflows |
| | | 10 | | /// referencing a specific workflow definition. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// This class is designed to identify workflows that are dependent on a given workflow definition. |
| | | 14 | | /// It leverages services such as <see cref="IWorkflowDefinitionService"/> and <see cref="IWorkflowDefinitionStore"/> |
| | | 15 | | /// to locate, graph, and analyze workflows that include the specified workflow definition. |
| | | 16 | | /// </remarks> |
| | 320 | 17 | | public class DefaultWorkflowReferenceQuery(IWorkflowDefinitionService workflowDefinitionService, IWorkflowDefinitionStor |
| | | 18 | | { |
| | | 19 | | public async Task<IEnumerable<string>> ExecuteAsync(string workflowDefinitionId, CancellationToken cancellationToken |
| | | 20 | | { |
| | 3 | 21 | | var workflowGraphs = await FindWorkflowsContainingUpdatedWorkflowDefinitionAsync(workflowDefinitionId, cancellat |
| | 4 | 22 | | return workflowGraphs.Select(x => x.Workflow.Identity.DefinitionId).Distinct(); |
| | 3 | 23 | | } |
| | | 24 | | |
| | | 25 | | private async Task<IEnumerable<WorkflowGraph>> FindWorkflowsContainingUpdatedWorkflowDefinitionAsync(string updatedW |
| | | 26 | | { |
| | 3 | 27 | | var allWorkflowGraphs = (await GetAllWorkflowGraphsAsync(cancellationToken)).ToList(); |
| | 3 | 28 | | var filteredWorkflowGraphs = allWorkflowGraphs |
| | 241 | 29 | | .Where(x => FindWorkflowActivityDefinitionActivityNodes(x.Root).Any(y => y.WorkflowDefinitionId == updatedWo |
| | 3 | 30 | | .ToList(); |
| | | 31 | | |
| | 3 | 32 | | return filteredWorkflowGraphs; |
| | 3 | 33 | | } |
| | | 34 | | |
| | | 35 | | private IEnumerable<WorkflowDefinitionActivity> FindWorkflowActivityDefinitionActivityNodes(ActivityNode parent) |
| | | 36 | | { |
| | 2776 | 37 | | foreach (var child in parent.Children) |
| | | 38 | | { |
| | 595 | 39 | | if (child.Activity is WorkflowDefinitionActivity workflowDefinitionActivity) |
| | 21 | 40 | | yield return workflowDefinitionActivity; |
| | | 41 | | else |
| | | 42 | | { |
| | 1189 | 43 | | foreach (var grandChild in FindWorkflowActivityDefinitionActivityNodes(child)) |
| | 21 | 44 | | yield return grandChild; |
| | | 45 | | } |
| | | 46 | | } |
| | 792 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async Task<IEnumerable<WorkflowGraph>> GetAllWorkflowGraphsAsync(CancellationToken cancellationToken) |
| | | 50 | | { |
| | 3 | 51 | | var workflowDefinitionFilter = new WorkflowDefinitionFilter |
| | 3 | 52 | | { |
| | 3 | 53 | | VersionOptions = VersionOptions.LatestOrPublished, |
| | 3 | 54 | | IsReadonly = false |
| | 3 | 55 | | }; |
| | 3 | 56 | | var workflowDefinitionSummaries = await workflowDefinitionStore.FindSummariesAsync(workflowDefinitionFilter, can |
| | | 57 | | |
| | | 58 | | // If there are workflow definition summaries with the same definition ID, only take the latest version. |
| | 3 | 59 | | workflowDefinitionSummaries = workflowDefinitionSummaries |
| | 221 | 60 | | .GroupBy(x => x.DefinitionId) |
| | 441 | 61 | | .Select(x => x.OrderByDescending(y => y.Version).First()) |
| | 3 | 62 | | .ToList(); |
| | | 63 | | |
| | 3 | 64 | | var workflowGraphs = new List<WorkflowGraph>(); |
| | | 65 | | |
| | 446 | 66 | | foreach (var workflowDefinitionSummary in workflowDefinitionSummaries) |
| | | 67 | | { |
| | 220 | 68 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(workflowDefinitionSummary.Id, can |
| | | 69 | | |
| | 220 | 70 | | if (workflowGraph != null) |
| | 220 | 71 | | workflowGraphs.Add(workflowGraph); |
| | | 72 | | } |
| | | 73 | | |
| | 3 | 74 | | return workflowGraphs; |
| | 3 | 75 | | } |
| | | 76 | | } |