Example #1
0
        private void Processing()
        {
            Logger.Debug("Inventory Actor Processing started ...");

            NotificationActorRef      = Context.ActorOf(Props.Create(() => new InventoryQueryActor(BackUpService)).WithMailbox(nameof(GetAllInventoryListMailbox)), typeof(InventoryQueryActor).Name);
            InventoryServicePingActor = Context.ActorOf(Props.Create(() => new InventoryServicePingActor()), typeof(InventoryServicePingActor).Name);

            Receive <RemoveProductMessage>(message =>
            {
                var productId = message?.RealTimeInventory?.ProductId;

                Logger.Debug("Actor " + productId + " has requested to be removed because " + message?.Reason?.Message + " and so will no longer be sent messages.", message);

                if (!string.IsNullOrEmpty(productId))
                {
                    _products.Remove(productId);
                    _removedRealTimeInventories[productId] = message;
                    NotificationActorRef.Tell(productId + " Actor has died! Reason : " + message?.Reason?.Message);
                }
            });

            Receive <GetRemovedProductMessage>(message =>
            {
                Sender.Tell(new GetRemovedProductCompletedMessage(_removedRealTimeInventories.Select(x => x.Value).ToList()));
            });

            Receive <QueryInventoryListMessage>(message =>
            {
                foreach (var product in _products)
                {
                    product.Value.Tell(new GetInventoryMessage(product.Key));
                }
            });

            Receive <GetInventoryCompletedMessage>(message =>
            {
                //todo remove this
                // _realTimeInventories[message.RealTimeInventory.ProductId] = message.RealTimeInventory;
                // NotificationActorRef.Tell(new QueryInventoryListCompletedMessage(new List<IRealTimeInventory>() { message.RealTimeInventory }));
            });

            Receive <IRequestMessage>(message =>
            {
                PerformanceService.Increment("Incoming Message");
                Logger.Debug(message.GetType().Name + " received for " + message.ProductId + " for update " + message.Update);
                var actorRef = GetActorRef(InventoryStorage, message.ProductId, PerformanceService);
                actorRef.Forward(message);
            });
            Receive <GetMetrics>(_ =>
            {
                PerformanceService.PrintMetrics();
            });
#if DEBUG
            Context.System.Scheduler.ScheduleTellRepeatedly(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000), Self, new GetMetrics(), Nobody.Instance);
#endif
        }
Example #2
0
 protected override SupervisorStrategy SupervisorStrategy()
 {
     return(new OneForOneStrategy(
                x =>
     {
         var message = x.Message + " - " + x.InnerException?.Message + " - it's possible an inventory actor has mal-functioned so i'm going to stop it :( ";
         Logger.Error(message);
         NotificationActorRef.Tell(message);
         return Directive.Restart;
     }));
 }
Example #3
0
        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);
        }