| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using MediaBrowser.Common.Configuration; |
| | | 8 | | using MediaBrowser.Model.Globalization; |
| | | 9 | | using MediaBrowser.Model.IO; |
| | | 10 | | using MediaBrowser.Model.Tasks; |
| | | 11 | | |
| | | 12 | | namespace 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, ILocalizationManager lo |
| | | 30 | | { |
| | 21 | 31 | | _configurationManager = configurationManager; |
| | 21 | 32 | | _fileSystem = fileSystem; |
| | 21 | 33 | | _localization = localization; |
| | 21 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | 21 | 37 | | public string Name => _localization.GetLocalizedString("TaskCleanLogs"); |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 0 | 40 | | public string Description => string.Format( |
| | 0 | 41 | | CultureInfo.InvariantCulture, |
| | 0 | 42 | | _localization.GetLocalizedString("TaskCleanLogsDescription"), |
| | 0 | 43 | | _configurationManager.CommonConfiguration.LogFileRetentionDays); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 0 | 46 | | public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 0 | 49 | | public string Key => "CleanLogFiles"; |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 0 | 52 | | public bool IsHidden => false; |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 0 | 55 | | public bool IsEnabled => true; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 0 | 58 | | 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 |
| | 0 | 74 | | 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_' |
| | 0 | 77 | | var filesToDelete = _fileSystem.GetFiles(_configurationManager.CommonApplicationPaths.LogDirectoryPath, true) |
| | 0 | 78 | | .Where(f => !f.Name.StartsWith("log_", StringComparison.Ordinal) |
| | 0 | 79 | | && _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) |
| | 0 | 80 | | .ToList(); |
| | | 81 | | |
| | 0 | 82 | | var index = 0; |
| | | 83 | | |
| | 0 | 84 | | foreach (var file in filesToDelete) |
| | | 85 | | { |
| | 0 | 86 | | double percent = index / (double)filesToDelete.Count; |
| | | 87 | | |
| | 0 | 88 | | progress.Report(100 * percent); |
| | | 89 | | |
| | 0 | 90 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 91 | | |
| | 0 | 92 | | _fileSystem.DeleteFile(file.FullName); |
| | | 93 | | |
| | 0 | 94 | | index++; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | progress.Report(100); |
| | | 98 | | |
| | 0 | 99 | | return Task.CompletedTask; |
| | | 100 | | } |
| | | 101 | | } |