< Summary

Information
Class: Elsa.Workflows.Runtime.BookmarkQueueProcessor
Assembly: Elsa.Workflows.Runtime
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/BookmarkQueueProcessor.cs
Line coverage
96%
Covered lines: 54
Uncovered lines: 2
Coverable lines: 56
Total lines: 110
Line coverage: 96.4%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ProcessAsync()75%4480%
ProcessPageAsync()100%22100%
ProcessItemAsync()100%2266.66%
HandleFailureAsync()100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/BookmarkQueueProcessor.cs

#LineLine coverage
 1using Elsa.Common.Entities;
 2using Elsa.Common.Models;
 3using Elsa.Extensions;
 4using Elsa.Common;
 5using Elsa.Workflows.Runtime.Entities;
 6using Elsa.Workflows.Runtime.Messages;
 7using Elsa.Workflows.Runtime.Options;
 8using Elsa.Workflows.Runtime.OrderDefinitions;
 9using Microsoft.Extensions.Logging;
 10using Microsoft.Extensions.Options;
 11
 12namespace Elsa.Workflows.Runtime;
 13
 35314public class BookmarkQueueProcessor(
 35315    IBookmarkQueueStore store,
 35316    IBookmarkQueueDeadLetterManager deadLetterManager,
 35317    IWorkflowResumer workflowResumer,
 35318    ISystemClock systemClock,
 35319    IOptions<BookmarkQueuePurgeOptions> options,
 35320    ILogger<BookmarkQueueProcessor> logger) : IBookmarkQueueProcessor
 21{
 22    public async Task ProcessAsync(CancellationToken cancellationToken = default)
 23    {
 35424        var batchSize = 50;
 35425        var offset = 0;
 26
 35427        while (!cancellationToken.IsCancellationRequested)
 28        {
 35229            var pageArgs = PageArgs.FromRange(offset, batchSize);
 35230            var page = await store.PageAsync(pageArgs, new BookmarkQueueItemOrder<DateTimeOffset>(x => x.CreatedAt, Orde
 31
 35232            await ProcessPageAsync(page, cancellationToken);
 33
 35134            if (page.Items.Count < batchSize)
 35                break;
 36
 037            offset += batchSize;
 038        }
 35339    }
 40
 41    private async Task ProcessPageAsync(Page<BookmarkQueueItem> page, CancellationToken cancellationToken = default)
 42    {
 1561143        foreach (var bookmarkQueueItem in page.Items)
 745444            await ProcessItemAsync(bookmarkQueueItem, cancellationToken);
 35145    }
 46
 47    private async Task ProcessItemAsync(BookmarkQueueItem item, CancellationToken cancellationToken = default)
 48    {
 745449        var filter = item.CreateBookmarkFilter();
 745450        var resumeOptions = item.Options;
 51
 745452        logger.LogDebug("Processing bookmark queue item {BookmarkQueueItemId} for workflow instance {WorkflowInstanceId}
 53
 54        List<RunWorkflowInstanceResponse> responses;
 55
 56        try
 57        {
 745458            responses = (await workflowResumer.ResumeAsync(filter, resumeOptions, cancellationToken)).ToList();
 745059        }
 160        catch (OperationCanceledException)
 61        {
 162            throw;
 63        }
 364        catch (Exception ex) when (ex is not OutOfMemoryException and not StackOverflowException)
 65        {
 366            await HandleFailureAsync(item, ex, cancellationToken);
 367            return;
 68        }
 69
 745070        if (responses.Count > 0)
 71        {
 2172            logger.LogDebug("Successfully resumed {WorkflowCount} workflow instances using stimulus {StimulusHash} for a
 2173            await store.DeleteAsync(item.Id, cancellationToken);
 74        }
 75        else
 76        {
 742977            logger.LogDebug("No matching bookmarks found for bookmark queue item {BookmarkQueueItemId} for workflow inst
 78        }
 745379    }
 80
 81    private async Task HandleFailureAsync(BookmarkQueueItem item, Exception exception, CancellationToken cancellationTok
 82    {
 383        item.DeliveryAttempts++;
 384        item.LastAttemptedAt = systemClock.UtcNow;
 385        item.LastErrorType = exception.GetType().FullName;
 386        item.LastErrorMessage = exception.Message;
 87
 388        if (item.DeliveryAttempts < options.Value.MaxDeliveryAttempts)
 89        {
 190            logger.LogWarning(
 191                exception,
 192                "Failed to process bookmark queue item {BookmarkQueueItemId}. Attempt {DeliveryAttempt} of {MaxDeliveryA
 193                item.Id,
 194                item.DeliveryAttempts,
 195                options.Value.MaxDeliveryAttempts);
 96
 197            await store.SaveAsync(item, cancellationToken);
 198            return;
 99        }
 100
 2101        logger.LogError(
 2102            exception,
 2103            "Moving bookmark queue item {BookmarkQueueItemId} to dead letter after {DeliveryAttempt} failed delivery att
 2104            item.Id,
 2105            item.DeliveryAttempts);
 106
 2107        await deadLetterManager.DeadLetterAsync(item, "Failed", exception, cancellationToken);
 2108        await store.DeleteAsync(item.Id, cancellationToken);
 3109    }
 110}