Ejemplo n.º 1
0
        public static PromiseActorRef Apply(IActorRefProvider provider, TimeSpan timeout, object targetName,
                                            string messageClassName, IActorRef sender = null)
        {
            sender = sender ?? ActorRefs.NoSender;
            var result    = new TaskCompletionSource <object>();
            var a         = new PromiseActorRef(provider, result, messageClassName);
            var scheduler = provider.Guardian.Underlying.System.Scheduler.Advanced;
            var c         = new Cancelable(scheduler, timeout);

            scheduler.ScheduleOnce(timeout, () => result.TrySetResult(new Status.Failure(new AskTimeoutException(
                                                                                             string.Format("Ask timed out on [{0}] after [{1} ms]. Sender[{2}] sent message of type {3}.", targetName, timeout.TotalMilliseconds, sender, messageClassName)))),
                                   c);

            result.Task.ContinueWith(r =>
            {
                try
                {
                    a.Stop();
                }
                finally
                {
                    c.Cancel(false);
                }
            }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously);

            return(a);
        }
Ejemplo n.º 2
0
        public static Task <bool> GracefulStop(this IActorRef target, TimeSpan timeout, object stopMessage)
        {
            var internalTarget = target.AsInstanceOf <IInternalActorRef>();

            var promiseRef = PromiseActorRef.Apply(internalTarget.Provider, timeout, target, stopMessage.GetType().Name);

            internalTarget.SendSystemMessage(new Watch(internalTarget, promiseRef));
            target.Tell(stopMessage, ActorRefs.NoSender);
            return(promiseRef.Result.ContinueWith(t =>
            {
                var returnResult = false;
                PatternMatch.Match(t.Result)
                .With <Terminated>(terminated =>
                {
                    returnResult = (terminated.ActorRef.Path.Equals(target.Path));
                })
                .Default(m =>
                {
                    returnResult = false;
                });

                internalTarget.SendSystemMessage(new Unwatch(internalTarget, promiseRef));
                return returnResult;
            }, TaskContinuationOptions.ExecuteSynchronously));
        }