DeleteLogFileTask.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. using MediaBrowser.Model.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<TaskTriggerInfo> GetDefaultTriggers()
  37. {
  38. return new[] {
  39. // Every so often
  40. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  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. public string Key
  70. {
  71. get { return "CleanLogFiles"; }
  72. }
  73. /// <summary>
  74. /// Gets the name of the task
  75. /// </summary>
  76. /// <value>The name.</value>
  77. public string Name
  78. {
  79. get { return "Log file cleanup"; }
  80. }
  81. /// <summary>
  82. /// Gets the description.
  83. /// </summary>
  84. /// <value>The description.</value>
  85. public string Description
  86. {
  87. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  88. }
  89. /// <summary>
  90. /// Gets the category.
  91. /// </summary>
  92. /// <value>The category.</value>
  93. public string Category
  94. {
  95. get
  96. {
  97. return "Maintenance";
  98. }
  99. }
  100. /// <summary>
  101. /// Gets a value indicating whether this instance is hidden.
  102. /// </summary>
  103. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  104. public bool IsHidden
  105. {
  106. get { return true; }
  107. }
  108. public bool IsEnabled
  109. {
  110. get { return true; }
  111. }
  112. public bool IsLogged
  113. {
  114. get { return true; }
  115. }
  116. }
  117. }