< Summary

Information
Class: Elsa.Http.Handlers.InvalidateHttpWorkflowsCache
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Handlers/InvalidateHttpWorkflowsCache.cs
Line coverage
42%
Covered lines: 17
Uncovered lines: 23
Coverable lines: 40
Total lines: 128
Line coverage: 42.5%
Branch coverage
28%
Covered branches: 4
Total branches: 14
Branch coverage: 28.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAsync(...)100%11100%
HandleAsync(...)100%210%
HandleAsync()0%620%
HandleAsync(...)100%11100%
HandleAsync()0%620%
HandleAsync(...)100%210%
HandleAsync()0%620%
HandleAsync()100%22100%
HandleAsync()100%22100%
InvalidateCacheAsync()100%11100%
InvalidateTriggerCacheForDefinitionVersionAsync()100%210%
InvalidateTriggerCacheAsync()0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Handlers/InvalidateHttpWorkflowsCache.cs

#LineLine coverage
 1using Elsa.Http.Bookmarks;
 2using Elsa.Mediator.Contracts;
 3using Elsa.Workflows.Management.Entities;
 4using Elsa.Workflows.Management.Notifications;
 5using Elsa.Workflows.Runtime;
 6using Elsa.Workflows.Runtime.Entities;
 7using Elsa.Workflows.Runtime.Filters;
 8using Elsa.Workflows.Runtime.Notifications;
 9using JetBrains.Annotations;
 10
 11namespace 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]
 31917public class InvalidateHttpWorkflowsCache(
 31918    IHttpWorkflowsCacheManager httpWorkflowsCacheManager,
 31919    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    {
 533        return InvalidateCacheAsync(notification.WorkflowDefinition.DefinitionId);
 34    }
 35
 36    /// <inheritdoc />
 37    public Task HandleAsync(WorkflowDefinitionRetracted notification, CancellationToken cancellationToken)
 38    {
 039        return InvalidateCacheAsync(notification.WorkflowDefinition.DefinitionId);
 40    }
 41
 42    /// <inheritdoc />
 43    public async Task HandleAsync(WorkflowDefinitionVersionsUpdated notification, CancellationToken cancellationToken)
 44    {
 045        foreach (WorkflowDefinition versionDefinition in notification.WorkflowDefinitions)
 46        {
 047            await InvalidateTriggerCacheForDefinitionVersionAsync(versionDefinition.Id, cancellationToken);
 48        }
 049    }
 50
 51    /// <inheritdoc />
 52    public Task HandleAsync(WorkflowDefinitionDeleted notification, CancellationToken cancellationToken)
 53    {
 454        return InvalidateCacheAsync(notification.DefinitionId);
 55    }
 56
 57    /// <inheritdoc />
 58    public async Task HandleAsync(WorkflowDefinitionsDeleted notification, CancellationToken cancellationToken)
 59    {
 060        foreach (string definitionId in notification.DefinitionIds)
 61        {
 062            await InvalidateCacheAsync(definitionId);
 63        }
 064    }
 65
 66    /// <inheritdoc />
 67    public Task HandleAsync(WorkflowDefinitionVersionDeleted notification, CancellationToken cancellationToken)
 68    {
 069        return InvalidateTriggerCacheForDefinitionVersionAsync(notification.WorkflowDefinition.Id, cancellationToken);
 70    }
 71
 72    /// <inheritdoc />
 73    public async Task HandleAsync(WorkflowDefinitionVersionsDeleted notification, CancellationToken cancellationToken)
 74    {
 075        foreach (string id in notification.Ids)
 76        {
 077            await InvalidateTriggerCacheForDefinitionVersionAsync(id, cancellationToken);
 78        }
 079    }
 80
 81    /// <inheritdoc />
 82    public async Task HandleAsync(WorkflowTriggersIndexed notification, CancellationToken cancellationToken)
 83    {
 48884        var hashes = new List<string>();
 49285        hashes.AddRange(notification.IndexedWorkflowTriggers.RemovedTriggers.Select(x => x.Hash)!);
 50186        hashes.AddRange(notification.IndexedWorkflowTriggers.AddedTriggers.Select(x => x.Hash)!);
 87
 101088        foreach (string hash in hashes)
 1789            await httpWorkflowsCacheManager.EvictTriggerAsync(hash, cancellationToken);
 90
 48891        await InvalidateCacheAsync(notification.IndexedWorkflowTriggers.Workflow.Identity.DefinitionId);
 48892    }
 93
 94    /// <inheritdoc />
 95    public async Task HandleAsync(WorkflowDefinitionsReloaded notification, CancellationToken cancellationToken)
 96    {
 87697        foreach (var reloadedWorkflowDefinition in notification.ReloadedWorkflowDefinitions)
 43298            await InvalidateCacheAsync(reloadedWorkflowDefinition.DefinitionId);
 699    }
 100
 101    private async Task InvalidateCacheAsync(string workflowDefinitionId)
 102    {
 929103        await httpWorkflowsCacheManager.EvictWorkflowAsync(workflowDefinitionId);
 929104    }
 105
 106    private async Task InvalidateTriggerCacheForDefinitionVersionAsync(string workflowDefinitionVersionId, CancellationT
 107    {
 0108        var filter = new TriggerFilter
 0109        {
 0110            WorkflowDefinitionVersionId = workflowDefinitionVersionId
 0111        };
 0112        var triggers = await triggerStore.FindManyAsync(filter, cancellationToken);
 113
 0114        await InvalidateTriggerCacheAsync(triggers, cancellationToken);
 0115    }
 116
 117    private async Task InvalidateTriggerCacheAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationTo
 118    {
 0119        foreach (var trigger in triggers)
 120        {
 0121            if (trigger.Payload is HttpEndpointBookmarkPayload httpPayload)
 122            {
 0123                var hash = httpWorkflowsCacheManager.ComputeBookmarkHash(httpPayload.Path, httpPayload.Method);
 0124                await httpWorkflowsCacheManager.EvictTriggerAsync(hash, cancellationToken);
 125            }
 126        }
 0127    }
 128}