示例#1
0
        public InstallManagerActor(IAppRegistry registry, IConfiguration configuration, IAppManager manager, IAutoUpdater autoUpdater)
        {
            SubscribeAbility ability = new SubscribeAbility(this);

            ability.MakeReceive();

            Receive <InstallerationCompled>(ic => ability.Send(ic));
            Receive <InstallRequest>(o => Context.ActorOf(Props.Create <ActualInstallerActor>(registry, configuration, autoUpdater)).Forward(o));
            Receive <UninstallRequest>(o => Context.ActorOf(Props.Create <ActualUninstallationActor>(registry, manager)).Forward(o));
        }
示例#2
0
        //public ITimerScheduler Timers { get; set; } = null!;

        private HostApiManagerActor()
        {
            var subscribeAbility = new SubscribeAbility(this);

            subscribeAbility.NewSubscription += subscribe =>
            {
                foreach (var(path, hostEntry) in _entries)
                {
                    subscribe.Intrest.Tell(new HostEntryChanged(hostEntry.Name, path, false));
                }
            };

            Flow <ActorDown>(b =>
                             b.Action(ad =>
            {
                if (_entries.Remove(ad.Actor.Path, out var entry))
                {
                    subscribeAbility.Send(new HostEntryChanged(entry.Name, entry.Actor.Path, true));
                }
            }));

            Flow <ActorUp>(b =>
                           b.Func(au =>
            {
                _entries[au.Actor.Path] = new HostEntry(string.Empty, au.Actor);
                subscribeAbility.Send(new HostEntryChanged(string.Empty, au.Actor.Path, false));
                return(new GetHostName());
            }).ToRefFromMsg(au => au.Actor)
                           .Then <GetHostNameResult>(b1 => b1.Action(r =>
            {
                if (!_entries.TryGetValue(Sender.Path, out var entry))
                {
                    return;
                }

                entry.Name = r.Name;
                subscribeAbility.Send(new HostEntryChanged(entry.Name, entry.Actor.Path, false));
            })));

            Receive <CommandBase>(c =>
            {
                var he = _entries.Values.FirstOrDefault(e => e.Name == c.Target);
                if (he == null)
                {
                    Sender.Tell(new OperationResponse(false));
                    return;
                }

                he.Actor.Forward(c);
            });

            subscribeAbility.MakeReceive();
        }
        public AppRegistryActor(IConfiguration configuration, IAppManager appManager)
        {
            _appManager       = appManager;
            _subscribeAbility = new SubscribeAbility(this);
            _appsDirectory    = Path.GetFullPath(configuration["AppsLocation"]);

            Flow <AllAppsQuery>(b => b.Func(_ => new AllAppsResponse(_apps.Keys.ToArray())).ToSender());

            Receive <LoadData>(HandleLoadData);
            Receive <SaveData>(HandleSaveData);

            Receive <InstalledAppQuery>(HandleQueryApp);
            Receive <NewRegistrationRequest>(HandleNewRegistration);
            Receive <UpdateRegistrationRequest>(HandleUpdateRequest);

            Receive <QueryHostApps>(ProcessQuery);
            Receive <AppStatusResponse>(FinishQuery);

            _subscribeAbility.MakeReceive();
        }
示例#4
0
        public DataTransferManagerActor()
        {
            var subscribe = new SubscribeAbility(this);

            Flow <TransferMessages.TransmitRequest>(b =>
                                                    b.Action(r =>
            {
                var op = Context.Child(r.OperationId);
                if (!op.Equals(ActorRefs.Nobody))
                {
                    r.From.Tell(new TransferFailed(r.OperationId, FailReason.DuplicateOperationId, null));
                    return;
                }

                Context.ActorOf(Props.Create <TransferOperatorActor>(), r.OperationId).Tell(r);
            }));

            Flow <TransferMessages.DataTranfer>(b => b.Action(tm =>
            {
                switch (tm)
                {
                case TransferMessages.RequestAccept acc:
                    _pendingTransfers.Remove(acc.OperationId);
                    break;

                case TransferMessages.RequestDeny den:
                    _pendingTransfers.Remove(den.OperationId);
                    break;
                }
                Context.Child(tm.OperationId).Tell(tm);
            }));

            Flow <DataTransferRequest>(b =>
                                       b.Action(r =>
            {
                var op = Context.Child(r.OperationId);
                if (!op.Equals(ActorRefs.Nobody))
                {
                    r.Target.Actor.Tell(new TransferFailed(r.OperationId, FailReason.DuplicateOperationId, null));
                    Self.Tell(new TransferFailed(r.OperationId, FailReason.DuplicateOperationId, null));
                    return;
                }

                Context.ActorOf(Props.Create <TransferOperatorActor>(), r.OperationId).Forward(r);
            }));

            Flow <IncomingDataTransfer>(b => b.Action(dt =>
            {
                if (_awaiters.TryGetValue(dt.OperationId, out var awaitRequest))
                {
                    awaitRequest.Target.Tell(new AwaitResponse(dt));
                    _awaiters.Remove(dt.OperationId);
                }
                else
                {
                    _pendingTransfers[dt.OperationId] = dt;
                }

                subscribe.Send(dt);
            }));

            Flow <TransferMessages.TransferCompled>(b =>
                                                    b.Action(tc =>
            {
                Context.Stop(Context.Child(tc.OperationId));
                subscribe.Send(tc, tc.GetType());
            }));

            Flow <TransferMessages.TransferMessage>(b =>
                                                    b.Action(tm =>
            {
                Context.Child(tm.OperationId).Tell(tm);
                subscribe.Send(tm, tm.GetType());
            }));

            Flow <AwaitRequest>(b => b.Action(r =>
            {
                if (_pendingTransfers.TryGetValue(r.Id, out var income))
                {
                    Sender.Tell(income);
                    _pendingTransfers.Remove(r.Id);
                }
                else
                {
                    _awaiters[r.Id] = new AwaitRequestInternal(Sender);
                    if (Timeout.InfiniteTimeSpan != r.Timeout)
                    {
                        Timers.StartSingleTimer(r.Id, new DeleteAwaiter(r.Id), r.Timeout);
                    }
                }
            }));

            Flow <DeleteAwaiter>(b => b.Action(d => _awaiters.Remove(d.Id)));

            subscribe.MakeReceive();
        }
        public AppManagerActor(IAppRegistry appRegistry, IInstaller installer, InstallChecker checker)
        {
            _appRegistry = appRegistry;
            var ability = new SubscribeAbility(this);

            installer.Actor
            .Tell(new EventSubscribe(typeof(InstallerationCompled)));

            Receive <UpdateTitle>(_ => Console.Title = "Application Host");

            Receive <InstallerationCompled>(ic =>
            {
                if (checker.IsInstallationStart)
                {
                    Context.System.Terminate();
                }

                void PipeToSelf(string name)
                {
                    appRegistry.Actor
                    .Ask <InstalledAppRespond>(new InstalledAppQuery(name), TimeSpan.FromSeconds(10))
                    .PipeTo(Self, success: ar => new StartApp(ar.App));
                }

                if (ic.InstallAction != InstallationAction.Install || !ic.Succesfull)
                {
                    return;
                }

                // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
                switch (ic.Type)
                {
                case AppType.Cluster:
                    Cluster.Get(Context.System).RegisterOnMemberUp(() => PipeToSelf(ic.Name));
                    break;

                case AppType.StartUp:
                    PipeToSelf(ic.Name);
                    break;
                }
            });

            Flow <StopApps>(b => b.Action(() => Context.GetChildren().Foreach(r => r.Tell(new InternalStopApp()))));

            Flow <StopApp>(b =>
                           b.Action(s =>
            {
                var child = Context.Child(s.Name);
                if (child.IsNobody())
                {
                    Sender.Tell(new StopResponse(s.Name, false));
                }
                else
                {
                    child.Forward(new InternalStopApp());
                }
            }));

            Flow <StopResponse>(b => b.Action(r => ability.Send(r)));

            Flow <StartApps>(b =>
            {
                b.Action(sa =>
                         appRegistry.Actor
                         .Ask <AllAppsResponse>(new AllAppsQuery())
                         .PipeTo(Self, Sender, r => new InternalFilterApps(sa.AppType, r.Apps)))
                .Then <InternalFilterApps>(b1 => b1.Action(fa => fa.Names.Foreach(s => Self.Tell(new InternalFilterApp(fa.AppType, s)))))
                .Then <InternalFilterApp>(
                    b1 => b1.Action(fa =>
                                    appRegistry.Actor
                                    .Ask <InstalledAppRespond>(new InstalledAppQuery(fa.Name))
                                    .ContinueWith(t =>
                {
                    if (!t.IsCompletedSuccessfully && t.Result.Fault && t.Result.App.IsEmpty())
                    {
                        return;
                    }

                    var data = t.Result.App;
                    if (data.AppType != fa.AppType)
                    {
                        return;
                    }

                    Self.Tell(new StartApp(data));
                })))
                .Then <StartApp>(b1 => b1.Action(sa =>
                {
                    if (sa.App.IsEmpty())
                    {
                        return;
                    }
                    if (!Context.Child(sa.App.Name).IsNobody())
                    {
                        return;
                    }

                    Context.ActorOf(Props.Create <AppProcessActor>(sa.App)).Tell(new InternalStartApp());
                }));
            });

            Receive <Status.Failure>(f => Log.Error(f.Cause, "Error while processing message"));

            #region SharedApi

            Receive <StopAllApps>(_ =>
            {
                Task.WhenAll(Context.GetChildren()
                             .Select(ar => ar.Ask <StopResponse>(new InternalStopApp(), TimeSpan.FromMinutes(0.7))).ToArray())
                .ContinueWith(t => new OperationResponse(t.IsCompletedSuccessfully && t.Result.All(s => !s.Error)))
                .PipeTo(Sender, failure: e =>
                {
                    Log.Warning(e, "Error on Shared Api Stop All Apps");
                    return(new OperationResponse(false));
                });
            });

            Flow <StartAllApps>(b => b.Func(_ =>
            {
                Self.Tell(new StartApps(AppType.Cluster));
                return(new OperationResponse(true));
            }).ToSender());

            Receive <QueryAppStaus>(s =>
            {
                var childs = Context.GetChildren().ToArray();

                Task.Run(async() =>
                {
                    List <AppProcessActor.GetNameResponse> names = new List <AppProcessActor.GetNameResponse>();

                    foreach (var actorRef in childs)
                    {
                        try
                        {
                            names.Add(await actorRef.Ask <AppProcessActor.GetNameResponse>(new AppProcessActor.GetName(), TimeSpan.FromSeconds(5)));
                        }
                        catch (Exception e)
                        {
                            Log.Error(e, "Error on Recive Prcess Apps Name");
                        }
                    }

                    return(names.ToArray());
                }).PipeTo(Sender,
                          success: arr => new AppStatusResponse(s.OperationId, arr.ToImmutableDictionary(g => g.Name, g => g.Running)),
                          failure: e =>
                {
                    Log.Error(e, "Error getting Status");
                    return(new AppStatusResponse(s.OperationId));
                });
            });

            Receive <StopHostApp>(sha =>
            {
                var pm = Context.Child(sha.AppName);
                if (pm.IsNobody())
                {
                    Context.Sender.Tell(new OperationResponse(false));
                }
                pm.Ask <StopResponse>(new InternalStopApp(), TimeSpan.FromMinutes(1))
                .PipeTo(Sender,
                        success: () => new OperationResponse(true),
                        failure: e =>
                {
                    Log.Warning(e, "Error on Shared Api Stop");
                    return(new OperationResponse(false));
                });
            });

            Receive <StartHostApp>
                (sha =>
            {
                if (string.IsNullOrWhiteSpace(sha.AppName))
                {
                    Sender.Tell(new OperationResponse(false));
                }
                var pm = Context.Child(sha.AppName);
                if (pm.IsNobody())
                {
                    var self = Self;
                    appRegistry.Actor.Ask <InstalledAppRespond>(new InstalledAppQuery(sha.AppName), TimeSpan.FromMinutes(1))
                    .ContinueWith(t =>
                    {
                        if (t.Result.Fault)
                        {
                            return(new OperationResponse(false));
                        }

                        self.Tell(new StartApp(t.Result.App));
                        return(new OperationResponse(true));
                    })
                    .PipeTo(Sender,
                            failure: e =>
                    {
                        Log.Warning(e, "Error on Shared Api Stop");
                        return(new OperationResponse(false));
                    });
                }
                else
                {
                    pm.Tell(new InternalStartApp());
                    Sender.Tell(new OperationResponse(true));
                }
            });

            #endregion

            ability.MakeReceive();
        }