2
0

DeleteLogFileTask.cs 3.3 KB

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