From 5c9f70c3752bd7297cb85bdc7ce748363a16ad8b Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Wed, 30 Apr 2025 09:29:13 +0200 Subject: Cleanup Tasks and Validators --- .../ScheduledTasks/Tasks/DeleteCacheFileTask.cs | 203 ++++++++++----------- 1 file changed, 101 insertions(+), 102 deletions(-) (limited to 'Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs') diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs index ff295d9b7..0e77f0102 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs @@ -11,134 +11,133 @@ using MediaBrowser.Model.IO; using MediaBrowser.Model.Tasks; using Microsoft.Extensions.Logging; -namespace Emby.Server.Implementations.ScheduledTasks.Tasks +namespace Emby.Server.Implementations.ScheduledTasks.Tasks; + +/// +/// Deletes old cache files. +/// +public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask { /// - /// Deletes old cache files. + /// Gets or sets the application paths. + /// + /// The application paths. + private readonly IApplicationPaths _applicationPaths; + private readonly ILogger _logger; + private readonly IFileSystem _fileSystem; + private readonly ILocalizationManager _localization; + + /// + /// Initializes a new instance of the class. /// - public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + public DeleteCacheFileTask( + IApplicationPaths appPaths, + ILogger logger, + IFileSystem fileSystem, + ILocalizationManager localization) { - /// - /// Gets or sets the application paths. - /// - /// The application paths. - private readonly IApplicationPaths _applicationPaths; - private readonly ILogger _logger; - private readonly IFileSystem _fileSystem; - private readonly ILocalizationManager _localization; - - /// - /// Initializes a new instance of the class. - /// - /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - public DeleteCacheFileTask( - IApplicationPaths appPaths, - ILogger logger, - IFileSystem fileSystem, - ILocalizationManager localization) - { - _applicationPaths = appPaths; - _logger = logger; - _fileSystem = fileSystem; - _localization = localization; - } + _applicationPaths = appPaths; + _logger = logger; + _fileSystem = fileSystem; + _localization = localization; + } - /// - public string Name => _localization.GetLocalizedString("TaskCleanCache"); + /// + public string Name => _localization.GetLocalizedString("TaskCleanCache"); - /// - public string Description => _localization.GetLocalizedString("TaskCleanCacheDescription"); + /// + public string Description => _localization.GetLocalizedString("TaskCleanCacheDescription"); - /// - public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); + /// + public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); - /// - public string Key => "DeleteCacheFiles"; + /// + public string Key => "DeleteCacheFiles"; - /// - public bool IsHidden => false; + /// + public bool IsHidden => false; - /// - public bool IsEnabled => true; + /// + public bool IsEnabled => true; - /// - public bool IsLogged => true; + /// + public bool IsLogged => true; - /// - public IEnumerable GetDefaultTriggers() + /// + public IEnumerable GetDefaultTriggers() + { + yield return new TaskTriggerInfo { - return - [ - // Every so often - new TaskTriggerInfo { Type = TaskTriggerInfoType.IntervalTrigger, IntervalTicks = TimeSpan.FromHours(24).Ticks } - ]; - } + Type = TaskTriggerInfoType.IntervalTrigger, + IntervalTicks = TimeSpan.FromHours(24).Ticks + }; + } - /// - public Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) + /// + public Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) + { + var minDateModified = DateTime.UtcNow.AddDays(-30); + + try + { + DeleteCacheFilesFromDirectory(_applicationPaths.CachePath, minDateModified, progress, cancellationToken); + } + catch (DirectoryNotFoundException) { - var minDateModified = DateTime.UtcNow.AddDays(-30); - - try - { - DeleteCacheFilesFromDirectory(_applicationPaths.CachePath, minDateModified, progress, cancellationToken); - } - catch (DirectoryNotFoundException) - { - // No biggie here. Nothing to delete - } - - progress.Report(90); - - minDateModified = DateTime.UtcNow.AddDays(-1); - - try - { - DeleteCacheFilesFromDirectory(_applicationPaths.TempDirectory, minDateModified, progress, cancellationToken); - } - catch (DirectoryNotFoundException) - { - // No biggie here. Nothing to delete - } - - return Task.CompletedTask; + // No biggie here. Nothing to delete } - /// - /// Deletes the cache files from directory with a last write time less than a given date. - /// - /// The directory. - /// The min date modified. - /// The progress. - /// The task cancellation token. - private void DeleteCacheFilesFromDirectory(string directory, DateTime minDateModified, IProgress progress, CancellationToken cancellationToken) + progress.Report(90); + + minDateModified = DateTime.UtcNow.AddDays(-1); + + try { - var filesToDelete = _fileSystem.GetFiles(directory, true) - .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) - .ToList(); + DeleteCacheFilesFromDirectory(_applicationPaths.TempDirectory, minDateModified, progress, cancellationToken); + } + catch (DirectoryNotFoundException) + { + // No biggie here. Nothing to delete + } - var index = 0; + return Task.CompletedTask; + } - foreach (var file in filesToDelete) - { - double percent = index; - percent /= filesToDelete.Count; + /// + /// Deletes the cache files from directory with a last write time less than a given date. + /// + /// The directory. + /// The min date modified. + /// The progress. + /// The task cancellation token. + private void DeleteCacheFilesFromDirectory(string directory, DateTime minDateModified, IProgress progress, CancellationToken cancellationToken) + { + var filesToDelete = _fileSystem.GetFiles(directory, true) + .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) + .ToList(); - progress.Report(100 * percent); + var index = 0; - cancellationToken.ThrowIfCancellationRequested(); + foreach (var file in filesToDelete) + { + double percent = index; + percent /= filesToDelete.Count; - FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger); + progress.Report(100 * percent); - index++; - } + cancellationToken.ThrowIfCancellationRequested(); - FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger); + FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger); - progress.Report(100); + index++; } + + FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger); + + progress.Report(100); } } -- cgit v1.2.3