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);
                }
            }
        }
Esempio n. 3
0
        public IRunspaceData Get(string userId, string runspaceId)
        {
            _logger.LogInformation($"Get runspace with id: {runspaceId}");
            IRunspaceData result = null;

            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);
                if (runspaceInfo == null && runspaceData != null)
                {
                    _userRunspaces.RemoveData(userId, runspaceId);
                }
                else
                {
                    result = runspaceData;
                }
            } catch (Exception ex) {
                throw new RunspaceProviderException(
                          string.Format(
                              APIGatewayResources.MultiTenantRunspaceProvider_GetFailed,
                              userId,
                              ex.Message),
                          ex);
            }

            return(result);
        }
        public void RemoveRunspaceWhenNoUser()
        {
            // 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 && Assert
            Assert.DoesNotThrow(() => userRunspaces.RemoveData(UserID2, RunspaceID2));

            Assert.IsTrue(userRunspaces.Contains(UserID1));
            Assert.IsTrue(userRunspaces.Contains(UserID1, RunspaceID1));
            Assert.IsTrue(userRunspaces.Contains(UserID1, RunspaceID2));
        }
        public void RemoveRunspace()
        {
            // Arrange
            const string UserID        = "user-id";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            var          userRunspaces = new UserToIdentifiableData <string>();

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

            // Act
            userRunspaces.RemoveData(UserID, RunspaceID1);

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

            Assert.NotNull(listResult);
            Assert.AreEqual(1, listResult.Length);
            Assert.IsFalse(listResult.Contains(RunspaceID1));
            Assert.IsTrue(listResult.Contains(RunspaceID2));
            Assert.IsTrue(userRunspaces.Contains(UserID, RunspaceID2));
            Assert.IsFalse(userRunspaces.Contains(UserID, RunspaceID1));
        }