| | | 1 | | using ArturRios.Heimdall.Command.Services; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Heimdall.WebApi.Email; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Works through <see cref="RegistrationQueue" />: one fresh DI scope and one |
| | | 7 | | /// <see cref="RegistrationProcessor" /> run per request, after the caller has been answered. One |
| | | 8 | | /// request at a time; a failure is logged and the next request taken. See |
| | | 9 | | /// <see cref="PasswordRecoveryDispatcher" />, which this mirrors. |
| | | 10 | | /// </summary> |
| | 899 | 11 | | public sealed class RegistrationDispatcher( |
| | 899 | 12 | | RegistrationQueue queue, |
| | 899 | 13 | | IServiceScopeFactory scopeFactory, |
| | 899 | 14 | | ILogger<RegistrationDispatcher> logger) : BackgroundService |
| | | 15 | | { |
| | | 16 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 17 | | { |
| | | 18 | | try |
| | | 19 | | { |
| | 1838 | 20 | | await foreach (var request in queue.Reader.ReadAllAsync(stoppingToken)) |
| | | 21 | | { |
| | 20 | 22 | | await ProcessAsync(request); |
| | | 23 | | } |
| | 0 | 24 | | } |
| | 899 | 25 | | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) |
| | | 26 | | { |
| | | 27 | | // Shutdown. Whatever is still queued is dropped, as in the recovery queue. |
| | 899 | 28 | | } |
| | 899 | 29 | | } |
| | | 30 | | |
| | | 31 | | private async Task ProcessAsync(RegistrationRequest request) |
| | | 32 | | { |
| | | 33 | | try |
| | | 34 | | { |
| | 20 | 35 | | await using var scope = scopeFactory.CreateAsyncScope(); |
| | | 36 | | |
| | 20 | 37 | | await scope.ServiceProvider.GetRequiredService<RegistrationProcessor>().ProcessAsync(request); |
| | 20 | 38 | | } |
| | 0 | 39 | | catch (Exception exception) |
| | | 40 | | { |
| | 0 | 41 | | logger.LogError(exception, "A registration request could not be processed"); |
| | 0 | 42 | | } |
| | 20 | 43 | | } |
| | | 44 | | } |