Ejemplo n.º 1
0
        private bool Handle(Shared.Messages.Command command)
        {
            // create a command handling actor instance if needed and then
            // forward the command
            var childActorId = GetHashMapping(command);
            var child        = Context.Child(childActorId);

            if (child == ActorRefs.Nobody)
            {
                child = Context.ActorOf(Props.Create <CommandHandlerWorkerActor>(_writer), childActorId);
            }
            // check for stop, poisonpill, kill, gracefulstop commands..
            // It's responsibility of this Actor to manage its children
            switch (command)
            {
            case StopCommand cmd:
                Context.Stop(child);
                return(true);

            case PoisonPillCommand cmd:
                child.Tell(PoisonPill.Instance);
                return(true);

            case KillCommand cmd:
                child.Tell(Kill.Instance);
                return(true);

            case GracefulStopCommand cmd:
                try
                {
                    var gracefulStop = child.GracefulStop(TimeSpan.FromSeconds(5));
                    gracefulStop.Wait();
                    ColoredConsole.WriteLineGreen("GracefulStop completed");
                }
                catch (AggregateException ex)
                {
                    // the GracefulStop can fail if it cannot complete within the specified TimeSpan.
                    // The Task will be cancelled.
                    ColoredConsole.WriteLineYellow($"GracefulStop failed, exception: {ex}");
                }
                return(true);
            }

            // child.Tell(command);
            child.Forward(command);
            return(true);
        }
Ejemplo n.º 2
0
 private static string GetHashMapping(Shared.Messages.Command command)
 {
     return(command.Context.UserId);
 }