-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ken
committed
Apr 2, 2024
1 parent
7613445
commit a28befd
Showing
10 changed files
with
103 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 18 additions & 3 deletions
21
src/MinimalDomainEvents.Outbox.Worker/BackgroundDispatchWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using MinimalDomainEvents.Contract; | ||
using MinimalDomainEvents.Dispatcher.Abstractions; | ||
using MinimalDomainEvents.Outbox.Abstractions; | ||
using MinimalDomainEvents.Outbox.Worker.Abstractions; | ||
|
||
namespace MinimalDomainEvents.Outbox.Worker; | ||
internal sealed class BackgroundDispatchWorker : BackgroundService | ||
{ | ||
private const int Delay = 2 * 1000; | ||
|
||
private readonly IOutboxRecordCollectionInitializer _outboxRecordCollectionInitializer; | ||
private readonly ITransactionProvider _transactionFactory; | ||
private readonly IDomainEventRetriever _domainEventRetriever; | ||
private readonly ICleanupOutboxRecords _cleanupOutboxRecords; | ||
private readonly IEnumerable<IDispatchDomainEvents> _dispatchers; | ||
|
||
public BackgroundDispatchWorker(IOutboxRecordCollectionInitializer outboxRecordCollectionInitializer, | ||
ITransactionProvider transactionFactory, | ||
IDomainEventRetriever domainEventRetriever, | ||
ICleanupOutboxRecords cleanupOutboxRecords) | ||
ICleanupOutboxRecords cleanupOutboxRecords, | ||
IEnumerable<IDispatchDomainEvents> dispatchers) | ||
{ | ||
_domainEventRetriever = domainEventRetriever; | ||
_outboxRecordCollectionInitializer = outboxRecordCollectionInitializer; | ||
_transactionFactory = transactionFactory; | ||
_cleanupOutboxRecords = cleanupOutboxRecords; | ||
_dispatchers = dispatchers; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await _outboxRecordCollectionInitializer.Initialize(stoppingToken); | ||
|
||
while (true) | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
using var transaction = await _transactionFactory.NewTransaction(stoppingToken); | ||
var domainEvents = await _domainEventRetriever.GetAndMarkAsDispatched(stoppingToken); | ||
//TODO - Dispatch domain events | ||
|
||
if (domainEvents is not null && domainEvents.Count > 0) | ||
{ | ||
foreach (var dispatcher in _dispatchers) | ||
await dispatcher.Dispatch(domainEvents); | ||
} | ||
|
||
await _cleanupOutboxRecords.CleanupExpiredOutboxRecords(stoppingToken); | ||
await transaction.Commit(stoppingToken); | ||
|
||
await Task.Delay(Delay, stoppingToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters