DeleteLogFileTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // Every so often
  39. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  40. };
  41. }
  42. /// <summary>
  43. /// Returns the task to be executed
  44. /// </summary>
  45. /// <param name="cancellationToken">The cancellation token.</param>
  46. /// <param name="progress">The progress.</param>
  47. /// <returns>Task.</returns>
  48. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  49. {
  50. // Delete log files more than n days old
  51. var minDateModified = DateTime.UtcNow.AddDays(-ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  52. // Only delete the .txt log files, the *.log files created by serilog get managed by itself
  53. var filesToDelete = _fileSystem.GetFiles(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, new[] { ".txt" }, true, true)
  54. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  55. .ToList();
  56. var index = 0;
  57. foreach (var file in filesToDelete)
  58. {
  59. double percent = index / (double)filesToDelete.Count;
  60. progress.Report(100 * percent);
  61. cancellationToken.ThrowIfCancellationRequested();
  62. _fileSystem.DeleteFile(file.FullName);
  63. index++;
  64. }
  65. progress.Report(100);
  66. return Task.CompletedTask;
  67. }
  68. public string Key => "CleanLogFiles";
  69. /// <summary>
  70. /// Gets the name of the task
  71. /// </summary>
  72. /// <value>The name.</value>
  73. public string Name => "Log file cleanup";
  74. /// <summary>
  75. /// Gets the description.
  76. /// </summary>
  77. /// <value>The description.</value>
  78. public string Description => string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  79. /// <summary>
  80. /// Gets the category.
  81. /// </summary>
  82. /// <value>The category.</value>
  83. public string Category => "Maintenance";
  84. /// <summary>
  85. /// Gets a value indicating whether this instance is hidden.
  86. /// </summary>
  87. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  88. public bool IsHidden => true;
  89. public bool IsEnabled => true;
  90. public bool IsLogged => true;
  91. }
  92. }