-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Marten Outbox Pattern with custom Projection (a.k.a Subscriptio…
…ns). Plugged it into samples removing calling EventBus from repository, by that getting at-least once processing guarantee. Updated Marten to latest v5 alpha Disabled API tests parallelisation until better test setup is provided
- Loading branch information
1 parent
f69ae84
commit dbe5e3b
Showing
52 changed files
with
230 additions
and
123 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Core.Events; | ||
using Marten; | ||
using Marten.Events; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using IEvent = Core.Events.IEvent; | ||
|
||
namespace Core.Marten.Subscriptions; | ||
|
||
public class MartenEventPublisher: IMartenEventsConsumer | ||
{ | ||
private readonly IServiceProvider serviceProvider; | ||
|
||
public MartenEventPublisher( | ||
IServiceProvider serviceProvider | ||
) | ||
{ | ||
this.serviceProvider = serviceProvider; | ||
} | ||
|
||
public async Task ConsumeAsync(IDocumentOperations documentOperations, IReadOnlyList<StreamAction> streamActions, | ||
CancellationToken ct) | ||
{ | ||
foreach (var @event in streamActions.SelectMany(streamAction => streamAction.Events)) | ||
{ | ||
// TODO: align all handlers to use StreamEvent | ||
// var streamEvent = new StreamEvent( | ||
// @event.Data, | ||
// new EventMetadata( | ||
// (ulong)@event.Version, | ||
// (ulong)@event.Sequence | ||
// ) | ||
// ); | ||
|
||
using var scope = serviceProvider.CreateScope(); | ||
var eventBus = scope.ServiceProvider.GetRequiredService<IEventBus>(); | ||
|
||
if (@event.Data is not IEvent mappedEvent) continue; | ||
|
||
await eventBus.Publish(mappedEvent, ct); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Marten; | ||
using Marten.Events; | ||
using Marten.Events.Projections; | ||
|
||
namespace Core.Marten.Subscriptions; | ||
|
||
public class MartenSubscription: IProjection | ||
{ | ||
private readonly IEnumerable<IMartenEventsConsumer> consumers; | ||
|
||
public MartenSubscription(IEnumerable<IMartenEventsConsumer> consumers) | ||
{ | ||
this.consumers = consumers; | ||
} | ||
|
||
public void Apply( | ||
IDocumentOperations operations, | ||
IReadOnlyList<StreamAction> streams | ||
) => | ||
throw new NotImplementedException("Subscriptions should work only in the async scope"); | ||
|
||
public async Task ApplyAsync( | ||
IDocumentOperations operations, | ||
IReadOnlyList<StreamAction> streams, | ||
CancellationToken ct | ||
) | ||
{ | ||
foreach (var consumer in consumers) | ||
{ | ||
await consumer.ConsumeAsync(operations, streams, ct); | ||
} | ||
} | ||
} | ||
|
||
|
||
public interface IMartenEventsConsumer | ||
{ | ||
Task ConsumeAsync( | ||
IDocumentOperations documentOperations, | ||
IReadOnlyList<StreamAction> streamActions, | ||
CancellationToken ct | ||
); | ||
} |
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
Oops, something went wrong.