DeleteCacheFileTask.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
  12. {
  13. /// <summary>
  14. /// Deletes old cache files
  15. /// </summary>
  16. public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask
  17. {
  18. /// <summary>
  19. /// Gets or sets the application paths.
  20. /// </summary>
  21. /// <value>The application paths.</value>
  22. private IApplicationPaths ApplicationPaths { get; set; }
  23. private readonly ILogger _logger;
  24. private readonly IFileSystem _fileSystem;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
  27. /// </summary>
  28. public DeleteCacheFileTask(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem)
  29. {
  30. ApplicationPaths = appPaths;
  31. _logger = logger;
  32. _fileSystem = fileSystem;
  33. }
  34. /// <summary>
  35. /// Creates the triggers that define when the task will run
  36. /// </summary>
  37. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  38. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  39. {
  40. // Until we can vary these default triggers per server and MBT, we need something that makes sense for both
  41. return new ITaskTrigger[] {
  42. // At startup
  43. new StartupTrigger (),
  44. // Every so often
  45. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  46. };
  47. }
  48. /// <summary>
  49. /// Returns the task to be executed
  50. /// </summary>
  51. /// <param name="cancellationToken">The cancellation token.</param>
  52. /// <param name="progress">The progress.</param>
  53. /// <returns>Task.</returns>
  54. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  55. {
  56. var minDateModified = DateTime.UtcNow.AddDays(-30);
  57. try
  58. {
  59. DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress);
  60. }
  61. catch (DirectoryNotFoundException)
  62. {
  63. // No biggie here. Nothing to delete
  64. }
  65. progress.Report(90);
  66. try
  67. {
  68. DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.TempDirectory, DateTime.MaxValue, progress);
  69. }
  70. catch (DirectoryNotFoundException)
  71. {
  72. // No biggie here. Nothing to delete
  73. }
  74. return Task.FromResult(true);
  75. }
  76. /// <summary>
  77. /// Deletes the cache files from directory with a last write time less than a given date
  78. /// </summary>
  79. /// <param name="cancellationToken">The task cancellation token.</param>
  80. /// <param name="directory">The directory.</param>
  81. /// <param name="minDateModified">The min date modified.</param>
  82. /// <param name="progress">The progress.</param>
  83. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  84. {
  85. var filesToDelete = new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories)
  86. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  87. .ToList();
  88. var index = 0;
  89. foreach (var file in filesToDelete)
  90. {
  91. double percent = index;
  92. percent /= filesToDelete.Count;
  93. progress.Report(100 * percent);
  94. cancellationToken.ThrowIfCancellationRequested();
  95. DeleteFile(file.FullName);
  96. index++;
  97. }
  98. progress.Report(100);
  99. }
  100. private void DeleteFile(string path)
  101. {
  102. try
  103. {
  104. File.Delete(path);
  105. }
  106. catch (IOException ex)
  107. {
  108. _logger.ErrorException("Error deleting file {0}", ex, path);
  109. }
  110. }
  111. /// <summary>
  112. /// Gets the name of the task
  113. /// </summary>
  114. /// <value>The name.</value>
  115. public string Name
  116. {
  117. get { return "Cache file cleanup"; }
  118. }
  119. /// <summary>
  120. /// Gets the description.
  121. /// </summary>
  122. /// <value>The description.</value>
  123. public string Description
  124. {
  125. get { return "Deletes cache files no longer needed by the system"; }
  126. }
  127. /// <summary>
  128. /// Gets the category.
  129. /// </summary>
  130. /// <value>The category.</value>
  131. public string Category
  132. {
  133. get
  134. {
  135. return "Maintenance";
  136. }
  137. }
  138. /// <summary>
  139. /// Gets a value indicating whether this instance is hidden.
  140. /// </summary>
  141. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  142. public bool IsHidden
  143. {
  144. get { return true; }
  145. }
  146. public bool IsEnabled
  147. {
  148. get { return true; }
  149. }
  150. }
  151. }