public async Task <IInventoryServiceCompletedMessage> PerformOperation(IRequestMessage request, Task <OperationResult <IRealTimeInventory> > response, IRealTimeInventory originalInventory) { var perf = new TestPerformanceService(); perf.Init(); return((await response).ProcessAndSendResult(request, CompletedMessageFactory.GetResponseCompletedMessage(request), originalInventory, null, perf).InventoryServiceCompletedMessage); }
private bool CanProcessMessage(string productId, IRequestMessage message) { if (_id == productId) { return(true); } var errorMessage = "Invalid request made to " + nameof(ProductInventoryActor) + " with an id of " + productId + " but my Id is " + _id + ". Message will not be processed "; Logger.Error(errorMessage); new OperationResult <IRealTimeInventory>() { IsSuccessful = false, Exception = new RealTimeInventoryException() { ErrorMessage = errorMessage } }.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message, false), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService); NotificationActorRef.Tell(errorMessage); return(false); }
public ProductInventoryActor(IInventoryStorage inventoryStorage, IActorRef InventoryQueryActorRef, string id, bool withCache, IPerformanceService performanceService) { PerformanceService = performanceService; _id = id; _withCache = withCache; InventoryStorage = inventoryStorage; RealTimeInventory = RealTimeInventory.InitializeFromStorage(InventoryStorage, id); NotificationActorRef = InventoryQueryActorRef; ReceiveAsync <GetInventoryMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } if (_withCache == false) { var result = await RealTimeInventory.ReadInventoryFromStorageAsync(InventoryStorage, message.ProductId); RealTimeInventory = result.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; } else { RealTimeInventory = RealTimeInventory.ToSuccessOperationResult().ProcessAndSendResult(message, (rti) => new GetInventoryCompletedMessage(rti, true), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; } }); ReceiveAsync <ReserveMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var result = await RealTimeInventory.ReserveAsync(InventoryStorage, message.ProductId, message.Update); RealTimeInventory = result.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); ReceiveAsync <UpdateQuantityMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var result = await RealTimeInventory.UpdateQuantityAsync(InventoryStorage, message.ProductId, message.Update); RealTimeInventory = result.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); ReceiveAsync <UpdateAndHoldQuantityMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var updateandHoldResultesult = await RealTimeInventory.UpdateQuantityAndHoldAsync(InventoryStorage, message.ProductId, message.Update); RealTimeInventory = updateandHoldResultesult.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); ReceiveAsync <PlaceHoldMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var result = await RealTimeInventory.PlaceHoldAsync(InventoryStorage, message.ProductId, message.Update); RealTimeInventory = result.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); ReceiveAsync <PurchaseMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var result = await RealTimeInventory.PurchaseAsync(InventoryStorage, message.ProductId, message.Update); RealTimeInventory = result.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); ReceiveAsync <PurchaseFromHoldsMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var result = await RealTimeInventory.PurchaseFromHoldsAsync(InventoryStorage, message.ProductId, message.Update).ConfigureAwait(false); RealTimeInventory = result.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); ReceiveAsync <FlushStreamsMessage>(async message => { var result = await RealTimeInventory.InventoryStorageFlushAsync(InventoryStorage, _id); Sender.Tell(result.Data); }); ReceiveAsync <ResetInventoryQuantityReserveAndHoldMessage>(async message => { if (!CanProcessMessage(message.ProductId, message)) { return; } var updateandHoldResultesult = await RealTimeInventory.ResetInventoryQuantityReserveAndHoldAsync(InventoryStorage, message.ProductId, message.Update, message.Reservations, message.Holds); RealTimeInventory = updateandHoldResultesult.ProcessAndSendResult(message, CompletedMessageFactory.GetResponseCompletedMessage(message), Logger, RealTimeInventory, Sender, NotificationActorRef, PerformanceService).RealTimeInventory; }); #if DEBUG // Context.System.Scheduler.ScheduleTellRepeatedly(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5), Nobody.Instance, RealTimeInventory, Self); #endif }