public GridNodeController(IActorRef commandPipe, IActorRef transportProxy)
 {
     _monitor = new ActorMonitor(Context);
     Receive <HeartBeat>(m => {
         _monitor.IncrementMessagesReceived();
         Sender.Tell(Alive.Instance);
     });
     Receive <Connect>(m => {
         _monitor.IncrementMessagesReceived();
         Sender.Tell(new Connected(commandPipe, transportProxy));
     });
 }
Beispiel #2
0
        public void Handle(CreateActorRouteMessage msg)
        {
            _monitor.IncrementMessagesReceived();
            var handleActor = CreateActor(msg.ActorType, CreateActorRouter(msg), msg.ActorName);

            foreach (var msgRoute in msg.Routes)
            {
                Log.Info("Subscribed {actor} to {messageType}", handleActor.Path, msgRoute.MessageType);
                _subscriber.Subscribe(msgRoute.MessageType, handleActor, Self);
            }

            Sender.Tell(RouteCreated.Instance);
        }
        public virtual void Handle(IMessageMetadataEnvelop <TMessage> msg)
        {
            _monitor.IncrementMessagesReceived();
            _log.Trace("Handler actor got message: {@Message}", msg);

            try
            {
                var handlerWithMetadata = _handler as IHandlerWithMetadata <TMessage>;
                if (handlerWithMetadata != null)
                {
                    handlerWithMetadata.Handle(msg.Message, msg.Metadata);
                }
                else
                {
                    _handler.Handle(msg.Message);
                }
            }
            catch (Exception e)
            {
                _log.Error(e, "Handler actor raised an error on message process: {@Message}", msg);

                var metadata = msg.Metadata.CreateChild(Guid.Empty,
                                                        new ProcessEntry(typeof(THandler).Name, MessageHandlingStatuses.PublishingFault, MessageHandlingStatuses.MessageProcessCasuedAnError));

                var fault = Fault.New(msg.Message, e, GetSagaId(msg.Message), typeof(THandler));

                _publisher.Publish(fault, metadata);
            }
        }
Beispiel #4
0
        public EventSourcedActor(IConstructAggregates aggregateConstructor,
                                 ISnapshotsPersistencePolicy policy,
                                 IPublisher publisher)
        {
            PersistenceId         = Self.Path.Name;
            SnapshotsPolicy       = policy;
            _aggregateConstructor = aggregateConstructor;
            Publisher             = publisher;
            Id      = AggregateActorName.Parse <T>(Self.Path.Name).Id;
            State   = (AggregateBase)aggregateConstructor.Build(typeof(T), Id, null);
            Monitor = new ActorMonitor(Context, typeof(T).Name);
            Command <GracefullShutdownRequest>(req =>
            {
                Monitor.IncrementMessagesReceived();
                DeleteSnapshots(SnapshotsPolicy.GetSnapshotsToDelete());
                Become(Terminating);
            });

            Command <CheckHealth>(s => Sender.Tell(new HealthStatus(s.Payload)));

            Command <SaveSnapshotSuccess>(s =>
            {
                NotifyWatchers(s);
                SnapshotsPolicy.MarkSnapshotSaved(s.Metadata);
            });

            Command <NotifyOnPersistenceEvents>(c =>
            {
                var waiter = c.Waiter ?? Sender;
                if (IsRecoveryFinished)
                {
                    waiter.Tell(RecoveryCompleted.Instance);
                }

                _persistenceWaiters.Add(waiter);
            });

            Recover <DomainEvent>(e =>
            {
                State.ApplyEvent(e);
                SnapshotsPolicy.MarkActivity(e.CreatedTime);
            });

            Recover <SnapshotOffer>(offer =>
            {
                SnapshotsPolicy.MarkSnapshotApplied(offer.Metadata);
                State = _aggregateConstructor.Build(typeof(T), Id, (IMemento)offer.Snapshot);
            });

            Recover <RecoveryCompleted>(message =>
            {
                _log.Debug("Recovery for actor {Id} is completed", PersistenceId);
                NotifyWatchers(message);
            });
        }
        protected override void OnReceive(object msg)
        {
            _monitor.IncrementMessagesReceived();
            Logger.Trace("{ActorHub} received {@message}", Self.Path, msg);

            msg.Match()
            .With <ClearChilds>(m => Clear())
            .With <CheckHealth>(s => Sender.Tell(new HealthStatus(s.Payload)))
            .With <IMessageMetadataEnvelop>(messageWitMetadata =>
            {
                ChildInfo knownChild;

                messageWitMetadata.Metadata.History.Add(new ProcessEntry(Self.Path.Name, "Forwarding to child", "All messages should be forwarded"));

                var childId = GetChildActorId(messageWitMetadata.Message);
                var name    = GetChildActorName(messageWitMetadata.Message);

                bool childWasCreated = false;
                if (!Children.TryGetValue(childId, out knownChild))
                {
                    childWasCreated    = true;
                    var childActorType = GetChildActorType(messageWitMetadata.Message);
                    var props          = Context.DI().Props(childActorType);
                    var childActorRef  = Context.ActorOf(props, name);
                    knownChild         = new ChildInfo(childActorRef);
                    Children[childId]  = knownChild;
                }

                knownChild.LastTimeOfAccess = BusinessDateTime.UtcNow;
                knownChild.ExpiresAt        = knownChild.LastTimeOfAccess + ChildMaxInactiveTime;
                knownChild.Ref.Tell(messageWitMetadata);

                Logger.Trace("Message {@msg} sent to {isknown} child {id}",
                             msg,
                             childWasCreated ? "known" : "unknown",
                             childId);
            });
        }