-
Notifications
You must be signed in to change notification settings - Fork 293
Add support MongoDB #3524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iancooper
merged 18 commits into
BrighterCommand:master
from
lillo42:add-support-mongodb
Feb 28, 2025
Merged
Add support MongoDB #3524
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
34754e6
Initial support to MongoDB
lillo42 48525ed
Merge with master
lillo42 a9b9f8c
Finish MongoDB outbox support
lillo42 5cee447
Add support to .NET Framework
lillo42 41274c9
Add support to inbox message
lillo42 7c48dcc
Add Unit of work
lillo42 fa1da47
Add MongoDb distributed lock
lillo42 abceffe
Add MongoDb test
lillo42 ea54ef1
Add CI test and rename the project
lillo42 e76b72d
Fixes TTL index
lillo42 fc3c0f8
Remove unnecessary config
lillo42 30e7a0b
Merge branch 'master' into add-support-mongodb
lillo42 23752b2
Increase mongodb retries
lillo42 8d9fdaa
try to fix mongo startup
lillo42 b3b0027
try to fix build
lillo42 037df9a
try fix build
lillo42 fb52959
try fix build
lillo42 57dd9fd
Merge with master
lillo42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
| services: | ||
| mongo: | ||
| image: mongo | ||
| environment: | ||
| MONGO_INITDB_ROOT_USERNAME: root | ||
| MONGO_INITDB_ROOT_PASSWORD: example | ||
| MONGO_INITDB_DATABASE: brighter | ||
| ports: | ||
| - "27017:27017" |
This file contains hidden or 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,85 @@ | ||
| using System.Text.Json; | ||
| using MongoDB.Bson.Serialization.Attributes; | ||
| using Paramore.Brighter.MongoDb; | ||
|
|
||
| namespace Paramore.Brighter.Inbox.MongoDb; | ||
|
|
||
| /// <summary> | ||
| /// The MongoDb inbox message | ||
| /// </summary> | ||
| public class InboxMessage : IMongoDbCollectionTTL | ||
| { | ||
| /// <summary> | ||
| /// Initialize new instance of <see cref="InboxMessage"/> | ||
| /// </summary> | ||
| public InboxMessage() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initialize new instance of <see cref="InboxMessage"/> | ||
| /// </summary> | ||
| /// <param name="command">The command.</param> | ||
| /// <param name="id">The command id.</param> | ||
| /// <param name="contextKey">The context key.</param> | ||
| /// <param name="timeStamp">The time stamp of when the message was created.</param> | ||
| /// <param name="expireAfterSeconds">The expires after X seconds.</param> | ||
| public InboxMessage(object command, string id, string contextKey, DateTimeOffset timeStamp, long? expireAfterSeconds) | ||
| { | ||
| Id = new InboxMessageId { Id = id, ContextKey = contextKey }; | ||
| TimeStamp = timeStamp; | ||
| CommandType = command.GetType().FullName!; | ||
| CommandBody = JsonSerializer.Serialize(command, JsonSerialisationOptions.Options); | ||
| ExpireAfterSeconds = expireAfterSeconds; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The Message ID | ||
| /// </summary> | ||
| [BsonId] | ||
| public InboxMessageId Id { get; set; } = new(); | ||
|
|
||
| /// <summary> | ||
| /// The <see cref="DateTimeOffset"/> when the message was crated | ||
| /// </summary> | ||
| public DateTimeOffset TimeStamp { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The command type(the full name) | ||
| /// </summary> | ||
| public string CommandType { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The command body | ||
| /// </summary> | ||
| public string CommandBody { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The TTL for this message | ||
| /// </summary> | ||
| public long? ExpireAfterSeconds { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The inbox message id | ||
| /// </summary> | ||
| public class InboxMessageId | ||
| { | ||
| /// <summary> | ||
| /// The id. | ||
| /// </summary> | ||
| public string Id { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The context key. | ||
| /// </summary> | ||
| public string? ContextKey { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Convert the <see cref="CommandBody"/> to <typeparamref name="T"/> | ||
| /// </summary> | ||
| /// <typeparam name="T">The <see cref="IRequest"/>.</typeparam> | ||
| /// <returns>New instance of <typeparamref cref="T"/>.</returns> | ||
| public T ToCommand<T>() | ||
| => JsonSerializer.Deserialize<T>(CommandBody, JsonSerialisationOptions.Options)!; | ||
| } |
This file contains hidden or 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,122 @@ | ||
| using MongoDB.Driver; | ||
|
lillo42 marked this conversation as resolved.
|
||
| using Paramore.Brighter.Inbox.Exceptions; | ||
| using Paramore.Brighter.MongoDb; | ||
|
|
||
| namespace Paramore.Brighter.Inbox.MongoDb; | ||
|
|
||
| /// <summary> | ||
| /// The inbox implementation to MongoDB | ||
| /// </summary> | ||
| public class MongoDbInbox : BaseMongoDb<InboxMessage>, IAmAnInboxAsync, IAmAnInboxSync | ||
| { | ||
| /// <summary> | ||
| /// Initialize a new instance of <see cref="MongoDbInbox"/>. | ||
| /// </summary> | ||
| /// <param name="configuration">The configuration.</param> | ||
| public MongoDbInbox(MongoDbConfiguration configuration) | ||
| : base(configuration) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool ContinueOnCapturedContext { get; set; } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task AddAsync<T>(T command, string contextKey, int timeoutInMilliseconds = -1, | ||
| CancellationToken cancellationToken = default) where T : class, IRequest | ||
| { | ||
| var message = new InboxMessage(command, command.Id, contextKey, Configuration.TimeProvider.GetUtcNow(), | ||
| ExpireAfterSeconds); | ||
|
|
||
| try | ||
| { | ||
| await Collection.InsertOneAsync(message, cancellationToken: cancellationToken) | ||
| .ConfigureAwait(ContinueOnCapturedContext); | ||
| } | ||
| catch (MongoWriteException e) | ||
| { | ||
| if (e.WriteError.Category == ServerErrorCategory.DuplicateKey) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| throw; | ||
| } | ||
| } | ||
|
lillo42 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| /// <inheritdoc /> | ||
| public async Task<T> GetAsync<T>(string id, string contextKey, int timeoutInMilliseconds = -1, | ||
| CancellationToken cancellationToken = default) where T : class, IRequest | ||
| { | ||
| var commandId = new InboxMessage.InboxMessageId { Id = id, ContextKey = contextKey }; | ||
| var filter = Builders<InboxMessage>.Filter.Eq(x => x.Id, commandId); | ||
|
|
||
| var command = await Collection.Find(filter) | ||
| .FirstOrDefaultAsync(cancellationToken) | ||
| .ConfigureAwait(ContinueOnCapturedContext); | ||
|
|
||
| if (command == null) | ||
| { | ||
| throw new RequestNotFoundException<T>(id); | ||
| } | ||
|
|
||
| return command.ToCommand<T>(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task<bool> ExistsAsync<T>(string id, string contextKey, int timeoutInMilliseconds = -1, | ||
| CancellationToken cancellationToken = default) where T : class, IRequest | ||
| { | ||
| var commandId = new InboxMessage.InboxMessageId { Id = id, ContextKey = contextKey }; | ||
| var filter = Builders<InboxMessage>.Filter.Eq("Id", commandId); | ||
| return await Collection.Find(filter) | ||
| .AnyAsync(cancellationToken: cancellationToken) | ||
| .ConfigureAwait(ContinueOnCapturedContext); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Add<T>(T command, string contextKey, int timeoutInMilliseconds = -1) where T : class, IRequest | ||
| { | ||
| var message = new InboxMessage(command, command.Id, contextKey, Configuration.TimeProvider.GetUtcNow(), | ||
| ExpireAfterSeconds); | ||
|
|
||
| try | ||
| { | ||
| Collection.InsertOne(message); | ||
| } | ||
| catch (MongoWriteException e) | ||
| { | ||
| if (e.WriteError.Category == ServerErrorCategory.DuplicateKey) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public T Get<T>(string id, string contextKey, int timeoutInMilliseconds = -1) where T : class, IRequest | ||
| { | ||
| var commandId = new InboxMessage.InboxMessageId { Id = id, ContextKey = contextKey }; | ||
| var filter = Builders<InboxMessage>.Filter.Eq(x => x.Id, commandId); | ||
|
|
||
| var command = Collection.Find(filter).FirstOrDefault(); | ||
| if (command == null) | ||
| { | ||
| throw new RequestNotFoundException<T>(id); | ||
| } | ||
|
|
||
| return command.ToCommand<T>(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool Exists<T>(string id, string contextKey, int timeoutInMilliseconds = -1) where T : class, IRequest | ||
| { | ||
| var commandId = new InboxMessage.InboxMessageId { Id = id, ContextKey = contextKey }; | ||
| var filter = Builders<InboxMessage>.Filter.Eq("Id", commandId); | ||
| return Collection.Find(filter) | ||
| .Any(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.