Skip to content

Commit b270fe9

Browse files
Merge pull request #738 from TransactionProcessing/task/#725_merchant_domain)event_hander_retry
Add retry policy for domain event handling
2 parents 9cbab54 + 2e4ceb9 commit b270fe9

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
using System;
2-
using System.Threading;
3-
using System.Threading.Tasks;
4-
using MediatR;
1+
using MediatR;
52
using Newtonsoft.Json;
3+
using Polly;
64
using Shared.DomainDrivenDesign.EventSourcing;
75
using Shared.EventStore.Aggregate;
86
using Shared.EventStore.EventHandling;
97
using Shared.Results;
108
using SimpleResults;
9+
using System;
10+
using System.Threading;
11+
using System.Threading.Tasks;
1112
using TransactionProcessor.Aggregates;
13+
using TransactionProcessor.BusinessLogic.Common;
1214
using TransactionProcessor.BusinessLogic.Events;
1315
using TransactionProcessor.BusinessLogic.Requests;
1416
using TransactionProcessor.Models.Merchant;
@@ -59,34 +61,27 @@ public async Task<Result> Handle(IDomainEvent domainEvent,
5961
private async Task<Result> HandleSpecificDomainEvent(CallbackReceivedEnrichedEvent domainEvent,
6062
CancellationToken cancellationToken)
6163
{
62-
if (domainEvent.TypeString == typeof(CallbackHandler.DataTransferObjects.Deposit).ToString())
63-
{
64-
// Work out the merchant id from the reference field (second part, split on hyphen)
65-
String merchantReference = domainEvent.Reference.Split("-")[1];
66-
67-
Result<Merchant> result = await this.EstateReportingRepository.GetMerchantFromReference(domainEvent.EstateId, merchantReference, cancellationToken);
68-
if (result.IsFailed)
69-
return ResultHelpers.CreateFailure(result);
70-
71-
// We now need to deserialise the message from the callback
72-
CallbackHandler.DataTransferObjects.Deposit callbackMessage = JsonConvert.DeserializeObject<CallbackHandler.DataTransferObjects.Deposit>(domainEvent.CallbackMessage);
73-
74-
MerchantCommands.MakeMerchantDepositCommand command = new(domainEvent.EstateId,
75-
result.Data.MerchantId,
76-
DataTransferObjects.Requests.Merchant.MerchantDepositSource.Automatic,
77-
new MakeMerchantDepositRequest
78-
{
79-
DepositDateTime = callbackMessage.DateTime,
80-
Reference = callbackMessage.Reference,
81-
Amount = callbackMessage.Amount,
82-
});
83-
return await this.Mediator.Send(command, cancellationToken);
84-
}
85-
return Result.Success();
86-
}
64+
IAsyncPolicy<Result> retryPolicy = PolicyFactory.CreatePolicy(2, policyTag: "MerchantSettlementDomainEventHandler - MerchantFeeSettledEvent");
65+
66+
return await retryPolicy.ExecuteAsync(async () => {
67+
if (domainEvent.TypeString == typeof(CallbackHandler.DataTransferObjects.Deposit).ToString()) {
68+
// Work out the merchant id from the reference field (second part, split on hyphen)
69+
String merchantReference = domainEvent.Reference.Split("-")[1];
8770

71+
Result<Merchant> result = await this.EstateReportingRepository.GetMerchantFromReference(domainEvent.EstateId, merchantReference, cancellationToken);
72+
if (result.IsFailed)
73+
return ResultHelpers.CreateFailure(result);
8874

75+
// We now need to deserialise the message from the callback
76+
CallbackHandler.DataTransferObjects.Deposit callbackMessage = JsonConvert.DeserializeObject<CallbackHandler.DataTransferObjects.Deposit>(domainEvent.CallbackMessage);
8977

78+
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, });
79+
return await this.Mediator.Send(command, cancellationToken);
80+
}
81+
82+
return Result.Success();
83+
});
84+
}
9085

9186
#endregion
9287
}

0 commit comments

Comments
 (0)