Exemple #1
0
        private static void Demo6BackSupervision()
        {
            Console.WriteLine("Creating SimpleActor Props with BackOff Supervisor");
            var childProps = SimpleActor.CreateProps();

            var supervisor = BackoffSupervisor.Props(
                Backoff.OnFailure(
                    childProps: childProps,
                    childName: "SimpleActor",
                    minBackoff: TimeSpan.FromSeconds(3),
                    maxBackoff: TimeSpan.FromSeconds(60),
                    randomFactor: 0.2)
                .WithAutoReset(TimeSpan.FromSeconds(160)));

            var simple = ActorsSystem.Instance.ActorOf(supervisor, "SimpleActor:Supervisor");

            Console.WriteLine("Press enter to start raise exception");
            Console.ReadLine();
            simple.Tell(new ExceptionMessage());
            Console.WriteLine("Exception Raised");
            Console.ReadLine();
            simple.Tell(new ExceptionMessage());
            Console.WriteLine("Exception Raised");
            Console.ReadLine();
            simple.Tell(new ExceptionMessage());
            Console.WriteLine("Exception Raised");
        }
        public void BackoffSupervisor_must_not_reply_to_sender_if_replyWhileStopped_is_not_specified()
        {
            EventFilter.Exception <TestException>().Expect(1, () =>
            {
                var supervisor = Create(Backoff.OnFailure(Child.Props(TestActor), "c1", TimeSpan.FromSeconds(100), TimeSpan.FromSeconds(300), 0.2, -1));
                supervisor.Tell(BackoffSupervisor.GetCurrentChild.Instance);

                var c1 = ExpectMsg <BackoffSupervisor.CurrentChild>().Ref;
                Watch(c1);
                supervisor.Tell(BackoffSupervisor.GetRestartCount.Instance);
                ExpectMsg <BackoffSupervisor.RestartCount>().Count.Should().Be(0);

                c1.Tell("boom");
                ExpectTerminated(c1);

                AwaitAssert(() =>
                {
                    supervisor.Tell(BackoffSupervisor.GetRestartCount.Instance);
                    ExpectMsg <BackoffSupervisor.RestartCount>().Count.Should().Be(1);
                });

                supervisor.Tell("boom"); //this will be sent to deadLetters
                ExpectNoMsg(500);
            });
        }
        public void BackoffOnRestartSupervisor_must_respect_withinTimeRange_property_of_OneForOneStrategy()
        {
            var probe = CreateTestProbe();
            // withinTimeRange indicates the time range in which maxNrOfRetries will cause the child to
            // stop. IE: If we restart more than maxNrOfRetries in a time range longer than withinTimeRange
            // that is acceptable.
            var options = Backoff.OnFailure(TestActor.Props(probe.Ref), "someChildName", 300.Milliseconds(), 10.Seconds(), 0.0, -1)
                          .WithSupervisorStrategy(new OneForOneStrategy(3, 1.Seconds(), ex => ex is StoppingException
                    ? Directive.Stop
                    : SupervisorStrategy.DefaultStrategy.Decider.Decide(ex)));
            var supervisor = Sys.ActorOf(BackoffSupervisor.Props(options));

            supervisor.Tell(BackoffSupervisor.GetCurrentChild.Instance);
            probe.ExpectMsg("STARTED");

            probe.Watch(supervisor);
            // Throw three times rapidly
            for (int i = 1; i <= 3; i++)
            {
                supervisor.Tell("THROW");
                probe.ExpectMsg("STARTED");
            }

            // Now wait the length of our window, and throw again. We should still restart.
            Thread.Sleep(1000);
            supervisor.Tell("THROW");
            probe.ExpectMsg("STARTED");
            // Now we'll issue three more requests, and should be terminated.
            supervisor.Tell("THROW");
            probe.ExpectMsg("STARTED");
            supervisor.Tell("THROW");
            probe.ExpectMsg("STARTED");
            supervisor.Tell("THROW");
            probe.ExpectTerminated(supervisor);
        }
