< 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
28%
Covered lines: 9
Uncovered lines: 23
Coverable lines: 32
Total lines: 101
Line coverage: 28.1%
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%
GetDefaultTriggers()100%11100%
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>
 17    public 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, ILocalizationManage
 30        {
 2231            _configurationManager = configurationManager;
 2232            _fileSystem = fileSystem;
 2233            _localization = localization;
 2234        }
 35
 36        /// <inheritdoc />
 2237        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        {
 2263            return
 2264            [
 2265                new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Tic
 2266            ];
 67        }
 68
 69        /// <inheritdoc />
 70        public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
 71        {
 72            // Delete log files more than n days old
 073            var minDateModified = DateTime.UtcNow.AddDays(-_configurationManager.CommonConfiguration.LogFileRetentionDay
 74
 75            // Only delete files that serilog doesn't manage (anything that doesn't start with 'log_'
 076            var filesToDelete = _fileSystem.GetFiles(_configurationManager.CommonApplicationPaths.LogDirectoryPath, true
 077                .Where(f => !f.Name.StartsWith("log_", StringComparison.Ordinal)
 078                            && _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
 079                .ToList();
 80
 081            var index = 0;
 82
 083            foreach (var file in filesToDelete)
 84            {
 085                double percent = index / (double)filesToDelete.Count;
 86
 087                progress.Report(100 * percent);
 88
 089                cancellationToken.ThrowIfCancellationRequested();
 90
 091                _fileSystem.DeleteFile(file.FullName);
 92
 093                index++;
 94            }
 95
 096            progress.Report(100);
 97
 098            return Task.CompletedTask;
 99        }
 100    }
 101}