示例#1
0
            public KillSwitchActor(ActorSystem system)
            {
                _actorDiscovery = ClusterActorDiscovery.Get(system).Discovery;

                Flow <ActorDown>(b =>
                {
                    b.Action(ad =>
                    {
                        Log.Info("Remove Killswitch Actor {Name}", ad.Actor.Path);
                        _actors
                        .FindIndex(ae => ae.Target.Equals(ad.Actor))
                        .When(i => i != -1, i => _actors.RemoveAt(i));
                    });
                });

                Flow <ActorUp>(b =>
                {
                    b.Func(au =>
                    {
                        Log.Info("New killswitch Actor {Name}", au.Actor.Path);
                        return(_actors
                               .AddAnd(ActorElement.New(au.Actor))
                               .To(_ => new RequestRegistration()));
                    })
                    .ToRefFromMsg(au => au.Actor)
                    .Then <RespondRegistration>(b1 =>
                    {
                        b1.Action(r =>
                        {
                            Log.Info("Set Killswitch Actor Type {Type} {Name}", r.RecpientType, Context.Sender.Path);
                            _actors
                            .Find(e => e.Target.Equals(Context.Sender))
                            .When(i => i != null, element => element.RecpientType = r.RecpientType);
                        });
                    });
                });

                Flow <RequestRegistration>(b =>
                                           b.Func(() => new RespondRegistration(KillRecpientType.Seed))
                                           .ToSender());

                Flow <KillClusterMsg>(b => b.Action(RunKillCluster));

                Flow <KillNode>(b =>
                                b.Action(_ =>
                {
                    Log.Info("Leaving Cluster");
                    Cluster.Get(Context.System).LeaveAsync();
                }));

                Flow <ActorUp>(b =>
                               b.Action(au => au.When(u => u.Tag == "None", up =>
                {
                    Log.Info("Incoming kill Watcher {Name}", up.Actor.Path);
                    Context.Watch(up.Actor);
                }))
                               .Then <Terminated>(b1 => b1.Func(t => new ActorDown(t.ActorRef, "None")).ToSelf()));
            }
            protected override void ConfigImpl()
            {
                Receive <ActorDown>(obs => obs.Do(ad => Log.Info($"Remove KillSwitch Actor {ad.Event.Actor.Path}"))
                                    .Select(m =>
                {
                    var(actorDown, state, _) = m;
                    var entry = state.Actors.Find(e => e.Target.Equals(actorDown.Actor));

                    if (entry == null)
                    {
                        return(state);
                    }
                    return(state with {
                        Actors = state.Actors.Remove(entry)
                    });
                }));

                Receive <ActorUp>(obs => obs.Do(au => Log.Info($"New killswitch Actor {au.Event.Actor.Path}"))
                                  .Select(m =>
                {
                    var(actorUp, state, _) = m;
                    actorUp.Actor.Tell(new RequestRegistration());
                    return(state with {
                        Actors = state.Actors.Add(ActorElement.New(actorUp.Actor))
                    });
                }));

                Receive <RespondRegistration>(obs => obs
                                              .Do(r => Log.Info($"Set Killswitch Actor Type {r.Event.RecpientType} {Sender.Path}"))
                                              .Select(r =>
                {
                    var(respondRegistration, state, _) = r;
                    var ele = state.Actors.Find(e => e.Target.Equals(Sender));
                    if (ele == null)
                    {
                        return(state);
                    }

                    return(state with
                    {
                        Actors = state.Actors.Replace(ele,
                                                      ele with {
                            RecpientType = respondRegistration.RecpientType
                        })
                    });
                }));

                Receive <RequestRegistration>(obs => obs.Select(_ => new RespondRegistration(KillRecpientType.Seed))
                                              .ToSender());

                Receive <KillClusterMsg>(obs => obs.SubscribeWithStatus(_ => RunKillCluster()));

                Receive <KillNode>(obs => obs.Do(_ => Log.Info("Leaving Cluster"))
                                   .SubscribeWithStatus(_ => Cluster.Get(Context.System).LeaveAsync()));

                Receive <ActorUp>(obs => obs.Where(m => m.Event.Tag == nameof(KillWatcher))
                                  .Do(up => Log.Info($"Incomming Kill Watcher {up.Event.Actor.Path}"))
                                  .Select(m => m.Event.Actor)
                                  .SubscribeWithStatus(actor => Context.Watch(actor)));

                Receive <Terminated>(obs => obs.Select(t => new ActorDown(t.Event.ActorRef, nameof(KillWatcher)))
                                     .ToSelf());

                Start.Subscribe(_ =>
                {
                    var actorDiscovery = ClusterActorDiscovery.Get(ObservableActor.Context.System).Discovery;

                    actorDiscovery.Tell(new RegisterActor(Self, KillSwitchName));
                    actorDiscovery.Tell(new MonitorActor(nameof(KillSwitchName)));
                });
            }