< Summary

Information
Class: Elsa.Common.Multitenancy.EventHandlers.RunBackgroundTasks
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Multitenancy/EventHandlers/RunBackgroundTasks.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 37
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
TenantActivatedAsync(...)100%44100%
TenantDeactivatedAsync(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Multitenancy/EventHandlers/RunBackgroundTasks.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2
 3namespace Elsa.Common.Multitenancy.EventHandlers;
 4
 5public class RunBackgroundTasks : ITenantActivatedEvent, ITenantDeactivatedEvent
 6{
 17    private readonly ICollection<Task> _runningTasks = new List<Task>();
 8    private CancellationTokenSource _cancellationTokenSource = default!;
 9
 10    public Task TenantActivatedAsync(TenantActivatedEventArgs args)
 11    {
 112        var cancellationToken = args.CancellationToken;
 113        _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
 14
 115        var tenantScope = args.TenantScope;
 116        var backgroundTasks = tenantScope.ServiceProvider.GetServices<IBackgroundTask>();
 117        var backgroundTaskStarter = tenantScope.ServiceProvider.GetRequiredService<IBackgroundTaskStarter>();
 118        var taskExecutor = tenantScope.ServiceProvider.GetRequiredService<ITaskExecutor>();
 19
 420        foreach (var backgroundTask in backgroundTasks)
 21        {
 122            var task = backgroundTaskStarter
 123                .StartAsync(backgroundTask, _cancellationTokenSource.Token)
 224                .ContinueWith(t => taskExecutor.ExecuteTaskAsync(backgroundTask, _cancellationTokenSource.Token), cancel
 225            if (!task.IsCompleted) _runningTasks.Add(task);
 26        }
 27
 128        return Task.CompletedTask;
 29    }
 30
 31    public Task TenantDeactivatedAsync(TenantDeactivatedEventArgs args)
 32    {
 133        _cancellationTokenSource.Cancel();
 134        _runningTasks.Clear();
 135        return Task.CompletedTask;
 36    }
 37}