DeleteLogFileTask.cs 4.1 KB

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