Beispiel #1
0
        /// <summary>
        /// Remove unused snapshots.
        /// A snapshot is considered unused if it has not been accessed for at least
        /// (MDHeartbeatPeriod + MDOldSnapshotHeartbeatMargin) minutes.
        /// However, the last MDMinNumberOfSnapshotsToKeep snapshots will be kept regardless
        /// of the access time.
        /// </summary>
        public void Purge(ILogger logger)
        {
            var allSnapshotPaths = _storage.GetInstalledAndInstallingSnapshots();

            var threshold = DateTime.UtcNow - _heartbeatPeriod - _oldHeartbeatAgeMargin;

            var pathSortedByAccessTime = allSnapshotPaths
                                         .Select(path => Tuple.Create(path, GetSnapshotAccessTimeUtc(path, logger)))
                                         .OrderBy(entry => entry.Item2)
                                         .ToArray();

            for (var i = 0; i < pathSortedByAccessTime.Length - _minNumberOfSnapshotsToKeep; ++i)
            {
                var creationTime = pathSortedByAccessTime[i].Item2;
                if (creationTime > threshold)
                {
                    break;
                }

                var pathToRemove = pathSortedByAccessTime[i].Item1;

                try
                {
                    var message = string.Format(PowerShellWorkerStrings.RemovingDependenciesFolder, pathToRemove);
                    logger.Log(isUserOnlyLog: false, LogLevel.Trace, message);

                    _storage.RemoveSnapshot(pathToRemove);
                }
                catch (IOException e)
                {
                    var message = string.Format(PowerShellWorkerStrings.FailedToRemoveDependenciesFolder, pathToRemove, e.Message);
                    logger.Log(isUserOnlyLog: false, LogLevel.Warning, message, e);
                }
            }
        }
Beispiel #2
0
        private bool IsAnyInstallationStartedRecently()
        {
            var threshold = DateTime.UtcNow - MinBackgroundUpgradePeriod;

            return(_storage
                   .GetInstalledAndInstallingSnapshots()
                   .Select(path => _storage.GetSnapshotCreationTimeUtc(path))
                   .Any(creationTime => creationTime > threshold));
        }