< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteCacheFileTask
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs
Line coverage
15%
Covered lines: 6
Uncovered lines: 32
Coverable lines: 38
Total lines: 143
Line coverage: 15.7%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%11100%
get_Description()100%210%
get_Category()100%210%
get_Key()100%210%
get_IsHidden()100%210%
get_IsEnabled()100%210%
get_IsLogged()100%210%
ExecuteAsync(...)100%210%
DeleteCacheFilesFromDirectory(...)0%620%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using MediaBrowser.Common.Configuration;
 8using MediaBrowser.Controller.IO;
 9using MediaBrowser.Model.Globalization;
 10using MediaBrowser.Model.IO;
 11using MediaBrowser.Model.Tasks;
 12using Microsoft.Extensions.Logging;
 13
 14namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
 15
 16/// <summary>
 17/// Deletes old cache files.
 18/// </summary>
 19public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask
 20{
 21    /// <summary>
 22    /// Gets or sets the application paths.
 23    /// </summary>
 24    /// <value>The application paths.</value>
 25    private readonly IApplicationPaths _applicationPaths;
 26    private readonly ILogger<DeleteCacheFileTask> _logger;
 27    private readonly IFileSystem _fileSystem;
 28    private readonly ILocalizationManager _localization;
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
 32    /// </summary>
 33    /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
 34    /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
 35    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 36    /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
 37    public DeleteCacheFileTask(
 38        IApplicationPaths appPaths,
 39        ILogger<DeleteCacheFileTask> logger,
 40        IFileSystem fileSystem,
 41        ILocalizationManager localization)
 42    {
 2143        _applicationPaths = appPaths;
 2144        _logger = logger;
 2145        _fileSystem = fileSystem;
 2146        _localization = localization;
 2147    }
 48
 49    /// <inheritdoc />
 2150    public string Name => _localization.GetLocalizedString("TaskCleanCache");
 51
 52    /// <inheritdoc />
 053    public string Description => _localization.GetLocalizedString("TaskCleanCacheDescription");
 54
 55    /// <inheritdoc />
 056    public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
 57
 58    /// <inheritdoc />
 059    public string Key => "DeleteCacheFiles";
 60
 61    /// <inheritdoc />
 062    public bool IsHidden => false;
 63
 64    /// <inheritdoc />
 065    public bool IsEnabled => true;
 66
 67    /// <inheritdoc />
 068    public bool IsLogged => true;
 69
 70    /// <inheritdoc />
 71    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
 72    {
 73        yield return new TaskTriggerInfo
 74        {
 75            Type = TaskTriggerInfoType.IntervalTrigger,
 76            IntervalTicks = TimeSpan.FromHours(24).Ticks
 77        };
 78    }
 79
 80    /// <inheritdoc />
 81    public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
 82    {
 083        var minDateModified = DateTime.UtcNow.AddDays(-30);
 84
 85        try
 86        {
 087            DeleteCacheFilesFromDirectory(_applicationPaths.CachePath, minDateModified, progress, cancellationToken);
 088        }
 089        catch (DirectoryNotFoundException)
 90        {
 91            // No biggie here. Nothing to delete
 092        }
 93
 094        progress.Report(90);
 95
 096        minDateModified = DateTime.UtcNow.AddDays(-1);
 97
 98        try
 99        {
 0100            DeleteCacheFilesFromDirectory(_applicationPaths.TempDirectory, minDateModified, progress, cancellationToken)
 0101        }
 0102        catch (DirectoryNotFoundException)
 103        {
 104            // No biggie here. Nothing to delete
 0105        }
 106
 0107        return Task.CompletedTask;
 108    }
 109
 110    /// <summary>
 111    /// Deletes the cache files from directory with a last write time less than a given date.
 112    /// </summary>
 113    /// <param name="directory">The directory.</param>
 114    /// <param name="minDateModified">The min date modified.</param>
 115    /// <param name="progress">The progress.</param>
 116    /// <param name="cancellationToken">The task cancellation token.</param>
 117    private void DeleteCacheFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, C
 118    {
 0119        var filesToDelete = _fileSystem.GetFiles(directory, true)
 0120            .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
 0121            .ToList();
 122
 0123        var index = 0;
 124
 0125        foreach (var file in filesToDelete)
 126        {
 0127            double percent = index;
 0128            percent /= filesToDelete.Count;
 129
 0130            progress.Report(100 * percent);
 131
 0132            cancellationToken.ThrowIfCancellationRequested();
 133
 0134            FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
 135
 0136            index++;
 137        }
 138
 0139        FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger);
 140
 0141        progress.Report(100);
 0142    }
 143}