private void CleanupRunspaces()
        {
            // NB: The below operation could be slow because interacts with
            // every running container to get stats.
            var runspaceIdsToRemove = _runspacesStatsMonitor.EvaluateRunspacesToRemove(IRunspacesStatsMonitor.RunspaceType.Runspace);

            foreach (var runspaceId in runspaceIdsToRemove)
            {
                var userId = _userRunspaces.GetUser(runspaceId);

                // Kill Runspace Container
                Kill(userId, runspaceId);

                // Unregister from stats
                _runspacesStatsMonitor.Unregister(runspaceId);
            }

            // Cleanup Local Data for containers that are not running
            // Get all running containers and if there are such for which
            // local data exists but they are not available, remove local data
            var runningRunspaces = _runspaceProvider.List().Select(a => a.Id);

            // Clean up user to runspace data
            var userIds = _userRunspaces.ListUsers();

            foreach (var userId in userIds ?? Enumerable.Empty <string>())
            {
                var userRunspaces = _userRunspaces.List(userId);
                foreach (var runspaceData in userRunspaces ?? Enumerable.Empty <IRunspaceData>())
                {
                    if (!runningRunspaces.Contains(runspaceData.Id))
                    {
                        _userRunspaces.RemoveData(userId, runspaceData.Id);
                    }
                }
            }

            // Clean up statistics data
            var monitoredRunspaces = _runspacesStatsMonitor.GetRegisteredRunspaces();

            foreach (var runspaceId in monitoredRunspaces ?? Enumerable.Empty <string>())
            {
                if (!runningRunspaces.Contains(runspaceId))
                {
                    _runspacesStatsMonitor.Unregister(runspaceId);
                }
            }
        }
        private void CleanupWebConsoles()
        {
            var webConsoleIdsToRemove = _runspacesStatsMonitor.EvaluateRunspacesToRemove(IRunspacesStatsMonitor.RunspaceType.WebConsole);

            foreach (var webConsoleId in webConsoleIdsToRemove)
            {
                var userId = _userWebConsoles.GetUser(webConsoleId);

                // Kill WebConsole
                KillWebConsole(userId, webConsoleId);

                // Unregister from stats
                _runspacesStatsMonitor.Unregister(webConsoleId);
            }

            // Cleanup Local Data for web consoles that are not running
            // Get all running web consoles and if there are such for which
            // local data exists but they are not available, remove local data
            var runningWebConsoles = _runspaceProvider.ListWebConsole().Select(a => a.Id);

            // Clean up user to web console data
            var userIds = _userWebConsoles.ListUsers();

            foreach (var userId in userIds ?? Enumerable.Empty <string>())
            {
                var userWebConosles = _userWebConsoles.List(userId);
                foreach (var webConsoleData in userWebConosles ?? Enumerable.Empty <IWebConsoleData>())
                {
                    if (!runningWebConsoles.Contains(webConsoleData.Id))
                    {
                        _userWebConsoles.RemoveData(userId, webConsoleData.Id);
                    }
                }
            }

            // Clean up statistics data
            var monitoredWebConsoles = _runspacesStatsMonitor.GetRegisteredWebConsoles();

            foreach (var webConsoleId in monitoredWebConsoles ?? Enumerable.Empty <string>())
            {
                if (!runningWebConsoles.Contains(webConsoleId))
                {
                    _runspacesStatsMonitor.Unregister(webConsoleId);
                }
            }
        }
