DeleteLogFileTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.Tasks;
  9. using MediaBrowser.Model.Globalization;
  10. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  11. {
  12. /// <summary>
  13. /// Deletes old log files.
  14. /// </summary>
  15. public class DeleteLogFileTask : IScheduledTask, IConfigurableScheduledTask
  16. {
  17. /// <summary>
  18. /// Gets or sets the configuration manager.
  19. /// </summary>
  20. /// <value>The configuration manager.</value>
  21. private IConfigurationManager ConfigurationManager { get; set; }
  22. private readonly IFileSystem _fileSystem;
  23. private readonly ILocalizationManager _localization;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="DeleteLogFileTask" /> class.
  26. /// </summary>
  27. /// <param name="configurationManager">The configuration manager.</param>
  28. /// <param name="fileSystem">The file system.</param>
  29. /// <param name="localization">The localization manager.</param>
  30. public DeleteLogFileTask(IConfigurationManager configurationManager, IFileSystem fileSystem, ILocalizationManager localization)
  31. {
  32. ConfigurationManager = configurationManager;
  33. _fileSystem = fileSystem;
  34. _localization = localization;
  35. }
  36. /// <summary>
  37. /// Creates the triggers that define when the task will run.
  38. /// </summary>
  39. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  40. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  41. {
  42. return new[] {
  43. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  44. };
  45. }
  46. /// <summary>
  47. /// Returns the task to be executed.
  48. /// </summary>
  49. /// <param name="cancellationToken">The cancellation token.</param>
  50. /// <param name="progress">The progress.</param>
  51. /// <returns>Task.</returns>
  52. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  53. {
  54. // Delete log files more than n days old
  55. var minDateModified = DateTime.UtcNow.AddDays(-ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  56. // Only delete the .txt log files, the *.log files created by serilog get managed by itself
  57. var filesToDelete = _fileSystem.GetFiles(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, new[] { ".txt" }, true, true)
  58. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  59. .ToList();
  60. var index = 0;
  61. foreach (var file in filesToDelete)
  62. {
  63. double percent = index / (double)filesToDelete.Count;
  64. progress.Report(100 * percent);
  65. cancellationToken.ThrowIfCancellationRequested();
  66. _fileSystem.DeleteFile(file.FullName);
  67. index++;
  68. }
  69. progress.Report(100);
  70. return Task.CompletedTask;
  71. }
  72. /// <inheritdoc />
  73. public string Name => _localization.GetLocalizedString("TaskCleanLogs");
  74. /// <inheritdoc />
  75. public string Description => string.Format(_localization.GetLocalizedString("TaskCleanLogsDescription"), ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  76. /// <inheritdoc />
  77. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  78. /// <inheritdoc />
  79. public string Key => "CleanLogFiles";
  80. /// <inheritdoc />
  81. public bool IsHidden => false;
  82. /// <inheritdoc />
  83. public bool IsEnabled => true;
  84. /// <inheritdoc />
  85. public bool IsLogged => true;
  86. }
  87. }