Skip to content

Commit

Permalink
Being more cautious about applying revision logic if the saga does no…
Browse files Browse the repository at this point in the history
…t exist w/ Marten. Closes GH-1095
  • Loading branch information
jeremydmiller committed Oct 23, 2024
1 parent 61d5701 commit 540cd9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
if (Saga.VariableType.CanBeCastTo<IRevisioned>())
{
writer.WriteComment($"{Saga.VariableType.FullNameInCode()} implements {typeof(IRevisioned).FullNameInCode()}, so Wolverine will try to update based on the revision as a concurrency protection");
writer.WriteLine($"BLOCK:if ({Saga.Usage} != null)");
writer.WriteLine($"var {ExpectedSagaRevision} = {Saga.Usage}.{nameof(IRevisioned.Version)} + 1;");
writer.FinishBlock();
}

Next?.GenerateCode(method, writer);
Expand Down
11 changes: 9 additions & 2 deletions src/Samples/Middleware/AppWithMiddleware/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public static void Handle(
#endregion


public record InvalidAccount(Guid AccountId);

#region sample_AccountLookupMiddleware

// This is *a* way to build middleware in Wolverine by basically just
Expand All @@ -82,7 +84,7 @@ public static class AccountLookupMiddleware
{
// The message *has* to be first in the parameter list
// Before or BeforeAsync tells Wolverine this method should be called before the actual action
public static async Task<(HandlerContinuation, Account?)> LoadAsync(
public static async Task<(HandlerContinuation, Account?, OutgoingMessages)> LoadAsync(
IAccountCommand command,
ILogger logger,

Expand All @@ -91,13 +93,18 @@ public static class AccountLookupMiddleware

CancellationToken cancellation)
{
var messages = new OutgoingMessages();
var account = await session.LoadAsync<Account>(command.AccountId, cancellation);
if (account == null)
{
logger.LogInformation("Unable to find an account for {AccountId}, aborting the requested operation", command.AccountId);

messages.RespondToSender(new InvalidAccount(command.AccountId));
return (HandlerContinuation.Stop, null, messages);
}

return (account == null ? HandlerContinuation.Stop : HandlerContinuation.Continue, account);
// messages would be empty here
return (HandlerContinuation.Continue, account, messages);
}
}

Expand Down

0 comments on commit 540cd9c

Please sign in to comment.