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

102 lines
3.6 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;
namespace Emby.Server.Implementations.ScheduledTasks.Tasks
2016-10-29 01:40:15 -04:00
{
/// <summary>
2020-02-01 22:27:25 +09:00
/// Deletes old log files.
2016-10-29 01:40:15 -04:00
/// </summary>
public class DeleteLogFileTask : IScheduledTask, IConfigurableScheduledTask
{
private readonly IConfigurationManager _configurationManager;
2016-10-29 01:40:15 -04:00
private readonly IFileSystem _fileSystem;
private readonly ILocalizationManager _localization;
2016-10-29 01:40:15 -04:00
/// <summary>
/// Initializes a new instance of the <see cref="DeleteLogFileTask" /> class.
/// </summary>
2024-09-04 17:38:10 +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>
2020-03-26 22:26:25 +01:00
public DeleteLogFileTask(IConfigurationManager configurationManager, IFileSystem fileSystem, ILocalizationManager localization)
2016-10-29 01:40:15 -04:00
{
_configurationManager = configurationManager;
2016-10-29 01:40:15 -04:00
_fileSystem = fileSystem;
2020-03-26 22:26:25 +01:00
_localization = localization;
2016-10-29 01:40:15 -04:00
}
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskCleanLogs");
/// <inheritdoc />
public string Description => string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("TaskCleanLogsDescription"),
_configurationManager.CommonConfiguration.LogFileRetentionDays);
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
/// <inheritdoc />
public string Key => "CleanLogFiles";
/// <inheritdoc />
public bool IsHidden => false;
/// <inheritdoc />
public bool IsEnabled => true;
/// <inheritdoc />
public bool IsLogged => true;
2024-09-04 17:38:10 +02:00
/// <inheritdoc />
2016-10-29 01:40:15 -04:00
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
2024-09-04 17:38:10 +02:00
return
[
2020-10-12 19:22:33 +02:00
new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks }
2024-09-04 17:38:10 +02:00
];
2016-10-29 01:40:15 -04:00
}
2022-02-15 18:59:46 +01:00
/// <inheritdoc />
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
2016-10-29 01:40:15 -04:00
{
// Delete log files more than n days old
var minDateModified = DateTime.UtcNow.AddDays(-_configurationManager.CommonConfiguration.LogFileRetentionDays);
2016-10-29 01:40:15 -04:00
2021-01-18 19:42:50 -07: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
var index = 0;
foreach (var file in filesToDelete)
{
double percent = index / (double)filesToDelete.Count;
2016-10-29 01:40:15 -04:00
progress.Report(100 * percent);
cancellationToken.ThrowIfCancellationRequested();
_fileSystem.DeleteFile(file.FullName);
index++;
}
progress.Report(100);
2018-09-12 19:26:21 +02:00
return Task.CompletedTask;
2016-10-29 01:40:15 -04:00
}
}
}