| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Workflows.Activities; |
| | | 4 | | using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity; |
| | | 5 | | using Elsa.Workflows.Management.Entities; |
| | | 6 | | using Elsa.Workflows.Management.Models; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Management.Services; |
| | | 10 | | |
| | | 11 | | internal record UpdatedWorkflowDefinition(WorkflowDefinition Definition, WorkflowGraph NewGraph); |
| | | 12 | | |
| | 405 | 13 | | public class WorkflowReferenceUpdater( |
| | 405 | 14 | | IWorkflowDefinitionPublisher publisher, |
| | 405 | 15 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 405 | 16 | | IWorkflowDefinitionStore workflowDefinitionStore, |
| | 405 | 17 | | IWorkflowReferenceGraphBuilder workflowReferenceGraphBuilder, |
| | 405 | 18 | | WorkflowDefinitionActivityDescriptorFactory workflowDefinitionActivityDescriptorFactory, |
| | 405 | 19 | | IActivityRegistry activityRegistry, |
| | 405 | 20 | | IApiSerializer serializer) |
| | | 21 | | : IWorkflowReferenceUpdater |
| | | 22 | | { |
| | | 23 | | private bool _isUpdating; |
| | | 24 | | |
| | | 25 | | public async Task<UpdateWorkflowReferencesResult> UpdateWorkflowReferencesAsync( |
| | | 26 | | WorkflowDefinition referencedDefinition, |
| | | 27 | | CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 5 | 29 | | if (_isUpdating || |
| | 5 | 30 | | referencedDefinition.Options is not { UsableAsActivity: true, AutoUpdateConsumingWorkflows: true }) |
| | 3 | 31 | | return new([]); |
| | | 32 | | |
| | 2 | 33 | | var referenceGraph = await workflowReferenceGraphBuilder.BuildGraphAsync(referencedDefinition.DefinitionId, canc |
| | | 34 | | |
| | | 35 | | // Get all consumer (source) and referenced (target) IDs from the edges |
| | 3 | 36 | | var referencingIds = referenceGraph.Edges.Select(e => e.Source).Distinct().ToList(); |
| | 3 | 37 | | var referencedIds = referenceGraph.Edges.Select(e => e.Target).Distinct().ToList(); |
| | | 38 | | |
| | 2 | 39 | | if (referencingIds.Count == 0) |
| | 1 | 40 | | return new([]); |
| | | 41 | | |
| | 1 | 42 | | var referencingWorkflowGraphs = (await workflowDefinitionService.FindWorkflowGraphsAsync(new() |
| | 1 | 43 | | { |
| | 1 | 44 | | DefinitionIds = referencingIds, |
| | 1 | 45 | | VersionOptions = VersionOptions.Latest, |
| | 1 | 46 | | IsReadonly = false |
| | 1 | 47 | | }, cancellationToken)) |
| | 2 | 48 | | .ToDictionary(g => g.Workflow.Identity.DefinitionId); |
| | | 49 | | |
| | 1 | 50 | | var referencedWorkflowDefinitionList = (await workflowDefinitionStore.FindManyAsync(new() |
| | 1 | 51 | | { |
| | 1 | 52 | | DefinitionIds = referencedIds, |
| | 1 | 53 | | VersionOptions = VersionOptions.Published, |
| | 1 | 54 | | IsReadonly = false |
| | 1 | 55 | | }, cancellationToken)).ToList(); |
| | | 56 | | |
| | 1 | 57 | | var referencedWorkflowDefinitionsPublished = referencedWorkflowDefinitionList |
| | 1 | 58 | | .GroupBy(x => x.DefinitionId) |
| | 1 | 59 | | .Select(group => |
| | 1 | 60 | | { |
| | 2 | 61 | | var publishedVersion = group.FirstOrDefault(x => x.IsPublished); |
| | 1 | 62 | | return publishedVersion ?? group.First(); |
| | 1 | 63 | | }) |
| | 2 | 64 | | .ToDictionary(d => d.DefinitionId); |
| | | 65 | | |
| | 1 | 66 | | var initialPublicationState = new Dictionary<string, bool>(); |
| | | 67 | | |
| | 4 | 68 | | foreach (var workflowGraph in referencingWorkflowGraphs) |
| | 1 | 69 | | initialPublicationState[workflowGraph.Key] = workflowGraph.Value.Workflow.Publication.IsPublished; |
| | | 70 | | |
| | | 71 | | // Add the initially referenced definition |
| | 1 | 72 | | referencedWorkflowDefinitionsPublished[referencedDefinition.DefinitionId] = referencedDefinition; |
| | | 73 | | |
| | | 74 | | // Use the OutboundEdges lookup from the graph (Source → what it depends on) |
| | 1 | 75 | | var dependencyMap = referenceGraph.OutboundEdges; |
| | | 76 | | |
| | | 77 | | // Perform topological sort to ensure dependent workflows are processed in the right order |
| | 1 | 78 | | var sortedWorkflowIds = referencingIds |
| | 2 | 79 | | .TSort(id => dependencyMap[id], true) |
| | 1 | 80 | | // Only process workflows that exist in our referencing workflows dictionary |
| | 2 | 81 | | .Where(id => referencingWorkflowGraphs.ContainsKey(id)) |
| | 1 | 82 | | .ToList(); |
| | | 83 | | |
| | 1 | 84 | | var updatedWorkflows = new Dictionary<string, UpdatedWorkflowDefinition>(); |
| | | 85 | | |
| | | 86 | | // Create a cache for drafts that we've already created during this operation |
| | 1 | 87 | | var draftCache = new Dictionary<string, WorkflowDefinition>(); |
| | | 88 | | |
| | 4 | 89 | | foreach (var id in sortedWorkflowIds) |
| | | 90 | | { |
| | 1 | 91 | | if (!referencingWorkflowGraphs.TryGetValue(id, out var graph) || !dependencyMap[id].Any()) |
| | | 92 | | continue; |
| | | 93 | | |
| | 4 | 94 | | foreach (var refId in dependencyMap[id]) |
| | | 95 | | { |
| | 1 | 96 | | var target = referencedWorkflowDefinitionsPublished.GetValueOrDefault(refId); |
| | 1 | 97 | | if (target == null) continue; |
| | | 98 | | |
| | 1 | 99 | | var updated = await UpdateWorkflowAsync(graph, target, draftCache, initialPublicationState, cancellation |
| | 1 | 100 | | if (updated == null) continue; |
| | | 101 | | |
| | 1 | 102 | | graph = updated.NewGraph; |
| | 1 | 103 | | updatedWorkflows[updated.Definition.DefinitionId] = updated; |
| | 1 | 104 | | referencedWorkflowDefinitionsPublished[id] = updated.Definition; |
| | 1 | 105 | | draftCache[id] = updated.Definition; |
| | 1 | 106 | | referencingWorkflowGraphs[id] = updated.NewGraph; |
| | | 107 | | } |
| | 1 | 108 | | } |
| | | 109 | | |
| | 1 | 110 | | _isUpdating = true; |
| | 4 | 111 | | foreach (var updatedWorkflow in updatedWorkflows.Values) |
| | | 112 | | { |
| | 1 | 113 | | var requiresPublication = initialPublicationState.GetValueOrDefault(updatedWorkflow.Definition.DefinitionId) |
| | 1 | 114 | | if (requiresPublication) |
| | 1 | 115 | | await publisher.PublishAsync(updatedWorkflow.Definition, cancellationToken); |
| | | 116 | | else |
| | 0 | 117 | | await publisher.SaveDraftAsync(updatedWorkflow.Definition, cancellationToken); |
| | | 118 | | } |
| | | 119 | | |
| | 1 | 120 | | _isUpdating = false; |
| | | 121 | | |
| | 2 | 122 | | return new(updatedWorkflows.Select(u => u.Value.Definition)); |
| | 5 | 123 | | } |
| | | 124 | | |
| | | 125 | | |
| | | 126 | | private async Task<UpdatedWorkflowDefinition?> UpdateWorkflowAsync( |
| | | 127 | | WorkflowGraph graph, |
| | | 128 | | WorkflowDefinition target, |
| | | 129 | | Dictionary<string, WorkflowDefinition> draftCache, |
| | | 130 | | Dictionary<string, bool> initialPublicationState, |
| | | 131 | | CancellationToken cancellationToken) |
| | | 132 | | { |
| | 1 | 133 | | var willTargetBePublished = initialPublicationState.GetValueOrDefault(target.DefinitionId, target.IsPublished); |
| | 1 | 134 | | if (!willTargetBePublished) |
| | 0 | 135 | | return null; |
| | | 136 | | |
| | 1 | 137 | | var id = graph.Workflow.Identity.DefinitionId; |
| | 1 | 138 | | var draft = await GetOrCreateDraftAsync(id, draftCache, cancellationToken); |
| | 1 | 139 | | if (draft == null) return null; |
| | | 140 | | |
| | 1 | 141 | | var newGraph = await workflowDefinitionService.MaterializeWorkflowAsync(draft, cancellationToken); |
| | 1 | 142 | | var outdated = FindActivities(newGraph.Root, target.DefinitionId) |
| | 1 | 143 | | .Where(a => a.WorkflowDefinitionVersionId != target.Id) |
| | 1 | 144 | | .ToList(); |
| | | 145 | | |
| | 1 | 146 | | if (!outdated.Any()) return null; |
| | | 147 | | |
| | 4 | 148 | | foreach (var act in outdated) |
| | | 149 | | { |
| | 1 | 150 | | act.WorkflowDefinitionVersionId = target.Id; |
| | 1 | 151 | | act.Version = target.Version; |
| | 1 | 152 | | act.LatestAvailablePublishedVersionId = target.Id; |
| | 1 | 153 | | act.LatestAvailablePublishedVersion = target.Version; |
| | | 154 | | } |
| | | 155 | | |
| | 1 | 156 | | if (newGraph.Root.Activity is Workflow wf) |
| | 1 | 157 | | draft.StringData = serializer.Serialize(wf.Root); |
| | | 158 | | |
| | 1 | 159 | | return new(draft, newGraph); |
| | 1 | 160 | | } |
| | | 161 | | |
| | | 162 | | private async Task<WorkflowDefinition?> GetOrCreateDraftAsync( |
| | | 163 | | string definitionId, |
| | | 164 | | Dictionary<string, WorkflowDefinition> draftCache, |
| | | 165 | | CancellationToken cancellationToken) |
| | | 166 | | { |
| | | 167 | | // Check if we already have a draft for this workflow |
| | 1 | 168 | | if (draftCache.TryGetValue(definitionId, out var cachedDraft)) |
| | 0 | 169 | | return cachedDraft; |
| | | 170 | | |
| | | 171 | | // Create or get a draft for this workflow |
| | 1 | 172 | | var draft = await publisher.GetDraftAsync(definitionId, VersionOptions.Latest, cancellationToken); |
| | 1 | 173 | | if (draft == null) return null; |
| | | 174 | | |
| | | 175 | | // Store the draft in the cache for potential future use |
| | 1 | 176 | | draftCache[definitionId] = draft; |
| | | 177 | | |
| | | 178 | | // Get the current published version of the workflow definition. |
| | 1 | 179 | | var publishedVersion = await workflowDefinitionStore.FindAsync( |
| | 1 | 180 | | WorkflowDefinitionHandle.ByDefinitionId(definitionId, VersionOptions.Published).ToFilter(), |
| | 1 | 181 | | cancellationToken); |
| | | 182 | | |
| | | 183 | | // Update the activity registry to be able to materialize the workflow. |
| | 1 | 184 | | var activityDescriptor = workflowDefinitionActivityDescriptorFactory.CreateDescriptor(draft, publishedVersion); |
| | 1 | 185 | | activityRegistry.Add(typeof(WorkflowDefinitionActivityProvider), activityDescriptor); |
| | | 186 | | |
| | 1 | 187 | | return draft; |
| | 1 | 188 | | } |
| | | 189 | | |
| | | 190 | | private static IEnumerable<WorkflowDefinitionActivity> FindActivities(ActivityNode node, string definitionId) |
| | | 191 | | { |
| | | 192 | | // Do not drill into activities that are WorkflowDefinitionActivity |
| | 5 | 193 | | if (node.Activity is WorkflowDefinitionActivity) |
| | 1 | 194 | | yield break; |
| | | 195 | | |
| | 16 | 196 | | foreach (var child in node.Children) |
| | | 197 | | { |
| | 4 | 198 | | if (child.Activity is WorkflowDefinitionActivity activity && activity.WorkflowDefinitionId == definitionId) |
| | 1 | 199 | | yield return activity; |
| | 10 | 200 | | foreach (var grandChildActivity in FindActivities(child, definitionId)) |
| | 1 | 201 | | yield return grandChildActivity; |
| | 4 | 202 | | } |
| | 4 | 203 | | } |
| | | 204 | | } |