-
-
Save dcomartin/44bc3d5208066e7340493a24f2d2de4e to your computer and use it in GitHub Desktop.
This file contains 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
public class OrderSaga : | |
Saga<OrderSagaData>, | |
IAmStartedByMessages<PlaceOrder>, | |
IHandleMessages<ProcessPayment>, | |
IHandleMessages<EmailConfirmation>, | |
IHandleTimeouts<CancelOrder> | |
{ | |
static readonly ILog Log = LogManager.GetLogger<OrderSaga>(); | |
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<OrderSagaData> mapper) | |
{ | |
mapper.MapSaga(sagaData => sagaData.OrderId) | |
.ToMessage<PlaceOrder>(message => message.OrderId) | |
.ToMessage<ProcessPayment>(message => message.OrderId) | |
.ToMessage<EmailConfirmation>(message => message.OrderId); | |
} | |
public async Task Handle(PlaceOrder message, IMessageHandlerContext context) | |
{ | |
Log.Info($"PlaceOrder received with OrderId {message.OrderId}"); | |
var timeout = DateTimeOffset.UtcNow.AddSeconds(5); | |
Log.Info("Requesting a CancelOrder that will be executed in 5 seconds."); | |
await RequestTimeout<CancelOrder>(context, timeout); | |
var processPayment = new ProcessPayment | |
{ | |
OrderId = Data.OrderId | |
}; | |
await context.SendLocal(processPayment); | |
} | |
public async Task Handle(ProcessPayment message, IMessageHandlerContext context) | |
{ | |
Data.PaymentProcessed = true; | |
Log.Info($"ProcessPayment received with OrderId {message.OrderId}"); | |
var emailConfirmation = new EmailConfirmation | |
{ | |
OrderId = Data.OrderId | |
}; | |
await context.SendLocal(emailConfirmation); | |
} | |
public Task Handle(EmailConfirmation message, IMessageHandlerContext context) | |
{ | |
Log.Info($"Email Confirmation received with OrderId {message.OrderId}"); | |
MarkAsComplete(); | |
Log.Info($"Process Complete for OrderId {message.OrderId}"); | |
return Task.CompletedTask; | |
} | |
public Task Timeout(CancelOrder state, IMessageHandlerContext context) | |
{ | |
if (Data.PaymentProcessed) | |
{ | |
Log.Info($"Cannot cancel order since payment has been processed for OrderId {Data.OrderId}."); | |
return Task.CompletedTask; | |
} | |
Log.Info($"CompleteOrder not received soon enough OrderId {Data.OrderId}."); | |
MarkAsComplete(); | |
Log.Info($"Process Complete for OrderId {Data.OrderId}"); | |
return Task.CompletedTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment