Ejemplo n.º 1
0
 public static void HasStateTypeOf <T>(ProcessId pid, string message = null)
 {
     if (!ActorContext.GetDispatcher(pid).HasStateTypeOf <T>())
     {
         failwith <Unit>(message == null ? $"Process ({pid}) doesn't have the expected state-type of {typeof(T).FullName} " : message);
     }
 }
Ejemplo n.º 2
0
 public static void CanAccept <T>(ProcessId pid, string message = null)
 {
     if (!ActorContext.GetDispatcher(pid).CanAccept <T>())
     {
         failwith <Unit>(message == null ? $"Process ({pid}) can't accept messages of type {typeof(T).FullName} " : message);
     }
 }
        /// <summary>
        /// Spawns a new process with Count worker processes, each message is mapped
        /// and sent to the least busy worker
        /// </summary>
        /// <typeparam name="S">State type</typeparam>
        /// <typeparam name="T">Message type</typeparam>
        /// <param name="Name">Delegator process name</param>
        /// <param name="Count">Number of worker processes</param>
        /// <param name="Inbox">Worker message handler</param>
        /// <param name="Flags">Process flags</param>
        /// <param name="Strategy">Failure supervision strategy</param>
        /// <returns>Process ID of the delegator process</returns>
        public static ProcessId leastBusyMap <S, T, U>(
            ProcessName Name,
            int Count,
            Func <S> Setup,
            Func <S, U, S> Inbox,
            Func <T, U> Map,
            ProcessFlags Flags = ProcessFlags.Default,
            State <StrategyContext, Unit> Strategy = null,
            int MaxMailboxSize = ProcessSetting.DefaultMailboxSize,
            string WorkerName  = "worker"
            )
        {
            if (Inbox == null)
            {
                throw new ArgumentNullException(nameof(Inbox));
            }
            if (Setup == null)
            {
                throw new ArgumentNullException(nameof(Setup));
            }
            if (Count < 1)
            {
                throw new ArgumentException($"{nameof(Count)} should be greater than 0");
            }

            return(spawn <Unit, T>(
                       Name,
                       () =>
            {
                spawnMany(Count, WorkerName, Setup, Inbox, Flags);
                return unit;
            },
                       (_, msg) =>
            {
                var umsg = Map(msg);

                var disps = (from child in Children.Map(c => Tuple(c, ActorContext.GetDispatcher(c))).Values
                             let count = child.Item2.GetInboxCount()
                                         where count >= 0
                                         orderby count
                                         select child)
                            .ToList();

                if (disps.Count == 0)
                {
                    throw new NoRouterWorkersException();
                }
                else
                {
                    fwd(disps.First().Item1, umsg);
                }
                return unit;
            },
                       Flags,
                       Strategy,
                       MaxMailboxSize
                       ));
        }
Ejemplo n.º 4
0
        public static void HasStateTypeOf <T>(ProcessId pid, string message = null)
        {
            var res = ActorContext.GetDispatcher(pid).HasStateTypeOf <T>();

            res.IfLeft(err =>
            {
                failwith <Unit>($"{err} for {pid}");
            });
        }
        /// <summary>
        /// Spawns a new process with that routes each message is mapped and
        /// sent to the least busy worker
        /// </summary>
        /// <typeparam name="S">State type</typeparam>
        /// <typeparam name="T">Message type</typeparam>
        /// <param name="Name">Delegator process name</param>
        /// <param name="Count">Number of worker processes</param>
        /// <param name="Inbox">Worker message handler</param>
        /// <param name="Flags">Process flags</param>
        /// <param name="Strategy">Failure supervision strategy</param>
        /// <returns>Process ID of the delegator process</returns>
        public static ProcessId leastBusyMap <T, U>(
            ProcessName Name,
            IEnumerable <ProcessId> Workers,
            Func <T, U> Map,
            RouterOption Options = RouterOption.Default,
            ProcessFlags Flags   = ProcessFlags.Default,
            int MaxMailboxSize   = ProcessSetting.DefaultMailboxSize
            )
        {
            if (Workers == null)
            {
                throw new ArgumentNullException(nameof(Workers));
            }
            var workers = Set.createRange(Workers);

            if (workers.Count < 1)
            {
                throw new ArgumentException($"{nameof(Workers)} should have a length of at least 1");
            }
            var router = spawn <T>(
                Name,
                msg =>
            {
                var umsg  = Map(msg);
                var disps = (from child in workers.Map(c => Tuple(c, ActorContext.GetDispatcher(c)))
                             let count = child.Item2.GetInboxCount()
                                         where count >= 0
                                         orderby count
                                         select child)
                            .ToList();

                if (disps.Count == 0)
                {
                    throw new NoRouterWorkersException();
                }
                else
                {
                    fwd(disps.First().Item1, msg);
                }
            },
                Flags,
                DefaultStrategy,
                MaxMailboxSize,
                Terminated: pid => workers = workers.Remove(pid)
                );

            return(WatchWorkers(router, workers, Options));
        }
Ejemplo n.º 6
0
 public Unit DispatchUnWatch(ProcessId pid)
 {
     ActorContext.GetDispatcher(pid).UnWatch(Id);
     return(ActorContext.RemoveWatcher(pid, Id));
 }
Ejemplo n.º 7
0
 public Unit DispatchWatch(ProcessId pid)
 {
     ActorContext.GetDispatcher(pid).Watch(Id);
     return(ActorContext.AddWatcher(pid, Id));
 }
Ejemplo n.º 8
0
 private IEnumerable <IActorDispatch> GetWorkers() =>
 group.Map(pid => ActorContext.GetDispatcher(pid));
Ejemplo n.º 9
0
 /// <summary>
 /// Find the number of items in the Process inbox
 /// </summary>
 /// <param name="pid">Process</param>
 /// <returns>Number of items in the Process inbox</returns>
 public static int inboxCount(ProcessId pid) =>
 ActorContext.GetDispatcher(pid).GetInboxCount();
Ejemplo n.º 10
0
 /// <summary>
 /// Stop watching for the death of the watching process
 /// </summary>
 /// <param name="watcher">Watcher</param>
 /// <param name="watching">Watched</param>
 public static Unit unwatch(ProcessId watcher, ProcessId watching) =>
 ActorContext.GetDispatcher(watcher).DispatchUnWatch(watching);
Ejemplo n.º 11
0
 /// <summary>
 /// Find out if a process exists
 ///
 ///     Rules:
 ///         * Local processes   - the process must actually be alive and in-memory
 ///         * Remote processes  - the process must have an inbox to receive messages
 ///                               and may be active, but it's not required.
 ///         * Dispatchers/roles - at least one process in the collection must exist(pid)
 ///         * JS processes      - not current supported
 /// </summary>
 /// <param name="pid">Process ID to check</param>
 /// <returns>True if exists</returns>
 public static bool exists(ProcessId pid) =>
 ActorContext.GetDispatcher(pid).Exists;