/// <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="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; }