| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Scheduling.Activities; |
| | | 3 | | using Elsa.Workflows.Helpers; |
| | | 4 | | using Elsa.Workflows.Runtime; |
| | | 5 | | using Elsa.Workflows.Runtime.Entities; |
| | | 6 | | using Elsa.Workflows.Runtime.Filters; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Timer = Elsa.Scheduling.Activities.Timer; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Scheduling.Handlers; |
| | | 11 | | |
| | | 12 | | public class UpdateTenantSchedules : ITenantActivatedEvent, ITenantDeletedEvent |
| | | 13 | | { |
| | 1 | 14 | | private static readonly string[] ActivityTypeNames = |
| | 1 | 15 | | [ |
| | 1 | 16 | | ActivityTypeNameHelper.GenerateTypeName<Cron>(), |
| | 1 | 17 | | ActivityTypeNameHelper.GenerateTypeName<Timer>(), |
| | 1 | 18 | | ActivityTypeNameHelper.GenerateTypeName<Delay>() |
| | 1 | 19 | | ]; |
| | | 20 | | |
| | | 21 | | public async Task TenantActivatedAsync(TenantActivatedEventArgs args) |
| | | 22 | | { |
| | 1 | 23 | | var serviceProvider = args.TenantScope.ServiceProvider; |
| | 1 | 24 | | var cancellationToken = args.CancellationToken; |
| | 1 | 25 | | var triggerScheduler = args.TenantScope.ServiceProvider.GetRequiredService<ITriggerScheduler>(); |
| | 1 | 26 | | var bookmarkScheduler = args.TenantScope.ServiceProvider.GetRequiredService<IBookmarkScheduler>(); |
| | 1 | 27 | | var triggers = (await GetTriggersAsync(serviceProvider, cancellationToken)).ToList(); |
| | 1 | 28 | | var bookmarks = (await GetBookmarksAsync(serviceProvider, cancellationToken)).ToList(); |
| | | 29 | | |
| | 1 | 30 | | await triggerScheduler.ScheduleAsync(triggers, args.CancellationToken); |
| | 1 | 31 | | await bookmarkScheduler.ScheduleAsync(bookmarks, args.CancellationToken); |
| | 1 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task TenantDeletedAsync(TenantDeletedEventArgs args) |
| | | 35 | | { |
| | 0 | 36 | | var serviceProvider = args.TenantScope.ServiceProvider; |
| | 0 | 37 | | var cancellationToken = args.CancellationToken; |
| | 0 | 38 | | var triggerScheduler = args.TenantScope.ServiceProvider.GetRequiredService<ITriggerScheduler>(); |
| | 0 | 39 | | var bookmarkScheduler = args.TenantScope.ServiceProvider.GetRequiredService<IBookmarkScheduler>(); |
| | 0 | 40 | | var triggers = (await GetTriggersAsync(serviceProvider, cancellationToken)).ToList(); |
| | 0 | 41 | | var bookmarks = (await GetBookmarksAsync(serviceProvider, cancellationToken)).ToList(); |
| | | 42 | | |
| | 0 | 43 | | await triggerScheduler.UnscheduleAsync(triggers, args.CancellationToken); |
| | 0 | 44 | | await bookmarkScheduler.UnscheduleAsync(bookmarks, args.CancellationToken); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task<IEnumerable<StoredTrigger>> GetTriggersAsync(IServiceProvider serviceProvider, CancellationToken |
| | | 48 | | { |
| | 1 | 49 | | var triggerStore = serviceProvider.GetRequiredService<ITriggerStore>(); |
| | 1 | 50 | | var filter = TriggerFilter.ByNames(ActivityTypeNames); |
| | 1 | 51 | | return await triggerStore.FindManyAsync(filter, cancellationToken); |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | private async Task<IEnumerable<StoredBookmark>> GetBookmarksAsync(IServiceProvider serviceProvider, CancellationToke |
| | | 55 | | { |
| | 1 | 56 | | var bookmarkStore = serviceProvider.GetRequiredService<IBookmarkStore>(); |
| | 1 | 57 | | var filter = BookmarkFilter.ByActivityTypeNames(ActivityTypeNames); |
| | 1 | 58 | | return await bookmarkStore.FindManyAsync(filter, cancellationToken); |
| | 1 | 59 | | } |
| | | 60 | | } |