DeleteLogFileTask.cs 4.2 KB

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