< Summary

Information
Class: Elsa.Scheduling.Handlers.UpdateTenantSchedules
Assembly: Elsa.Scheduling
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Scheduling/Handlers/UpdateTenantSchedules.cs
Line coverage
71%
Covered lines: 23
Uncovered lines: 9
Coverable lines: 32
Total lines: 60
Line coverage: 71.8%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
TenantActivatedAsync()100%11100%
TenantDeletedAsync()100%210%
GetTriggersAsync()100%11100%
GetBookmarksAsync()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Scheduling/Handlers/UpdateTenantSchedules.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Scheduling.Activities;
 3using Elsa.Workflows.Helpers;
 4using Elsa.Workflows.Runtime;
 5using Elsa.Workflows.Runtime.Entities;
 6using Elsa.Workflows.Runtime.Filters;
 7using Microsoft.Extensions.DependencyInjection;
 8using Timer = Elsa.Scheduling.Activities.Timer;
 9
 10namespace Elsa.Scheduling.Handlers;
 11
 12public class UpdateTenantSchedules : ITenantActivatedEvent, ITenantDeletedEvent
 13{
 114    private static readonly string[] ActivityTypeNames =
 115    [
 116        ActivityTypeNameHelper.GenerateTypeName<Cron>(),
 117        ActivityTypeNameHelper.GenerateTypeName<Timer>(),
 118        ActivityTypeNameHelper.GenerateTypeName<Delay>()
 119    ];
 20
 21    public async Task TenantActivatedAsync(TenantActivatedEventArgs args)
 22    {
 123        var serviceProvider = args.TenantScope.ServiceProvider;
 124        var cancellationToken = args.CancellationToken;
 125        var triggerScheduler = args.TenantScope.ServiceProvider.GetRequiredService<ITriggerScheduler>();
 126        var bookmarkScheduler = args.TenantScope.ServiceProvider.GetRequiredService<IBookmarkScheduler>();
 127        var triggers = (await GetTriggersAsync(serviceProvider, cancellationToken)).ToList();
 128        var bookmarks = (await GetBookmarksAsync(serviceProvider, cancellationToken)).ToList();
 29
 130        await triggerScheduler.ScheduleAsync(triggers, args.CancellationToken);
 131        await bookmarkScheduler.ScheduleAsync(bookmarks, args.CancellationToken);
 132    }
 33
 34    public async Task TenantDeletedAsync(TenantDeletedEventArgs args)
 35    {
 036        var serviceProvider = args.TenantScope.ServiceProvider;
 037        var cancellationToken = args.CancellationToken;
 038        var triggerScheduler = args.TenantScope.ServiceProvider.GetRequiredService<ITriggerScheduler>();
 039        var bookmarkScheduler = args.TenantScope.ServiceProvider.GetRequiredService<IBookmarkScheduler>();
 040        var triggers = (await GetTriggersAsync(serviceProvider, cancellationToken)).ToList();
 041        var bookmarks = (await GetBookmarksAsync(serviceProvider, cancellationToken)).ToList();
 42
 043        await triggerScheduler.UnscheduleAsync(triggers, args.CancellationToken);
 044        await bookmarkScheduler.UnscheduleAsync(bookmarks, args.CancellationToken);
 045    }
 46
 47    private async Task<IEnumerable<StoredTrigger>> GetTriggersAsync(IServiceProvider serviceProvider, CancellationToken 
 48    {
 149        var triggerStore = serviceProvider.GetRequiredService<ITriggerStore>();
 150        var filter = TriggerFilter.ByNames(ActivityTypeNames);
 151        return await triggerStore.FindManyAsync(filter, cancellationToken);
 152    }
 53
 54    private async Task<IEnumerable<StoredBookmark>> GetBookmarksAsync(IServiceProvider serviceProvider, CancellationToke
 55    {
 156        var bookmarkStore = serviceProvider.GetRequiredService<IBookmarkStore>();
 157        var filter = BookmarkFilter.ByActivityTypeNames(ActivityTypeNames);
 158        return await bookmarkStore.FindManyAsync(filter, cancellationToken);
 159    }
 60}