< Summary

Information
Class: Elsa.Workflows.Runtime.BookmarkQueueWorker
Assembly: Elsa.Workflows.Runtime
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/BookmarkQueueWorker.cs
Line coverage
87%
Covered lines: 29
Uncovered lines: 4
Coverable lines: 33
Total lines: 74
Line coverage: 87.8%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Start()50%2283.33%
Stop()100%22100%
AwaitSignalAsync()100%2270%
ProcessAsync()100%11100%

File(s)

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

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.Logging;
 3using ThrottleDebounce;
 4
 5namespace Elsa.Workflows.Runtime;
 6
 7public class BookmarkQueueWorker : IBookmarkQueueWorker
 8{
 9    private readonly RateLimitedFunc<CancellationToken, Task> _rateLimitedProcessAsync;
 10    private CancellationTokenSource _cts = null!;
 11    private bool _running;
 12    private readonly IBookmarkQueueSignaler _signaler;
 13    private readonly IServiceScopeFactory _scopeFactory;
 14    private readonly ILogger<BookmarkQueueWorker> _logger;
 15
 516    public BookmarkQueueWorker(IBookmarkQueueSignaler signaler, IServiceScopeFactory scopeFactory, ILogger<BookmarkQueue
 17    {
 518        _signaler = signaler;
 519        _scopeFactory = scopeFactory;
 520        _logger = logger;
 521        _rateLimitedProcessAsync = Throttler.Throttle<CancellationToken, Task>(ProcessAsync, TimeSpan.FromMilliseconds(5
 522    }
 23
 24    public void Start()
 25    {
 526        if (_running)
 027            return;
 28
 529        _cts = new();
 530        _running = true;
 31
 532        _ = Task.Run(AwaitSignalAsync);
 533    }
 34
 35    public void Stop()
 36    {
 537        if (_running)
 38        {
 539            _running = false;
 540            _cts.Cancel();
 41        }
 42
 543        _cts.Dispose();
 544    }
 45
 46    private async Task AwaitSignalAsync()
 47    {
 48548        while (!_cts.IsCancellationRequested)
 49        {
 50            try
 51            {
 48552                await _signaler.AwaitAsync(_cts.Token);
 48053                await _rateLimitedProcessAsync.InvokeAsync(_cts.Token);
 48054            }
 555            catch (OperationCanceledException)
 56            {
 557                break; // Stop() was called
 58            }
 059            catch (Exception ex)
 60            {
 061                _logger.LogError(ex, "BookmarkQueueWorker error – continuing loop");
 062            }
 63        }
 564    }
 65
 66    protected virtual async Task ProcessAsync(CancellationToken cancellationToken)
 67    {
 4968        _logger.LogDebug("Processing bookmark queue...");
 4969        using var scope = _scopeFactory.CreateScope();
 4870        var processor = scope.ServiceProvider.GetRequiredService<IBookmarkQueueProcessor>();
 4871        await processor.ProcessAsync(cancellationToken);
 4872        _logger.LogDebug("Processed bookmark queue.");
 4873    }
 74}