| | | 1 | | using Elsa.Http.Bookmarks; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.Management.Entities; |
| | | 4 | | using Elsa.Workflows.Management.Notifications; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using Elsa.Workflows.Runtime.Entities; |
| | | 7 | | using Elsa.Workflows.Runtime.Filters; |
| | | 8 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Http.Handlers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// A handler that invalidates the HTTP workflows cache when a workflow definition is published, retracted, or deleted o |
| | | 15 | | /// </summary> |
| | | 16 | | [UsedImplicitly] |
| | 319 | 17 | | public class InvalidateHttpWorkflowsCache( |
| | 319 | 18 | | IHttpWorkflowsCacheManager httpWorkflowsCacheManager, |
| | 319 | 19 | | ITriggerStore triggerStore) : |
| | | 20 | | INotificationHandler<WorkflowDefinitionPublished>, |
| | | 21 | | INotificationHandler<WorkflowDefinitionRetracted>, |
| | | 22 | | INotificationHandler<WorkflowDefinitionVersionsUpdated>, |
| | | 23 | | INotificationHandler<WorkflowDefinitionDeleted>, |
| | | 24 | | INotificationHandler<WorkflowDefinitionsDeleted>, |
| | | 25 | | INotificationHandler<WorkflowDefinitionVersionDeleted>, |
| | | 26 | | INotificationHandler<WorkflowDefinitionVersionsDeleted>, |
| | | 27 | | INotificationHandler<WorkflowTriggersIndexed>, |
| | | 28 | | INotificationHandler<WorkflowDefinitionsReloaded> |
| | | 29 | | { |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Task HandleAsync(WorkflowDefinitionPublished notification, CancellationToken cancellationToken) |
| | | 32 | | { |
| | 5 | 33 | | return InvalidateCacheAsync(notification.WorkflowDefinition.DefinitionId); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public Task HandleAsync(WorkflowDefinitionRetracted notification, CancellationToken cancellationToken) |
| | | 38 | | { |
| | 0 | 39 | | return InvalidateCacheAsync(notification.WorkflowDefinition.DefinitionId); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public async Task HandleAsync(WorkflowDefinitionVersionsUpdated notification, CancellationToken cancellationToken) |
| | | 44 | | { |
| | 0 | 45 | | foreach (WorkflowDefinition versionDefinition in notification.WorkflowDefinitions) |
| | | 46 | | { |
| | 0 | 47 | | await InvalidateTriggerCacheForDefinitionVersionAsync(versionDefinition.Id, cancellationToken); |
| | | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public Task HandleAsync(WorkflowDefinitionDeleted notification, CancellationToken cancellationToken) |
| | | 53 | | { |
| | 4 | 54 | | return InvalidateCacheAsync(notification.DefinitionId); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public async Task HandleAsync(WorkflowDefinitionsDeleted notification, CancellationToken cancellationToken) |
| | | 59 | | { |
| | 0 | 60 | | foreach (string definitionId in notification.DefinitionIds) |
| | | 61 | | { |
| | 0 | 62 | | await InvalidateCacheAsync(definitionId); |
| | | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public Task HandleAsync(WorkflowDefinitionVersionDeleted notification, CancellationToken cancellationToken) |
| | | 68 | | { |
| | 0 | 69 | | return InvalidateTriggerCacheForDefinitionVersionAsync(notification.WorkflowDefinition.Id, cancellationToken); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public async Task HandleAsync(WorkflowDefinitionVersionsDeleted notification, CancellationToken cancellationToken) |
| | | 74 | | { |
| | 0 | 75 | | foreach (string id in notification.Ids) |
| | | 76 | | { |
| | 0 | 77 | | await InvalidateTriggerCacheForDefinitionVersionAsync(id, cancellationToken); |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public async Task HandleAsync(WorkflowTriggersIndexed notification, CancellationToken cancellationToken) |
| | | 83 | | { |
| | 488 | 84 | | var hashes = new List<string>(); |
| | 492 | 85 | | hashes.AddRange(notification.IndexedWorkflowTriggers.RemovedTriggers.Select(x => x.Hash)!); |
| | 501 | 86 | | hashes.AddRange(notification.IndexedWorkflowTriggers.AddedTriggers.Select(x => x.Hash)!); |
| | | 87 | | |
| | 1010 | 88 | | foreach (string hash in hashes) |
| | 17 | 89 | | await httpWorkflowsCacheManager.EvictTriggerAsync(hash, cancellationToken); |
| | | 90 | | |
| | 488 | 91 | | await InvalidateCacheAsync(notification.IndexedWorkflowTriggers.Workflow.Identity.DefinitionId); |
| | 488 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <inheritdoc /> |
| | | 95 | | public async Task HandleAsync(WorkflowDefinitionsReloaded notification, CancellationToken cancellationToken) |
| | | 96 | | { |
| | 876 | 97 | | foreach (var reloadedWorkflowDefinition in notification.ReloadedWorkflowDefinitions) |
| | 432 | 98 | | await InvalidateCacheAsync(reloadedWorkflowDefinition.DefinitionId); |
| | 6 | 99 | | } |
| | | 100 | | |
| | | 101 | | private async Task InvalidateCacheAsync(string workflowDefinitionId) |
| | | 102 | | { |
| | 929 | 103 | | await httpWorkflowsCacheManager.EvictWorkflowAsync(workflowDefinitionId); |
| | 929 | 104 | | } |
| | | 105 | | |
| | | 106 | | private async Task InvalidateTriggerCacheForDefinitionVersionAsync(string workflowDefinitionVersionId, CancellationT |
| | | 107 | | { |
| | 0 | 108 | | var filter = new TriggerFilter |
| | 0 | 109 | | { |
| | 0 | 110 | | WorkflowDefinitionVersionId = workflowDefinitionVersionId |
| | 0 | 111 | | }; |
| | 0 | 112 | | var triggers = await triggerStore.FindManyAsync(filter, cancellationToken); |
| | | 113 | | |
| | 0 | 114 | | await InvalidateTriggerCacheAsync(triggers, cancellationToken); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | private async Task InvalidateTriggerCacheAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationTo |
| | | 118 | | { |
| | 0 | 119 | | foreach (var trigger in triggers) |
| | | 120 | | { |
| | 0 | 121 | | if (trigger.Payload is HttpEndpointBookmarkPayload httpPayload) |
| | | 122 | | { |
| | 0 | 123 | | var hash = httpWorkflowsCacheManager.ComputeBookmarkHash(httpPayload.Path, httpPayload.Method); |
| | 0 | 124 | | await httpWorkflowsCacheManager.EvictTriggerAsync(hash, cancellationToken); |
| | | 125 | | } |
| | | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | } |