private async ValueTask <IAppConnection> ResolveTargetConnectionAsync(
            IProvidedMethodReference methodReference,
            IAppConnection source,
            IContextLinkageOptions contextLinkageOptions)
        {
            var method     = _registryService.GetProvidedMethod(methodReference);
            var launchMode = GetLaunchMode(method);
            var appId      = methodReference.ProvidedService.ApplicationId;

            if (methodReference.ProvidedService.ConnectionId.HasValue)
            {
                var connectionId = methodReference.ProvidedService.ConnectionId.Value;
                if (!_appLifecycleManager.TryGetOnlineConnection(connectionId, out var connection))
                {
                    throw new InvalidOperationException($"The requested app {appId} connection {connectionId} is not online");
                }
                return(connection);
            }
            Task <ResolvedConnection> resolveTask;

            lock (_resolveConnectionSync)
            {
                if (launchMode != LaunchMode.MultiInstance)
                {
                    var onlineConnections = _appLifecycleManager
                                            .GetOnlineConnections()
                                            .Where(x => x.Info.ApplicationId.Equals(appId) &&
                                                   !x.Id.Equals(source.Id)).ToArray();

                    if (_contextLinkageManager.IsContextShouldBeConsidered(contextLinkageOptions, source))
                    {
                        onlineConnections = _contextLinkageManager
                                            .GetAppsInContexts(contextLinkageOptions, source, true)
                                            .Join(onlineConnections, x => x.ConnectionId.Value, y => y.Id, (x, y) => y)
                                            .ToArray();
                    }

                    if (onlineConnections.Any())
                    {
                        return(onlineConnections.First());
                    }
                }

                if (launchMode == LaunchMode.None || !_appLifecycleManager.CanBeLaunched(appId))
                {
                    throw new InvalidOperationException(
                              $"The requested app {appId} is not online and cannot be launched");
                }

                var resolveMode = ConvertToResolveMode(launchMode);

                resolveTask = _appLifecycleManager.LaunchAndConnectAsync(appId, resolveMode, source.Info);
            }
            var resolvedConnection = await resolveTask.ConfigureAwait(false);

            return(resolvedConnection.AppConnection);
        }
Ejemplo n.º 2
0
        public async Task <ResolveAppResponse> ResolveApp(ResolveAppRequest request, MethodCallContext context)
        {
            Log.Info("Resolving app by request {{{0}}} from {{{1}}}", request, context);
            var referrerConnectionInfo = new AppConnectionDescriptor(
                context.ConsumerConnectionId,
                context.ConsumerApplicationId,
                context.ConsumerApplicationInstanceId,
                context.ConsumerTransportType);
            var resolveMode = Convert(request.AppResolveMode);

            if (resolveMode == ResolveMode.SingleInstance)
            {
                var connection = _appLifecycleManager.GetOnlineConnections().FirstOrDefault(c => c.Info.ApplicationId.Equals(request.AppId));
                Log.Debug("Resolved connection for app {0} with mode {1} to online instance {{{2}}}", connection, resolveMode, connection);
                if (connection != null)
                {
                    return(new ResolveAppResponse
                    {
                        AppInstanceId = connection.Info.ApplicationInstanceId.ToProto(),
                        AppConnectionId = connection.Info.ConnectionId.ToProto(),
                        IsNewInstanceLaunched = false,
                    });
                }
            }

            var resolvedConnection = await _appLifecycleManager.LaunchAndConnectAsync(
                request.AppId, resolveMode, referrerConnectionInfo).ConfigureAwait(false);

            var info = resolvedConnection.AppConnection.Info;

            Log.Info("App connection {{{0}}} resolved by request from {{{1}}}", resolvedConnection, context);
            var response = new ResolveAppResponse
            {
                AppConnectionId       = info.ConnectionId.ToProto(),
                AppInstanceId         = info.ApplicationInstanceId.ToProto(),
                IsNewInstanceLaunched = resolvedConnection.IsNewInstance
            };

            return(response);
        }
Ejemplo n.º 3
0
        private async ValueTask <IAppConnection> ResolveTargetConnectionAsync(
            IProvidedMethodReference methodReference,
            IAppConnection source,
            ITransportChannel sourceChannel,
            IContextLinkageOptions contextLinkageOptions)
        {
            if (methodReference.ProvidedService.ConnectionId.HasValue)
            {
                var connectionId = methodReference.ProvidedService.ConnectionId.Value;
                if (!_appLifecycleManager.TryGetOnlineConnection(connectionId, out var connection))
                {
                    throw new InvalidOperationException($"The requested connection {connectionId} is not online");
                }
                return(connection);
            }

            var appId = methodReference.ProvidedService.ApplicationId;

            if (methodReference.ProvidedService.ApplicationInstanceId.HasValue)
            {
                var appInstanceId = methodReference.ProvidedService.ApplicationInstanceId.Value;
                if (appId.HasValue && _appLifecycleManager.TryGetConnectionInProgress(appInstanceId, appId.Value, out var connectionInProgress))
                {
                    return(await connectionInProgress);
                }
                var connections = _appLifecycleManager.GetAppInstanceConnections(appInstanceId).ToList();
                if (connections.Count == 0)
                {
                    throw new InvalidOperationException($"App instance {appInstanceId} is doesn't have online connections");
                }

                if (appId.HasValue)
                {
                    var connection = connections.FirstOrDefault(c => c.Info.ApplicationId.Equals(appId.Value));
                    if (connection == null)
                    {
                        throw new InvalidOperationException($"App instance {appInstanceId} is doesn't have connection with {appId.Value} application id");
                    }
                    return(connection);
                }

                if (connections.Count == 1)
                {
                    return(connections.Single());
                }

                throw new InvalidOperationException($"App instance {appInstanceId} has several connections, you need to specify ApplicationId to make call to specific connection");
            }

            if (!appId.HasValue)
            {
                throw new InvalidOperationException($"AppId is required to resolve target connection for {methodReference} provided method reference");
            }
            var appIdValue = appId.Value;

            var method     = _registryService.GetProvidedMethod(methodReference);
            var launchMode = GetLaunchMode(method);

            Task <ResolvedConnection> resolveTask;

            lock (_resolveConnectionSync)
            {
                if (launchMode != LaunchMode.MultiInstance)
                {
                    var onlineConnections = _appLifecycleManager
                                            .GetOnlineConnections()
                                            .Where(x => x.Info.ApplicationId.Equals(appIdValue) &&
                                                   !x.Id.Equals(source.Id)).ToArray();

                    if (_contextLinkageManager.IsContextShouldBeConsidered(contextLinkageOptions, source))
                    {
                        onlineConnections = _contextLinkageManager
                                            .GetAppsInContexts(contextLinkageOptions, source, true)
                                            .Join(onlineConnections, x => x.ConnectionId.Value, y => y.Id, (x, y) => y)
                                            .ToArray();
                    }

                    if (onlineConnections.Any())
                    {
                        return(onlineConnections.First());
                    }
                }

                if (launchMode == LaunchMode.None || !_appLifecycleManager.CanBeLaunched(appIdValue))
                {
                    throw new InvalidOperationException(
                              $"The requested app {appIdValue} is not online and cannot be launched");
                }

                var resolveMode = ConvertToResolveMode(launchMode);

                resolveTask = _appLifecycleManager.LaunchAndConnectAsync(appIdValue, resolveMode, source.Info);
            }
            var resolvedConnection = await resolveTask.ConfigureAwait(false);

            return(resolvedConnection.AppConnection);
        }