|
| 1 | +using EFCore.PostgresExtensions.Enums; |
| 2 | +using EFCore.PostgresExtensions.Extensions; |
| 3 | +using FinHub.Mock1.Box.Abstractions; |
| 4 | +using MassTransit; |
| 5 | +using MassTransit.PostgresOutbox.Enums; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Hosting; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Newtonsoft.Json; |
| 11 | + |
| 12 | +namespace MassTransit.PostgresOutbox.Jobs |
| 13 | +{ |
| 14 | + internal class OutboxMessagePublisherService<TDbContext> : BackgroundService |
| 15 | + where TDbContext : DbContext, IOutboxDbContext |
| 16 | + { |
| 17 | + private readonly int _batchCount; |
| 18 | + private readonly PeriodicTimer _timer; |
| 19 | + private readonly ILogger<OutboxMessagePublisherService<TDbContext>> _logger; |
| 20 | + private readonly IServiceScopeFactory _serviceScopeFactory; |
| 21 | + private readonly IPublishEndpoint _publishEndpoint; |
| 22 | + |
| 23 | + public OutboxMessagePublisherService(IPublishEndpoint publishEndpoint, |
| 24 | + IServiceScopeFactory serviceScopeFactory, |
| 25 | + ILogger<OutboxMessagePublisherService<TDbContext>> logger, Settings settings) |
| 26 | + { |
| 27 | + _publishEndpoint = publishEndpoint; |
| 28 | + _serviceScopeFactory = serviceScopeFactory; |
| 29 | + _logger = logger; |
| 30 | + _timer = new(settings.PublisherTimerPeriod); |
| 31 | + _batchCount = settings.PublisherBatchCount; |
| 32 | + } |
| 33 | + |
| 34 | + protected override async Task ExecuteAsync(CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + while (await _timer.WaitForNextTickAsync(cancellationToken)) |
| 37 | + { |
| 38 | + _logger.LogInformation($"{nameof(OutboxMessagePublisherService<TDbContext>)} started iteration"); |
| 39 | + |
| 40 | + using var scope = _serviceScopeFactory.CreateScope(); |
| 41 | + using var dbContext = scope.ServiceProvider.GetRequiredService<TDbContext>(); |
| 42 | + using var transactionScope = await dbContext.Database.BeginTransactionAsync(System.Data.IsolationLevel.ReadCommitted, cancellationToken); |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + var messages = await dbContext.OutboxMessages |
| 47 | + .Where(x => x.State == MessageState.New) |
| 48 | + .OrderBy(x => x.CreatedAt) |
| 49 | + .ForUpdate(LockBehavior.SkipLocked) |
| 50 | + .Take(_batchCount) |
| 51 | + .ToListAsync(cancellationToken); |
| 52 | + |
| 53 | + if (messages.Count == 0) |
| 54 | + { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + var publishedMessageIds = new List<Guid>(messages.Count); |
| 59 | + |
| 60 | + foreach (var message in messages) |
| 61 | + { |
| 62 | + try |
| 63 | + { |
| 64 | + var type = Type.GetType(message.Type); |
| 65 | + |
| 66 | + var messageObject = JsonConvert.DeserializeObject(message.Payload, type!); |
| 67 | + |
| 68 | + await _publishEndpoint.Publish(messageObject!, type!, x => x.Headers.Set(Constants.OutboxMessageId, message.Id), cancellationToken); |
| 69 | + |
| 70 | + publishedMessageIds.Add(message.Id); |
| 71 | + } |
| 72 | + catch (Exception ex) |
| 73 | + { |
| 74 | + _logger.LogError(ex, ex.Message); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (publishedMessageIds.Count == 0) |
| 79 | + { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + var utcNow = DateTime.UtcNow; |
| 84 | + |
| 85 | + await dbContext.OutboxMessages |
| 86 | + .Where(b => publishedMessageIds.Contains(b.Id)) |
| 87 | + .ExecuteUpdateAsync(x => x.SetProperty(m => m.State, MessageState.Done) |
| 88 | + .SetProperty(m => m.UpdatedAt, utcNow), cancellationToken: cancellationToken); |
| 89 | + |
| 90 | + await dbContext.SaveChangesAsync(); |
| 91 | + await transactionScope.CommitAsync(); |
| 92 | + |
| 93 | + } |
| 94 | + catch (Exception ex) |
| 95 | + { |
| 96 | + _logger.LogError(ex, ex.Message); |
| 97 | + await transactionScope.RollbackAsync(cancellationToken); |
| 98 | + } |
| 99 | + |
| 100 | + _logger.LogInformation($"{nameof(OutboxMessagePublisherService<TDbContext>)} finished iteration"); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments