DeleteLogFileTask.cs 4.1 KB

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