Exemple #4
0
        public void BackoffSupervisorOnFailure(ApplicationEnvironment applicationEnvironment)
        {
            ActorSystem actorSystem = ActorSystem.Create("app");
            // This class represents a configuration object used in creating an
            // ActorBase actor
            Props childProps = Props.Create <EchoActor>();
            //
            TimeSpan minBackoff = TimeSpan.FromSeconds(3);

            TimeSpan maxBackoff     = TimeSpan.FromSeconds(30);
            double   randomFactor   = 0.2;
            int      maxNrOfRetries = 2;
            // Builds back-off options for creating a back-off supervisor.
            BackoffOptions backoffOptions  = Backoff.OnFailure(childProps, "myEcho", minBackoff, maxBackoff, randomFactor, maxNrOfRetries);
            Props          supervisor      = BackoffSupervisor.Props(backoffOptions);
            IActorRef      supervisorActor = actorSystem.ActorOf(supervisor, "echoSupervisor");

            supervisorActor.Tell("EchoMessage1");
            supervisorActor.Tell(new Exception("File not found exception"));
            TestUtilities.ThreadSleepSeconds(5);
            supervisorActor.Tell("EchoMessage2");
            TestUtilities.ThreadSleepSeconds(5);
            supervisorActor.Tell("EchoMessage3");
            TestUtilities.MethodEnds();
        }
Exemple #5
0
        //TODO: move to constructor
        private IActorRef SpawnRunner()
        {
            Props runnerProps;

            try
            {
                runnerProps = Context.DI().Props <TJobRunner>().WithDispatcher(Context.Props.Dispatcher);
            }
            catch (Exception ex)
            {
                Context.GetLogger().Error(ex, "No DI available at the moment, falling back to default props creation.");
                runnerProps = Props.Create <TJobRunner>().WithDispatcher(Context.Props.Dispatcher);
            }

            var runnerSupervisorProps =
                BackoffSupervisor.Props(
                    Backoff.OnFailure(
                        runnerProps,
                        $"{Name}-runner",
                        TimeSpan.FromSeconds(10),
                        TimeSpan.FromSeconds(60),
                        0.2,
                        3)).WithDispatcher(Context.Props.Dispatcher);

            var runner = Context.ActorOf(runnerSupervisorProps, $"{Name}-runner-supervisor");

            return(runner);
        }
        private Props SupervisorProps(IActorRef probeRef)
        {
            var options = Backoff.OnFailure(TestActor.Props(probeRef), "someChildName", 200.Milliseconds(), 10.Seconds(), 0.0, -1)
                          .WithSupervisorStrategy(new OneForOneStrategy(4, TimeSpan.FromSeconds(30), ex => ex is StoppingException
                    ? Directive.Stop
                    : SupervisorStrategy.DefaultStrategy.Decider.Decide(ex)));

            return(BackoffSupervisor.Props(options));
        }
 public static Props Props(string id, string card)
 {
     return(BackoffSupervisor.Props(
                Backoff.OnFailure(
                    Akka.Actor.Props.Create <EcomAuthorizeActor>(id, card),
                    nameof(EcomAuthorizeActor) + id,
                    TimeSpan.FromSeconds(3),
                    TimeSpan.FromSeconds(20),
                    0.2)));
 }
Exemple #8
0
        protected override void PreStart()
        {
            log.Info("Starting");

            var subscriberProps = Props.Create(() => new SubscriptionActor(Self, this.mqttConfig, this.subscriptionTopic, this.alexaResponseTopic));
            var supervisedProps = BackoffSupervisor.Props(
                Backoff.OnFailure(
                    subscriberProps,
                    childName: $"subscription-{this.subscriptionTopic}",
                    minBackoff: TimeSpan.FromSeconds(3),
                    maxBackoff: TimeSpan.FromSeconds(60),
                    randomFactor: 0.2,
                    maxNrOfRetries: 5
                    ));

            this.subscriber = Context.ActorOf(supervisedProps, $"supervisor-sub-{this.subscriptionTopic}");
        }
