-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathIntegrationEventBus.cs
113 lines (98 loc) · 4.78 KB
/
IntegrationEventBus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
namespace Masa.Contrib.Dispatcher.IntegrationEvents;
public class IntegrationEventBus : IIntegrationEventBus
{
private readonly DispatcherOptions _dispatcherOptions;
private readonly IPublisher _publisher;
private readonly ILogger<IntegrationEventBus>? _logger;
private readonly IIntegrationEventLogService? _eventLogService;
private readonly IOptionsMonitor<MasaAppConfigureOptions>? _masaAppConfigureOptions;
private readonly IEventBus? _eventBus;
private readonly IUnitOfWork? _unitOfWork;
public IntegrationEventBus(IOptions<DispatcherOptions> options,
IPublisher publisher,
IIntegrationEventLogService? eventLogService = null,
IOptionsMonitor<MasaAppConfigureOptions>? masaAppConfigureOptions = null,
ILogger<IntegrationEventBus>? logger = null,
IEventBus? eventBus = null,
IUnitOfWork? unitOfWork = null)
{
_dispatcherOptions = options.Value;
_publisher = publisher;
_eventLogService = eventLogService;
_masaAppConfigureOptions = masaAppConfigureOptions;
_logger = logger;
_eventBus = eventBus;
_unitOfWork = unitOfWork;
}
public IEnumerable<Type> GetAllEventTypes() =>
_eventBus == null
? _dispatcherOptions.AllEventTypes
: _dispatcherOptions.AllEventTypes.Concat(_eventBus.GetAllEventTypes()).Distinct();
public async Task PublishAsync<TEvent>(TEvent @event, CancellationToken cancellationToken = default)
where TEvent : IEvent
{
if (@event is IIntegrationEvent integrationEvent)
{
await PublishIntegrationAsync(integrationEvent, cancellationToken);
}
else if (_eventBus != null)
{
await _eventBus.PublishAsync(@event, cancellationToken);
}
else
{
throw new NotSupportedException(nameof(@event));
}
}
private async Task PublishIntegrationAsync<TEvent>(TEvent @event, CancellationToken cancellationToken)
where TEvent : IIntegrationEvent
{
if (@event.UnitOfWork == null && _unitOfWork != null)
@event.UnitOfWork = _unitOfWork;
var topicName = @event.Topic;
if (@event.UnitOfWork is { UseTransaction: true } && _eventLogService != null)
{
bool isAdd = false;
try
{
_logger?.LogDebug("----- Saving changes and integrationEvent: {IntegrationEventId}", @event.GetEventId());
await _eventLogService.SaveEventAsync(@event, @event.UnitOfWork!.Transaction, cancellationToken);
isAdd = true;
_logger?.LogDebug(
"----- Publishing integration event: {IntegrationEventIdPublished} from {AppId} - ({IntegrationEvent})",
@event.GetEventId(),
_masaAppConfigureOptions?.CurrentValue.AppId ?? string.Empty, @event);
await _eventLogService.MarkEventAsInProgressAsync(@event.GetEventId(),
_dispatcherOptions.MinimumRetryInterval,
cancellationToken);
_logger?.LogDebug("Publishing event {Event} to {TopicName}", @event, topicName);
await _publisher.PublishAsync(topicName, (dynamic)@event, cancellationToken);
await _eventLogService.MarkEventAsPublishedAsync(@event.GetEventId(), cancellationToken);
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error Publishing integration event: {IntegrationEventId} from {AppId} - ({IntegrationEvent})",
@event.GetEventId(), _masaAppConfigureOptions?.CurrentValue.AppId ?? string.Empty, @event);
if (!isAdd) throw;
LocalQueueProcessor.Default.AddJobs(new IntegrationEventLogItem(@event.GetEventId(), @event.Topic, @event));
await _eventLogService.MarkEventAsFailedAsync(@event.GetEventId(), cancellationToken);
}
}
else
{
_logger?.LogDebug(
"----- Publishing integration event (don't use local message): {IntegrationEventIdPublished} from {AppId} - ({IntegrationEvent})",
@event.GetEventId(),
_masaAppConfigureOptions?.CurrentValue.AppId ?? string.Empty, @event);
await _publisher.PublishAsync(topicName, (dynamic)@event, cancellationToken);
}
}
public async Task CommitAsync(CancellationToken cancellationToken = default)
{
if (_unitOfWork is null)
throw new ArgumentNullException(nameof(IUnitOfWork), "You need to UseUoW when adding services");
await _unitOfWork.CommitAsync(cancellationToken);
}
}