DeleteLogFileTask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using CommonIO;
  9. namespace MediaBrowser.Common.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<ITaskTrigger> GetDefaultTriggers()
  36. {
  37. // Until we can vary these default triggers per server and MBT, we need something that makes sense for both
  38. return new ITaskTrigger[] {
  39. // Every so often
  40. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  41. };
  42. }
  43. /// <summary>
  44. /// Returns the task to be executed
  45. /// </summary>
  46. /// <param name="cancellationToken">The cancellation token.</param>
  47. /// <param name="progress">The progress.</param>
  48. /// <returns>Task.</returns>
  49. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  50. {
  51. // Delete log files more than n days old
  52. var minDateModified = DateTime.UtcNow.AddDays(-ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  53. var filesToDelete = _fileSystem.GetFiles(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, 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;
  60. percent /= filesToDelete.Count;
  61. progress.Report(100 * percent);
  62. cancellationToken.ThrowIfCancellationRequested();
  63. _fileSystem.DeleteFile(file.FullName);
  64. index++;
  65. }
  66. progress.Report(100);
  67. return Task.FromResult(true);
  68. }
  69. /// <summary>
  70. /// Gets the name of the task
  71. /// </summary>
  72. /// <value>The name.</value>
  73. public string Name
  74. {
  75. get { return "Log file cleanup"; }
  76. }
  77. /// <summary>
  78. /// Gets the description.
  79. /// </summary>
  80. /// <value>The description.</value>
  81. public string Description
  82. {
  83. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  84. }
  85. /// <summary>
  86. /// Gets the category.
  87. /// </summary>
  88. /// <value>The category.</value>
  89. public string Category
  90. {
  91. get
  92. {
  93. return "Maintenance";
  94. }
  95. }
  96. /// <summary>
  97. /// Gets a value indicating whether this instance is hidden.
  98. /// </summary>
  99. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  100. public bool IsHidden
  101. {
  102. get { return true; }
  103. }
  104. public bool IsEnabled
  105. {
  106. get { return true; }
  107. }
  108. }
  109. }