| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Common.Multitenancy.EventHandlers; |
| | | 3 | | using Elsa.Common.Multitenancy.HostedServices; |
| | | 4 | | using Elsa.Common.RecurringTasks; |
| | | 5 | | using Elsa.Features.Abstractions; |
| | | 6 | | using Elsa.Features.Services; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Common.Features; |
| | | 11 | | |
| | 1 | 12 | | public class MultitenancyFeature(IModule module) : FeatureBase(module) |
| | | 13 | | { |
| | 3 | 14 | | private Func<IServiceProvider, ITenantsProvider> _tenantsProviderFactory = sp => sp.GetRequiredService<DefaultTenant |
| | | 15 | | |
| | | 16 | | public MultitenancyFeature UseTenantsProvider<TTenantsProvider>() where TTenantsProvider : class, ITenantsProvider |
| | | 17 | | { |
| | 0 | 18 | | Services.TryAddScoped<TTenantsProvider>(); |
| | 0 | 19 | | return UseTenantsProvider(sp => sp.GetRequiredService<TTenantsProvider>()); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public MultitenancyFeature UseTenantsProvider(Func<IServiceProvider, ITenantsProvider> tenantsProviderFactory) |
| | | 23 | | { |
| | 0 | 24 | | _tenantsProviderFactory = tenantsProviderFactory; |
| | 0 | 25 | | return this; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public override void ConfigureHostedServices() |
| | | 29 | | { |
| | 1 | 30 | | Module |
| | 1 | 31 | | .ConfigureHostedService<ActivateTenants>(-1) |
| | 1 | 32 | | ; |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | public override void Apply() |
| | | 36 | | { |
| | 1 | 37 | | Services |
| | 1 | 38 | | .AddSingleton<ITenantScopeFactory, DefaultTenantScopeFactory>() |
| | 1 | 39 | | .AddSingleton<ITenantAccessor, DefaultTenantAccessor>() |
| | 1 | 40 | | .AddSingleton<ITenantFinder, DefaultTenantFinder>() |
| | 1 | 41 | | .AddSingleton<ITenantService, DefaultTenantService>() |
| | 1 | 42 | | |
| | 1 | 43 | | // Order is important: Startup task first, then background and recurring tasks. |
| | 1 | 44 | | .AddSingleton<ITenantActivatedEvent, RunStartupTasks>() |
| | 1 | 45 | | |
| | 1 | 46 | | .AddSingleton<RunBackgroundTasks>() |
| | 1 | 47 | | .AddSingleton<ITenantActivatedEvent>(sp => sp.GetRequiredService<RunBackgroundTasks>()) |
| | 1 | 48 | | .AddSingleton<ITenantDeactivatedEvent>(sp => sp.GetRequiredService<RunBackgroundTasks>()) |
| | 1 | 49 | | |
| | 1 | 50 | | .AddSingleton<StartRecurringTasks>() |
| | 1 | 51 | | .AddSingleton<ITenantActivatedEvent>(sp => sp.GetRequiredService<StartRecurringTasks>()) |
| | 1 | 52 | | .AddSingleton<ITenantDeactivatedEvent>(sp => sp.GetRequiredService<StartRecurringTasks>()) |
| | 1 | 53 | | |
| | 1 | 54 | | .AddSingleton<RecurringTaskScheduleManager>() |
| | 1 | 55 | | .AddSingleton<TenantEventsManager>() |
| | 1 | 56 | | .AddScoped<DefaultTenantsProvider>() |
| | 1 | 57 | | .AddScoped<DefaultTenantResolver>() |
| | 1 | 58 | | .AddScoped<ITaskExecutor, TaskExecutor>() |
| | 1 | 59 | | .AddScoped<IBackgroundTaskStarter, TaskExecutor>() |
| | 1 | 60 | | .AddScoped(_tenantsProviderFactory); |
| | 1 | 61 | | } |
| | | 62 | | } |