Files
jellyfin-jellyfin-1/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs

102 lines
3.4 KiB
C#
Raw Normal View History

using System;
2016-10-29 01:40:15 -04:00
using System.Collections.Generic;
using System.Globalization;
2016-10-29 01:40:15 -04:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.Globalization;
2016-10-29 01:40:15 -04:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Tasks;
2025-04-30 09:29:13 +02:00
namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
/// <summary>
/// Deletes old log files.
/// </summary>
public class DeleteLogFileTask : IScheduledTask, IConfigurableScheduledTask
2016-10-29 01:40:15 -04:00
{
2025-04-30 09:29:13 +02:00
private readonly IConfigurationManager _configurationManager;
private readonly IFileSystem _fileSystem;
private readonly ILocalizationManager _localization;
2016-10-29 01:40:15 -04:00
/// <summary>
2025-04-30 09:29:13 +02:00
/// Initializes a new instance of the <see cref="DeleteLogFileTask" /> class.
2016-10-29 01:40:15 -04:00
/// </summary>
2025-04-30 09:29:13 +02:00
/// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
public DeleteLogFileTask(IConfigurationManager configurationManager, IFileSystem fileSystem, ILocalizationManager localization)
2016-10-29 01:40:15 -04:00
{
2025-04-30 09:29:13 +02:00
_configurationManager = configurationManager;
_fileSystem = fileSystem;
_localization = localization;
}
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskCleanLogs");
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Description => string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("TaskCleanLogsDescription"),
_configurationManager.CommonConfiguration.LogFileRetentionDays);
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Key => "CleanLogFiles";
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public bool IsHidden => false;
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public bool IsEnabled => true;
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public bool IsLogged => true;
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
yield return new TaskTriggerInfo
2016-10-29 01:40:15 -04:00
{
2025-04-30 09:29:13 +02:00
Type = TaskTriggerInfoType.IntervalTrigger,
IntervalTicks = TimeSpan.FromHours(24).Ticks
};
}
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
// Delete log files more than n days old
var minDateModified = DateTime.UtcNow.AddDays(-_configurationManager.CommonConfiguration.LogFileRetentionDays);
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
// Only delete files that serilog doesn't manage (anything that doesn't start with 'log_'
var filesToDelete = _fileSystem.GetFiles(_configurationManager.CommonApplicationPaths.LogDirectoryPath, true)
.Where(f => !f.Name.StartsWith("log_", StringComparison.Ordinal)
&& _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
.ToList();
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
var index = 0;
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
foreach (var file in filesToDelete)
{
double percent = index / (double)filesToDelete.Count;
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
progress.Report(100 * percent);
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
cancellationToken.ThrowIfCancellationRequested();
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
_fileSystem.DeleteFile(file.FullName);
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
index++;
}
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
progress.Report(100);
2016-10-29 01:40:15 -04:00
2025-04-30 09:29:13 +02:00
return Task.CompletedTask;
2016-10-29 01:40:15 -04:00
}
}