Beispiel #1
0
        public void Cleanup()
        {
            _logger.LogInformation($"Cleanup");
            // NB: The below operation could be slow because interacts with
            // every running container to get stats.
            var runspaceIdsToRemove = _runspacesStatsMonitor.EvaluateRunspacesToRemove();

            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);
                }
            }
        }