From 2e4ceb993d71da948c86fc860438203087ec0cd9 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Tue, 25 Feb 2025 13:09:38 +0000 Subject: [PATCH] Add retry policy for domain event handling Introduced a retry mechanism using Polly in `MerchantDomainEventHandler.cs` to handle specific domain events. The new implementation allows for up to two retries if the event processing fails. Additionally, reorganized and cleaned up the `using` directives for improved readability. --- .../MerchantDomainEventHandler.cs | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs b/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs index 7c03f67b..6e338924 100644 --- a/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs +++ b/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs @@ -1,14 +1,16 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using MediatR; +using MediatR; using Newtonsoft.Json; +using Polly; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; using Shared.EventStore.EventHandling; using Shared.Results; using SimpleResults; +using System; +using System.Threading; +using System.Threading.Tasks; using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Common; using TransactionProcessor.BusinessLogic.Events; using TransactionProcessor.BusinessLogic.Requests; using TransactionProcessor.Models.Merchant; @@ -59,34 +61,27 @@ public async Task Handle(IDomainEvent domainEvent, private async Task HandleSpecificDomainEvent(CallbackReceivedEnrichedEvent domainEvent, CancellationToken cancellationToken) { - if (domainEvent.TypeString == typeof(CallbackHandler.DataTransferObjects.Deposit).ToString()) - { - // Work out the merchant id from the reference field (second part, split on hyphen) - String merchantReference = domainEvent.Reference.Split("-")[1]; - - Result result = await this.EstateReportingRepository.GetMerchantFromReference(domainEvent.EstateId, merchantReference, cancellationToken); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); - - // We now need to deserialise the message from the callback - CallbackHandler.DataTransferObjects.Deposit callbackMessage = JsonConvert.DeserializeObject(domainEvent.CallbackMessage); - - MerchantCommands.MakeMerchantDepositCommand command = new(domainEvent.EstateId, - result.Data.MerchantId, - DataTransferObjects.Requests.Merchant.MerchantDepositSource.Automatic, - new MakeMerchantDepositRequest - { - DepositDateTime = callbackMessage.DateTime, - Reference = callbackMessage.Reference, - Amount = callbackMessage.Amount, - }); - return await this.Mediator.Send(command, cancellationToken); - } - return Result.Success(); - } + IAsyncPolicy retryPolicy = PolicyFactory.CreatePolicy(2, policyTag: "MerchantSettlementDomainEventHandler - MerchantFeeSettledEvent"); + + return await retryPolicy.ExecuteAsync(async () => { + if (domainEvent.TypeString == typeof(CallbackHandler.DataTransferObjects.Deposit).ToString()) { + // Work out the merchant id from the reference field (second part, split on hyphen) + String merchantReference = domainEvent.Reference.Split("-")[1]; + Result result = await this.EstateReportingRepository.GetMerchantFromReference(domainEvent.EstateId, merchantReference, cancellationToken); + if (result.IsFailed) + return ResultHelpers.CreateFailure(result); + // We now need to deserialise the message from the callback + CallbackHandler.DataTransferObjects.Deposit callbackMessage = JsonConvert.DeserializeObject(domainEvent.CallbackMessage); + MerchantCommands.MakeMerchantDepositCommand command = new(domainEvent.EstateId, result.Data.MerchantId, DataTransferObjects.Requests.Merchant.MerchantDepositSource.Automatic, new MakeMerchantDepositRequest { DepositDateTime = callbackMessage.DateTime, Reference = callbackMessage.Reference, Amount = callbackMessage.Amount, }); + return await this.Mediator.Send(command, cancellationToken); + } + + return Result.Success(); + }); + } #endregion }