DeleteLogFileTask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // At startup
  41. new StartupTrigger (),
  42. // Every so often
  43. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  44. };
  45. }
  46. /// <summary>
  47. /// Returns the task to be executed
  48. /// </summary>
  49. /// <param name="cancellationToken">The cancellation token.</param>
  50. /// <param name="progress">The progress.</param>
  51. /// <returns>Task.</returns>
  52. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  53. {
  54. // Delete log files more than n days old
  55. var minDateModified = DateTime.UtcNow.AddDays(-(ConfigurationManager.CommonConfiguration.LogFileRetentionDays));
  56. var filesToDelete = new DirectoryInfo(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  57. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  58. .ToList();
  59. var index = 0;
  60. foreach (var file in filesToDelete)
  61. {
  62. double percent = index;
  63. percent /= filesToDelete.Count;
  64. progress.Report(100 * percent);
  65. cancellationToken.ThrowIfCancellationRequested();
  66. File.Delete(file.FullName);
  67. index++;
  68. }
  69. progress.Report(100);
  70. return Task.FromResult(true);
  71. }
  72. /// <summary>
  73. /// Gets the name of the task
  74. /// </summary>
  75. /// <value>The name.</value>
  76. public string Name
  77. {
  78. get { return "Log file cleanup"; }
  79. }
  80. /// <summary>
  81. /// Gets the description.
  82. /// </summary>
  83. /// <value>The description.</value>
  84. public string Description
  85. {
  86. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  87. }
  88. /// <summary>
  89. /// Gets the category.
  90. /// </summary>
  91. /// <value>The category.</value>
  92. public string Category
  93. {
  94. get
  95. {
  96. return "Maintenance";
  97. }
  98. }
  99. /// <summary>
  100. /// Gets a value indicating whether this instance is hidden.
  101. /// </summary>
  102. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  103. public bool IsHidden
  104. {
  105. get { return true; }
  106. }
  107. }
  108. }