Exemple #9
0
        public void BackoffOnRestartSupervisor_must_accept_commands_while_child_is_terminating()
        {
            var postStopLatch = CreateTestLatch(1);
            var options       = Backoff.OnFailure(SlowlyFailingActor.Props(postStopLatch), "someChildName", 1.Ticks(), 1.Ticks(), 0.0)
                                .WithSupervisorStrategy(new OneForOneStrategy(ex =>
            {
                if (ex is StoppingException)
                {
                    return(Directive.Stop);
                }

                return(SupervisorStrategy.DefaultStrategy.Decider.Decide(ex));
            }));
            var supervisor = Sys.ActorOf(BackoffSupervisor.Props(options));

            supervisor.Tell(BackoffSupervisor.GetCurrentChild.Instance);
            // new instance
            var child = ExpectMsg <BackoffSupervisor.CurrentChild>().Ref;

            child.Tell("PING");
            ExpectMsg("PONG");

            supervisor.Tell("THROW");
            ExpectMsg("THROWN");

            child.Tell("PING");
            ExpectNoMsg(100.Milliseconds()); // Child is in limbo due to latch in postStop. There is no Terminated message yet

            supervisor.Tell(BackoffSupervisor.GetCurrentChild.Instance);
            ExpectMsg <BackoffSupervisor.CurrentChild>().Ref.Should().BeSameAs(child);

            supervisor.Tell(BackoffSupervisor.GetRestartCount.Instance);
            ExpectMsg <BackoffSupervisor.RestartCount>().Count.Should().Be(0);

            postStopLatch.CountDown();

            // New child is ready
            AwaitAssert(() =>
            {
                supervisor.Tell(BackoffSupervisor.GetCurrentChild.Instance);
                // new instance
                ExpectMsg <BackoffSupervisor.CurrentChild>().Ref.Should().NotBeSameAs(child);
            });
        }
Exemple #10
0
        private void ReceivedEstablishSubscription(MqttMessage.EstablishSubscription message)
        {
            log.Info($"Establishing subscription actor (currently {this.registeredProcessors.Count} processors registered)");
            if (!this.subscriber.IsNobody())
            {
                Context.Unwatch(this.subscriber);
            }
            var subscriberProps = Props.Create(() => new SubscriptionActor(Self, this.mqttConfig, this.subscriptionTopic, this.alexaResponseTopic));
            var supervisedProps = BackoffSupervisor.Props(
                Backoff.OnFailure(
                    subscriberProps,
                    childName: $"subscription-{this.subscriptionTopic}",
                    minBackoff: TimeSpan.FromSeconds(3),
                    maxBackoff: TimeSpan.FromSeconds(600),
                    randomFactor: 0.2,
                    maxNrOfRetries: 10
                    ));

            this.subscriber = Context.ActorOf(supervisedProps, $"supervisor-sub-{this.subscriptionTopic}");
            Context.Watch(this.subscriber);
            this.registeredProcessors.ForEach(this.subscriber.Tell);
        }
Exemple #11
0
        public JobManager()
        {
            var schedulerProps = Props.Create(typeof(TJobScheduler)).WithDispatcher(Context.Props.Dispatcher);
            var schedulerName  = $"{Name}-scheduler";

            var schedulerSupervisorProps =
                BackoffSupervisor.Props(
                    Backoff.OnFailure(
                        schedulerProps,
                        schedulerName,
                        TimeSpan.FromSeconds(10),
                        TimeSpan.FromSeconds(60),
                        0.2,
                        3)).WithDispatcher(Context.Props.Dispatcher);

            JobRunner    = SpawnRunner();
            JobScheduler = Context.ActorOf(schedulerSupervisorProps, $"{schedulerName}-supervisor");

            Log = Context.GetLogger();

            Receive <TJob>(Forward);
            Receive <SchedulerMessage <TJob, TIdentity> >(Forward);
        }
 private BackoffOptions OnFailureOptions(Props props, int maxNrOfRetries = -1) => Backoff.OnFailure(props, "c1", 100.Milliseconds(), 3.Seconds(), 0.2, maxNrOfRetries);
Exemple #13
0
 private BackoffOptions OnFailureOptions(Props props) => Backoff.OnFailure(props, "c1", 100.Milliseconds(), 3.Seconds(), 0.2);