| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.Mediator.Contracts; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Alterations.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Dispatches an alteration job for execution using an in-memory channel. |
| | | 10 | | /// </summary> |
| | 5 | 11 | | public class BackgroundAlterationJobDispatcher( |
| | 5 | 12 | | IJobQueue jobQueue, |
| | 5 | 13 | | ITenantAccessor tenantAccessor, |
| | 5 | 14 | | ITenantScopeFactory tenantScopeFactory) : IAlterationJobDispatcher |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public ValueTask DispatchAsync(string jobId, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 5 | 19 | | var tenant = tenantAccessor.Tenant; |
| | 10 | 20 | | jobQueue.Enqueue(ct => ExecuteJobAsync(jobId, tenant, ct)); |
| | 5 | 21 | | return default; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | private async Task ExecuteJobAsync(string alterationJobId, Tenant? tenant, CancellationToken cancellationToken) |
| | | 25 | | { |
| | 5 | 26 | | await using var tenantScope = tenantScopeFactory.CreateScope(tenant); |
| | 5 | 27 | | var alterationJobRunner = tenantScope.ServiceProvider.GetRequiredService<IAlterationJobRunner>(); |
| | 5 | 28 | | await alterationJobRunner.RunAsync(alterationJobId, cancellationToken); |
| | 4 | 29 | | } |
| | | 30 | | } |