< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteLogFileTask
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs
Line coverage
17%
Covered lines: 5
Uncovered lines: 23
Coverable lines: 28
Total lines: 101
Line coverage: 17.8%
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(...)0%620%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using MediaBrowser.Common.Configuration;
 8using MediaBrowser.Model.Globalization;
 9using MediaBrowser.Model.IO;
 10using MediaBrowser.Model.Tasks;
 11
 12namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
 13
 14/// <summary>
 15/// Deletes old log files.
 16/// </summary>
 17public class DeleteLogFileTask : IScheduledTask, IConfigurableScheduledTask
 18{
 19    private readonly IConfigurationManager _configurationManager;
 20    private readonly IFileSystem _fileSystem;
 21    private readonly ILocalizationManager _localization;
 22
 23    /// <summary>
 24    /// Initializes a new instance of the <see cref="DeleteLogFileTask" /> class.
 25    /// </summary>
 26    /// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
 27    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 28    /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
 29    public DeleteLogFileTask(IConfigurationManager configurationManager, IFileSystem fileSystem, ILocalizationManager lo
 30    {
 2131        _configurationManager = configurationManager;
 2132        _fileSystem = fileSystem;
 2133        _localization = localization;
 2134    }
 35
 36    /// <inheritdoc />
 2137    public string Name => _localization.GetLocalizedString("TaskCleanLogs");
 38
 39    /// <inheritdoc />
 040    public string Description => string.Format(
 041        CultureInfo.InvariantCulture,
 042        _localization.GetLocalizedString("TaskCleanLogsDescription"),
 043        _configurationManager.CommonConfiguration.LogFileRetentionDays);
 44
 45    /// <inheritdoc />
 046    public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
 47
 48    /// <inheritdoc />
 049    public string Key => "CleanLogFiles";
 50
 51    /// <inheritdoc />
 052    public bool IsHidden => false;
 53
 54    /// <inheritdoc />
 055    public bool IsEnabled => true;
 56
 57    /// <inheritdoc />
 058    public bool IsLogged => true;
 59
 60    /// <inheritdoc />
 61    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
 62    {
 63        yield return new TaskTriggerInfo
 64        {
 65            Type = TaskTriggerInfoType.IntervalTrigger,
 66            IntervalTicks = TimeSpan.FromHours(24).Ticks
 67        };
 68    }
 69
 70    /// <inheritdoc />
 71    public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
 72    {
 73        // Delete log files more than n days old
 074        var minDateModified = DateTime.UtcNow.AddDays(-_configurationManager.CommonConfiguration.LogFileRetentionDays);
 75
 76        // Only delete files that serilog doesn't manage (anything that doesn't start with 'log_'
 077        var filesToDelete = _fileSystem.GetFiles(_configurationManager.CommonApplicationPaths.LogDirectoryPath, true)
 078            .Where(f => !f.Name.StartsWith("log_", StringComparison.Ordinal)
 079                        && _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
 080            .ToList();
 81
 082        var index = 0;
 83
 084        foreach (var file in filesToDelete)
 85        {
 086            double percent = index / (double)filesToDelete.Count;
 87
 088            progress.Report(100 * percent);
 89
 090            cancellationToken.ThrowIfCancellationRequested();
 91
 092            _fileSystem.DeleteFile(file.FullName);
 93
 094            index++;
 95        }
 96
 097        progress.Report(100);
 98
 099        return Task.CompletedTask;
 100    }
 101}