-
Notifications
You must be signed in to change notification settings - Fork 48
Store message body attachments on FailedMessage documents #3719
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
Changes from all commits
fdfa82d
e7d0912
e69dcdf
03f500a
0765da5
99a8e8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,61 @@ | ||
| namespace ServiceControl.Operations.BodyStorage.RavenAttachments | ||
| { | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Raven.Client.Documents; | ||
| using Raven.Client.Documents.Commands.Batches; | ||
| using Sparrow.Json.Parsing; | ||
| using Raven.Client.Documents.Session; | ||
| using ServiceControl.MessageFailures; | ||
| using ServiceControl.MessageFailures.Api; | ||
|
|
||
| class RavenAttachmentsBodyStorage : IBodyStorage | ||
| { | ||
| const string AttachmentName = "body"; | ||
| public const string AttachmentName = "body"; | ||
| readonly IDocumentStore documentStore; | ||
|
|
||
| public RavenAttachmentsBodyStorage(IDocumentStore documentStore) | ||
| { | ||
| this.documentStore = documentStore; | ||
| } | ||
|
|
||
| // TODO: This method is only used in tests and not by ServiceControl itself! But in the Raven3.5 persister, it IS used! | ||
| // It should probably be removed and tests should use the RavenDbRecoverabilityIngestionUnitOfWork | ||
| public async Task Store(string messageId, string contentType, int bodySize, Stream bodyStream) | ||
| public Task Store(string uniqueId, string contentType, int bodySize, Stream bodyStream) | ||
| => throw new NotImplementedException("Only included for interface compatibility with Raven3.5 persister implementation. Raven5 tests should use IIngestionUnitOfWorkFactory to store failed messages/bodies."); | ||
|
|
||
| public async Task<MessageBodyStreamResult> TryFetch(string bodyId) | ||
| { | ||
| var documentId = MessageBodyIdGenerator.MakeDocumentId(messageId); | ||
| using var session = documentStore.OpenAsyncSession(); | ||
|
|
||
| // BodyId could be a MessageID or a UniqueID, but if a UniqueID then it will be a DeterministicGuid of MessageID and endpoint name and be Guid-parseable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can it be a message ID or unique ID?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because in my redesign, we're always storing it using the unique ID, and the message metadata will say "look at
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we test SI + SP and they use the BodyUrl we could simplify this and only use Unique ID? |
||
| // This is preferred, then we know we're getting the correct message body that is attached to the FailedMessage document | ||
| if (Guid.TryParse(bodyId, out _)) | ||
tmasternak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var result = await ResultForUniqueId(session, bodyId); | ||
| if (result != null) | ||
| { | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| var emptyDoc = new DynamicJsonValue(); | ||
| var putOwnerDocumentCmd = new PutCommandData(documentId, null, emptyDoc); | ||
| // See if we can look up a FailedMessage by MessageID | ||
| var query = session.Query<FailedMessageViewIndex.SortAndFilterOptions, FailedMessageViewIndex>() | ||
| .Where(msg => msg.MessageId == bodyId, true) | ||
| .OfType<FailedMessage>() | ||
| .Select(msg => msg.UniqueMessageId); | ||
|
|
||
| var stream = bodyStream; | ||
| var putAttachmentCmd = new PutAttachmentCommandData(documentId, "body", stream, contentType, changeVector: null); | ||
| var uniqueId = await query.FirstOrDefaultAsync(); | ||
|
|
||
| using var session = documentStore.OpenAsyncSession(); | ||
| session.Advanced.Defer(new ICommandData[] { putOwnerDocumentCmd, putAttachmentCmd }); | ||
| await session.SaveChangesAsync(); | ||
| if (uniqueId != null) | ||
| { | ||
| return await ResultForUniqueId(session, uniqueId); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public async Task<MessageBodyStreamResult> TryFetch(string messageId) | ||
| async Task<MessageBodyStreamResult> ResultForUniqueId(IAsyncDocumentSession session, string uniqueId) | ||
| { | ||
| var documentId = MessageBodyIdGenerator.MakeDocumentId(messageId); | ||
|
|
||
| using var session = documentStore.OpenAsyncSession(); | ||
| var documentId = FailedMessageIdGenerator.MakeDocumentId(uniqueId); | ||
|
|
||
| var result = await session.Advanced.Attachments.GetAsync(documentId, AttachmentName); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.