| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Mediator.Contracts; |
| | | 5 | | using Elsa.Workflows.Activities; |
| | | 6 | | using Elsa.Workflows.Helpers; |
| | | 7 | | using Elsa.Workflows.Management; |
| | | 8 | | using Elsa.Workflows.Management.Entities; |
| | | 9 | | using Elsa.Workflows.Runtime.Comparers; |
| | | 10 | | using Elsa.Workflows.Runtime.Entities; |
| | | 11 | | using Elsa.Workflows.Runtime.Filters; |
| | | 12 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | using Open.Linq.AsyncExtensions; |
| | | 15 | | |
| | | 16 | | namespace Elsa.Workflows.Runtime; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public class TriggerIndexer : ITriggerIndexer |
| | | 20 | | { |
| | | 21 | | private readonly IActivityVisitor _activityVisitor; |
| | | 22 | | private readonly IWorkflowDefinitionService _workflowDefinitionService; |
| | | 23 | | private readonly IExpressionEvaluator _expressionEvaluator; |
| | | 24 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 25 | | private readonly ITriggerStore _triggerStore; |
| | | 26 | | private readonly IActivityRegistry _activityRegistry; |
| | | 27 | | private readonly INotificationSender _notificationSender; |
| | | 28 | | private readonly IServiceProvider _serviceProvider; |
| | | 29 | | private readonly IStimulusHasher _hasher; |
| | | 30 | | private readonly ILogger _logger; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Constructor. |
| | | 34 | | /// </summary> |
| | 478 | 35 | | public TriggerIndexer( |
| | 478 | 36 | | IActivityVisitor activityVisitor, |
| | 478 | 37 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 478 | 38 | | IExpressionEvaluator expressionEvaluator, |
| | 478 | 39 | | IIdentityGenerator identityGenerator, |
| | 478 | 40 | | ITriggerStore triggerStore, |
| | 478 | 41 | | IActivityRegistry activityRegistry, |
| | 478 | 42 | | INotificationSender notificationSender, |
| | 478 | 43 | | IServiceProvider serviceProvider, |
| | 478 | 44 | | IStimulusHasher hasher, |
| | 478 | 45 | | ILogger<TriggerIndexer> logger) |
| | | 46 | | { |
| | 478 | 47 | | _activityVisitor = activityVisitor; |
| | 478 | 48 | | _expressionEvaluator = expressionEvaluator; |
| | 478 | 49 | | _identityGenerator = identityGenerator; |
| | 478 | 50 | | _triggerStore = triggerStore; |
| | 478 | 51 | | _activityRegistry = activityRegistry; |
| | 478 | 52 | | _notificationSender = notificationSender; |
| | 478 | 53 | | _serviceProvider = serviceProvider; |
| | 478 | 54 | | _hasher = hasher; |
| | 478 | 55 | | _logger = logger; |
| | 478 | 56 | | _workflowDefinitionService = workflowDefinitionService; |
| | 478 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public async Task DeleteTriggersAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 61 | | { |
| | 6 | 62 | | var triggers = (await _triggerStore.FindManyAsync(filter, cancellationToken)).ToList(); |
| | 13 | 63 | | var workflowDefinitionVersionIds = triggers.Select(x => x.WorkflowDefinitionVersionId).Distinct().ToList(); |
| | | 64 | | |
| | 26 | 65 | | foreach (var workflowDefinitionVersionId in workflowDefinitionVersionIds) |
| | | 66 | | { |
| | | 67 | | try |
| | | 68 | | { |
| | 7 | 69 | | var workflowGraph = await _workflowDefinitionService.FindWorkflowGraphAsync(workflowDefinitionVersionId, |
| | | 70 | | |
| | 3 | 71 | | if (workflowGraph == null) |
| | 0 | 72 | | continue; |
| | | 73 | | |
| | 3 | 74 | | await DeleteTriggersAsync(workflowGraph.Workflow, cancellationToken); |
| | 3 | 75 | | } |
| | 4 | 76 | | catch (Exception ex) |
| | | 77 | | { |
| | 4 | 78 | | _logger.LogWarning(ex, "Failed to load workflow graph for workflow definition version {WorkflowDefinitio |
| | 4 | 79 | | } |
| | 7 | 80 | | } |
| | 6 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public async Task<IndexedWorkflowTriggers> IndexTriggersAsync(WorkflowDefinition definition, CancellationToken cance |
| | | 85 | | { |
| | 637 | 86 | | var workflowGraph = await _workflowDefinitionService.MaterializeWorkflowAsync(definition, cancellationToken); |
| | 637 | 87 | | return await IndexTriggersAsync(workflowGraph.Workflow, cancellationToken); |
| | 637 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | public async Task<IndexedWorkflowTriggers> IndexTriggersAsync(Workflow workflow, CancellationToken cancellationToken |
| | | 92 | | { |
| | | 93 | | // Get current triggers |
| | 637 | 94 | | var currentTriggers = await GetCurrentTriggersAsync(workflow.Identity.DefinitionId, cancellationToken).ToList(); |
| | | 95 | | |
| | | 96 | | // Collect new triggers **if the workflow is published**. |
| | 637 | 97 | | var newTriggers = workflow.Publication.IsPublished |
| | 637 | 98 | | ? await GetTriggersInternalAsync(workflow, cancellationToken).ToListAsync(cancellationToken) |
| | 637 | 99 | | : new(0); |
| | | 100 | | |
| | | 101 | | // Diff triggers. |
| | 637 | 102 | | var diff = Diff.For(currentTriggers, newTriggers, new WorkflowTriggerEqualityComparer()); |
| | | 103 | | |
| | | 104 | | // Replace triggers for the specified workflow. |
| | 637 | 105 | | await _triggerStore.ReplaceAsync(diff.Removed, diff.Added, cancellationToken); |
| | | 106 | | |
| | 637 | 107 | | var indexedWorkflow = new IndexedWorkflowTriggers(workflow, diff.Added, diff.Removed, diff.Unchanged); |
| | | 108 | | |
| | | 109 | | // Publish event. |
| | 637 | 110 | | await _notificationSender.SendAsync(new WorkflowTriggersIndexed(indexedWorkflow), cancellationToken); |
| | 637 | 111 | | return indexedWorkflow; |
| | 637 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | | 115 | | public async Task<IEnumerable<StoredTrigger>> GetTriggersAsync(Workflow workflow, CancellationToken cancellationToke |
| | | 116 | | { |
| | 54 | 117 | | return await GetTriggersInternalAsync(workflow, cancellationToken).ToListAsync(cancellationToken); |
| | 54 | 118 | | } |
| | | 119 | | |
| | | 120 | | private async Task DeleteTriggersAsync(Workflow workflow, CancellationToken cancellationToken = default) |
| | | 121 | | { |
| | 3 | 122 | | var emptyTriggerList = new List<StoredTrigger>(0); |
| | 3 | 123 | | var currentTriggers = await GetCurrentTriggersAsync(workflow.Identity.DefinitionId, cancellationToken).ToList(); |
| | 3 | 124 | | var diff = Diff.For(currentTriggers, emptyTriggerList, new WorkflowTriggerEqualityComparer()); |
| | 3 | 125 | | await _triggerStore.ReplaceAsync(diff.Removed, diff.Added, cancellationToken); |
| | 3 | 126 | | var indexedWorkflow = new IndexedWorkflowTriggers(workflow, emptyTriggerList, currentTriggers, emptyTriggerList) |
| | 3 | 127 | | await _notificationSender.SendAsync(new WorkflowTriggersIndexed(indexedWorkflow), cancellationToken); |
| | 3 | 128 | | } |
| | | 129 | | |
| | | 130 | | private async Task<IEnumerable<StoredTrigger>> GetCurrentTriggersAsync(string workflowDefinitionId, CancellationToke |
| | | 131 | | { |
| | 640 | 132 | | var filter = new TriggerFilter |
| | 640 | 133 | | { |
| | 640 | 134 | | WorkflowDefinitionId = workflowDefinitionId |
| | 640 | 135 | | }; |
| | 640 | 136 | | return await _triggerStore.FindManyAsync(filter, cancellationToken); |
| | 640 | 137 | | } |
| | | 138 | | |
| | | 139 | | private async IAsyncEnumerable<StoredTrigger> GetTriggersInternalAsync(Workflow workflow, [EnumeratorCancellation] C |
| | | 140 | | { |
| | 691 | 141 | | var context = new WorkflowIndexingContext(workflow, cancellationToken); |
| | 691 | 142 | | var nodes = await _activityVisitor.VisitAsync(workflow.Root, cancellationToken); |
| | | 143 | | |
| | | 144 | | // Get a list of trigger activities that are configured as "startable". |
| | 691 | 145 | | var triggerActivities = nodes |
| | 691 | 146 | | .Flatten() |
| | 2414 | 147 | | .Where(x => x.Activity.GetCanStartWorkflow() && x.Activity is ITrigger) |
| | 83 | 148 | | .Select(x => x.Activity) |
| | 691 | 149 | | .Cast<ITrigger>() |
| | 691 | 150 | | .ToList(); |
| | | 151 | | |
| | | 152 | | // For each trigger activity, create a trigger. |
| | 1548 | 153 | | foreach (var triggerActivity in triggerActivities) |
| | | 154 | | { |
| | 83 | 155 | | var triggers = await CreateWorkflowTriggersAsync(context, triggerActivity); |
| | | 156 | | |
| | 332 | 157 | | foreach (var trigger in triggers) |
| | 83 | 158 | | yield return trigger; |
| | | 159 | | } |
| | 691 | 160 | | } |
| | | 161 | | |
| | | 162 | | private async Task<ICollection<StoredTrigger>> CreateWorkflowTriggersAsync(WorkflowIndexingContext context, ITrigger |
| | | 163 | | { |
| | 83 | 164 | | var workflow = context.Workflow; |
| | 83 | 165 | | var cancellationToken = context.CancellationToken; |
| | 83 | 166 | | var activityTypeName = trigger.Type; |
| | 83 | 167 | | var triggerDescriptor = _activityRegistry.Find(activityTypeName, trigger.Version); |
| | | 168 | | |
| | 83 | 169 | | if (triggerDescriptor == null) |
| | | 170 | | { |
| | 0 | 171 | | _logger.LogWarning("Could not find activity descriptor for activity type {ActivityType}", activityTypeName); |
| | 0 | 172 | | return new List<StoredTrigger>(0); |
| | | 173 | | } |
| | | 174 | | |
| | 83 | 175 | | var expressionExecutionContext = await trigger.CreateExpressionExecutionContextAsync(triggerDescriptor, _service |
| | 83 | 176 | | var triggerIndexingContext = new TriggerIndexingContext(context, expressionExecutionContext, trigger, cancellati |
| | 83 | 177 | | var triggerData = await TryGetTriggerDataAsync(trigger, triggerIndexingContext); |
| | 83 | 178 | | var triggerName = triggerIndexingContext.TriggerName; |
| | | 179 | | |
| | | 180 | | // If no trigger payloads were returned, create a null payload. |
| | 83 | 181 | | if (!triggerData.Any()) triggerData.Add(null!); |
| | | 182 | | |
| | 166 | 183 | | var triggers = triggerData.Select(payload => new StoredTrigger |
| | 166 | 184 | | { |
| | 166 | 185 | | Id = _identityGenerator.GenerateId(), |
| | 166 | 186 | | WorkflowDefinitionId = workflow.Identity.DefinitionId, |
| | 166 | 187 | | WorkflowDefinitionVersionId = workflow.Identity.Id, |
| | 166 | 188 | | Name = triggerName, |
| | 166 | 189 | | ActivityId = trigger.Id, |
| | 166 | 190 | | Hash = _hasher.Hash(triggerName, payload), |
| | 166 | 191 | | Payload = payload |
| | 166 | 192 | | }); |
| | | 193 | | |
| | 83 | 194 | | return triggers.ToList(); |
| | 83 | 195 | | } |
| | | 196 | | |
| | | 197 | | private async Task<List<object>> TryGetTriggerDataAsync(ITrigger trigger, TriggerIndexingContext context) |
| | | 198 | | { |
| | | 199 | | try |
| | | 200 | | { |
| | 83 | 201 | | return (await trigger.GetTriggerPayloadsAsync(context)).ToList(); |
| | | 202 | | } |
| | 0 | 203 | | catch (Exception e) |
| | | 204 | | { |
| | 0 | 205 | | _logger.LogWarning(e, "Failed to get trigger data for activity {ActivityId}", trigger.Id); |
| | 0 | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | return new(0); |
| | 83 | 209 | | } |
| | | 210 | | } |