< Summary

Information
Class: Elsa.Common.Multitenancy.EventHandlers.StartRecurringTasks
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Multitenancy/EventHandlers/StartRecurringTasks.cs
Line coverage
90%
Covered lines: 28
Uncovered lines: 3
Coverable lines: 31
Total lines: 49
Line coverage: 90.3%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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%22100%
<TenantActivatedAsync()100%1150%
TenantDeactivatedAsync()100%44100%

File(s)

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

#LineLine coverage
 1using Elsa.Common.RecurringTasks;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Logging;
 4
 5namespace Elsa.Common.Multitenancy.EventHandlers;
 6
 17public class StartRecurringTasks(RecurringTaskScheduleManager scheduleManager, ILogger<StartRecurringTasks> logger) : IT
 8{
 19    private readonly ICollection<ScheduledTimer> _scheduledTimers = new List<ScheduledTimer>();
 10    private CancellationTokenSource _cancellationTokenSource = null!;
 11
 12    public async Task TenantActivatedAsync(TenantActivatedEventArgs args)
 13    {
 114        var cancellationToken = args.CancellationToken;
 115        _cancellationTokenSource = new CancellationTokenSource();
 116        var tenantScope = args.TenantScope;
 117        var tasks = tenantScope.ServiceProvider.GetServices<IRecurringTask>().ToList();
 118        var taskExecutor = tenantScope.ServiceProvider.GetRequiredService<ITaskExecutor>();
 19
 820        foreach (var task in tasks)
 21        {
 322            var schedule = scheduleManager.GetScheduleFor(task.GetType());
 323            var timer = schedule.CreateTimer(async () =>
 324            {
 325                try
 326                {
 227                    await taskExecutor.ExecuteTaskAsync(task, _cancellationTokenSource.Token);
 228                }
 029                catch (OperationCanceledException e)
 330                {
 031                    logger.LogInformation(e, "Task {TaskType} was cancelled", task.GetType().Name);
 032                }
 333
 534            });
 335            _scheduledTimers.Add(timer);
 336            await task.StartAsync(cancellationToken);
 37        }
 138    }
 39
 40    public async Task TenantDeactivatedAsync(TenantDeactivatedEventArgs args)
 41    {
 142        var tenantScope = args.TenantScope;
 143        _cancellationTokenSource.Cancel();
 1144        foreach (var timer in _scheduledTimers) await timer.DisposeAsync();
 145        _scheduledTimers.Clear();
 146        var tasks = tenantScope.ServiceProvider.GetServices<IRecurringTask>();
 1147        foreach (var task in tasks) await task.StopAsync(args.CancellationToken);
 148    }
 49}