Пример #1
0
        /// <summary>
        /// Finds out expired resources in the <paramref name="resourceStorage"/> and deletes them.
        /// </summary>
        /// <typeparam name="T"> The type of an identifier of a resource. </typeparam>
        /// <param name="resourceStorage">
        /// The storage where to perform cleanup in.
        /// </param>
        /// <param name="expirationPolicy">
        /// The resource expiration policy.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="resourceStorage"/> is <see langword="null"/> or
        /// <paramref name="expirationPolicy"/> is <see langword="null"/>.
        /// </exception>
        public async Task ExecuteStorageCleanup <T>(
            [NotNull] IResourceStorage <T> resourceStorage,
            [NotNull] IResourceExpirationPolicy expirationPolicy)
        {
            AssertArg.NotNull(resourceStorage, nameof(resourceStorage));
            AssertArg.NotNull(expirationPolicy, nameof(expirationPolicy));

            _log?.Debug($"Calling {resourceStorage.GetType().Name} storage for resource details.");

            var resources = (await resourceStorage.GetResourceDetails()).ToArray();

            _log?.Debug($"Fetched details about {resources.Length} resources.");

            var resourceIdsToDelete = expirationPolicy
                                      .FindExpiredResources(resources)
                                      .ToArray();

            if (!resourceIdsToDelete.Any())
            {
                _log?.Info("No expired resources were found thus no resource is deleted.");

                return;
            }

            _log?.Debug($"Calling {resourceStorage.GetType().Name} storage for expired resources deletion.");

            await resourceStorage.DeleteResources(resourceIdsToDelete);

            _log?.Debug($"Deleted {resourceIdsToDelete.Length} expired resources.");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Log4NetWrapper" /> class.
        /// </summary>
        /// <param name="logImpl">
        /// The implementation of the <see cref="T:log4net.ILog"/> interface.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="logImpl"/> is <see langword="null"/>.
        /// </exception>
        public Log4NetWrapper(
            [NotNull] log4net.ILog logImpl)
        {
            AssertArg.NotNull(logImpl, nameof(logImpl));

            _logImpl = logImpl;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectoryFileStorage" /> class.
        /// </summary>
        /// <param name="systemClock">
        /// The source of system clock.
        /// </param>
        /// <param name="settings">
        /// The settings of the storage.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="systemClock"/> or
        /// <paramref name="settings"/> is <see langword="null"/>.
        /// </exception>
        public DirectoryFileStorage(
            [NotNull] ISystemClock systemClock,
            [NotNull] DirectoryFileStorageSettings settings)
        {
            AssertArg.NotNull(systemClock, nameof(systemClock));
            AssertArg.NotNull(settings, nameof(settings));

            _systemClock = systemClock;
            _settings    = settings;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectoryFileStorage" /> class.
        /// </summary>
        /// <param name="systemClock">
        /// The source of system clock.
        /// </param>
        /// <param name="settings">
        /// The settings of the storage.
        /// </param>
        /// <param name="log">
        /// A log where to write log messages into.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="systemClock"/> or
        /// <paramref name="settings"/> or
        /// <paramref name="log"/> is <see langword="null"/>.
        /// </exception>
        public DirectoryFileStorage(
            [NotNull] ISystemClock systemClock,
            [NotNull] DirectoryFileStorageSettings settings,
            [NotNull] ILog log)
            : this(systemClock, settings)
        {
            AssertArg.NotNull(log, nameof(log));

            _log = log;
        }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AppConfig"/> class.
        /// </summary>
        /// <param name="cleanupDirectoryPath">
        /// The path to the directory to cleanup.
        /// </param>
        /// <param name="retentionRules">
        /// The sequence of retention rules.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="cleanupDirectoryPath"/> is <see langword="null"/> or empty or whitespace or
        /// <paramref name="retentionRules"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="retentionRules"/> contains a <see langword="null"/> item.
        /// </exception>
        public AppConfig(
            [NotNull] string cleanupDirectoryPath,
            [NotNull, ItemNotNull] IReadOnlyCollection <RetentionRule> retentionRules)
        {
            AssertArg.NotNullOrWhiteSpace(cleanupDirectoryPath, nameof(cleanupDirectoryPath));
            AssertArg.NotNull(retentionRules, nameof(retentionRules));
            AssertArg.NoNullItems(retentionRules, nameof(retentionRules));

            CleanupDirectoryPath = cleanupDirectoryPath;
            RetentionRules       = retentionRules.ToReadOnlyList();
        }
        /// <summary>
        /// Deletes files that are specified in the <paramref name="resourceIds"/> argument.
        /// </summary>
        /// <param name="resourceIds">
        /// The sequence of full names of files to delete.
        /// </param>
        public Task DeleteResources(IEnumerable <string> resourceIds)
        {
            AssertArg.NotNull(resourceIds, nameof(resourceIds));

            var fileNamesToDelete = resourceIds.ToArray();

            fileNamesToDelete.ForEach(File.Delete);

            _log?.Debug(
                $"The following files were removed:{NewLine}" +
                fileNamesToDelete.Select(fn => $"\t{fn}").JoinThrough(NewLine));

            return(Task.CompletedTask);
        }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="App{T}" /> class.
        /// </summary>
        /// <param name="cleanupExecutor">
        /// The executor of cleanup of the <paramref name="storage"/>.
        /// </param>
        /// <param name="storage">
        /// The storage to be cleanup up.
        /// </param>
        /// <param name="expirationPolicy">
        /// The resource expiration policy.
        /// </param>
        /// <param name="log">
        /// The log where to write messages to.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="cleanupExecutor"/> is <see langword="null"/> or
        /// <paramref name="storage"/> is <see langword="null"/> or
        /// <paramref name="expirationPolicy"/> is <see langword="null"/> or
        /// <paramref name="log"/> is <see langword="null"/>.
        /// </exception>
        public App(
            [NotNull] CleanupExecutor cleanupExecutor,
            [NotNull] IResourceStorage <T> storage,
            [NotNull] IResourceExpirationPolicy expirationPolicy,
            [NotNull] ILog log)
        {
            AssertArg.NotNull(cleanupExecutor, nameof(cleanupExecutor));
            AssertArg.NotNull(storage, nameof(storage));
            AssertArg.NotNull(expirationPolicy, nameof(expirationPolicy));
            AssertArg.NotNull(log, nameof(log));

            _cleanupExecutor  = cleanupExecutor;
            _storage          = storage;
            _expirationPolicy = expirationPolicy;
            _log = log;
        }
        /// <summary>
        /// Finds expired resources among the <paramref name="resources"/> specified.
        /// </summary>
        /// <typeparam name="T"> The type of an identifier of a resource. </typeparam>
        /// <param name="resources">The sequence resources.</param>
        /// <returns> The sequence of identifiers of expired resources. </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="resources"/> is <see langword="null"/>.
        /// </exception>
        public IEnumerable <T> FindExpiredResources <T>(IReadOnlyCollection <IResource <T> > resources)
        {
            AssertArg.NotNull(resources, nameof(resources));
            AssertArg.NoNullItems(resources, nameof(resources));

            var orderedResources       = resources.OrderBy(i => i.Age).ToArray();
            var examineScopeStartIndex = 0;
            var examineScopeLength     = orderedResources.Length;

            foreach (var rule in _orderedRules)
            {
                if (examineScopeLength <= 0)
                {
                    break;
                }

                var examineScopeResources = orderedResources
                                            .Skip(examineScopeStartIndex)
                                            .Take(examineScopeLength)
                                            .ToArray();

                var outOfRuleCount = examineScopeResources
                                     .TakeWhile(i => i.Age <= rule.OlderThan)
                                     .Count();

                var retainCount = examineScopeResources
                                  .Skip(outOfRuleCount)
                                  .Take(rule.AllowedAmount)
                                  .Count();

                examineScopeStartIndex += outOfRuleCount;
                examineScopeLength      = retainCount;
            }

            var totalRetainCount =
                examineScopeStartIndex +
                examineScopeLength;

            return(orderedResources
                   .Skip(totalRetainCount)
                   .Select(i => i.Id));
        }
Пример #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LoggingModule"/> class
        /// and applies logger configuration specified by the <paramref name="config"/> argument.
        /// </summary>
        /// <param name="config">
        /// The configuration options required to configure log4net.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="config"/> is <see langword="null"/>.
        /// </exception>
        public LoggingModule([NotNull] ILogConfiguration config)
        {
            AssertArg.NotNull(config, nameof(config));

            ConfigureLog4Net(config.ConfigFilePath);
        }
Пример #10
0
        public LogConfig(string configFilePath)
        {
            AssertArg.NotNull(configFilePath, nameof(configFilePath));

            ConfigFilePath = configFilePath;
        }
Пример #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AppConfigBuilder"/> class.
        /// </summary>
        /// <param name="log">
        /// A log where to write log messages into.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="log"/> is <see langword="null"/>.
        /// </exception>
        public AppConfigBuilder([NotNull] ILog log) : this()
        {
            AssertArg.NotNull(log, nameof(log));

            _log = log;
        }
Пример #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CleanupExecutor"/> class.
        /// </summary>
        /// <param name="log">
        /// A log where to write log messages into.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="log"/> is <see langword="null"/>.
        /// </exception>
        public CleanupExecutor([NotNull] ILog log) : this()
        {
            AssertArg.NotNull(log, nameof(log));

            _log = log;
        }