Beispiel #3
0
        public void Kill(string userId, string runspaceId)
        {
            _logger.LogInformation($"Kill runspace {runspaceId}");
            try {
                Sessions.Instance.EnsureValidUser(userId);

                if (!_userRunspaces.Contains(userId))
                {
                    throw new RunspaceProviderException(
                              string.Format(
                                  APIGatewayResources.MultiTenantRunspaceProvider_UserHasNoRunspaces,
                                  userId));
                }

                if (!_userRunspaces.Contains(userId, runspaceId))
                {
                    throw new RunspaceProviderException(
                              string.Format(
                                  APIGatewayResources.MultiTenantRunspaceProvider_UserHasNoRunspaceWithId,
                                  userId,
                                  runspaceId));
                }

                _userRunspaces.RemoveData(userId, runspaceId);
                _runspaceProvider.Kill(runspaceId);
                _runspacesStatsMonitor.Unregister(runspaceId);
                if (_userRunspaces.List(userId) == null)
                {
                    _userRunspaces.RemoveUser(userId);
                }
            } catch (Exception ex) {
                throw new RunspaceProviderException(
                          string.Format(
                              APIGatewayResources.MultiTenantRunspaceProvider_KillFailed,
                              userId,
                              ex.Message),
                          ex);
            }
        }
        public void ListWhenNoUser()
        {
            // Arrange
            const string UserID1       = "user-id1";
            const string UserID2       = "user-id2";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID1, RunspaceID1, RunspaceID1);
            userRunspaces.Add(UserID1, RunspaceID2, RunspaceID2);

            // Act
            var listResult = userRunspaces.List(UserID2);

            // Assert
            Assert.IsNull(listResult);
        }
        public void List()
        {
            // Arrange
            const string UserID        = "user-id";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceData1 = "runspace-data1";
            const string RunspaceID2   = "runspace-id2";
            const string RunspaceData2 = "runspace-data2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID, RunspaceID1, RunspaceData1);
            userRunspaces.Add(UserID, RunspaceID2, RunspaceData2);

            // Act
            var listResult = userRunspaces.List(UserID);

            // Assert
            Assert.NotNull(listResult);
            Assert.AreEqual(2, listResult.Length);
            Assert.IsTrue(listResult.Contains(RunspaceData1));
            Assert.IsTrue(listResult.Contains(RunspaceData2));
        }
Beispiel #6
0
        public void Kill(string userId, string runspaceId)
        {
            _logger.LogInformation($"Kill runspace {runspaceId}");
            try {
                Sessions.Instance.EnsureValidUser(userId);

                if (!_userRunspaces.Contains(userId))
                {
                    throw new RunspaceProviderException(
                              string.Format(
                                  APIGatewayResources.MultiTenantRunspaceProvider_UserHasNoRunspaces,
                                  userId));
                }

                if (!_userRunspaces.Contains(userId, runspaceId))
                {
                    throw new RunspaceProviderException(
                              string.Format(
                                  APIGatewayResources.MultiTenantRunspaceProvider_UserHasNoRunspaceWithId,
                                  userId,
                                  runspaceId));
                }

                var runspaceInfo = _runspaceProvider.Get(runspaceId);
                var runspaceData = _userRunspaces.GetData(userId, runspaceId);
                _userRunspaces.RemoveData(userId, runspaceId);

                if (runspaceData.RunVcConnectionScript)
                {
                    // Run Disconnect script to close VC server sessions first and then kill the runspace
                    Task.Run(() => {
                        _logger.LogDebug("RunspaceProvider -> Run disconnect VI server ");
                        try {
                            var scriptExecutionRequest = new DataTypes.ScriptExecution {
                                OutputObjectsFormat = OutputObjectsFormat.Json,
                                Name   = "disconnectallservers",
                                Script = PCLIScriptsReader.DisconnectAllServers
                            };

                            _logger.LogDebug($"Start Disconnect All Servers script");
                            var scriptResult = ScriptExecutionMediatorSingleton.
                                               Instance.
                                               ScriptExecutionMediator.
                                               StartScriptExecution(userId, runspaceInfo, scriptExecutionRequest).Result;

                            _logger.LogDebug($"Wait Disconnect All Servers script to complete");
                            while (scriptResult.State == ScriptState.Running)
                            {
                                var intermediateResult = ScriptExecutionMediatorSingleton.Instance.ScriptExecutionMediator.GetScriptExecution(
                                    userId,
                                    scriptResult.Id);
                                if (intermediateResult != null)
                                {
                                    scriptResult = intermediateResult;
                                }
                                Thread.Sleep(100);
                            }
                        } catch (RunspaceEndpointException runspaceEndointException) {
                            _logger.LogError(runspaceEndointException, "Runspace endpoint exception while waiting connect VC script");
                        } catch (Exception exc) {
                            _logger.LogError(exc, "Wait Disconnect All Servers script failed");
                        }

                        _runspaceProvider.Kill(runspaceId);
                    });
                }
                else
                {
                    _runspaceProvider.Kill(runspaceId);
                }

                _runspacesStatsMonitor.Unregister(runspaceId);
                if (_userRunspaces.List(userId) == null)
                {
                    _userRunspaces.RemoveUser(userId);
                }
            } catch (Exception ex) {
                throw new RunspaceProviderException(
                          string.Format(
                              APIGatewayResources.MultiTenantRunspaceProvider_KillFailed,
                              userId,
                              ex.Message),
                          ex);
            }
        }