DeleteCacheFileTask.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Model.Logging;
  10. namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
  11. {
  12. /// <summary>
  13. /// Deletes old cache files
  14. /// </summary>
  15. public class DeleteCacheFileTask : IScheduledTask
  16. {
  17. /// <summary>
  18. /// Gets or sets the application paths.
  19. /// </summary>
  20. /// <value>The application paths.</value>
  21. private IApplicationPaths ApplicationPaths { get; set; }
  22. private readonly ILogger _logger;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
  25. /// </summary>
  26. /// <param name="appPaths">The app paths.</param>
  27. public DeleteCacheFileTask(IApplicationPaths appPaths, ILogger logger)
  28. {
  29. ApplicationPaths = appPaths;
  30. _logger = logger;
  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. var minDateModified = DateTime.UtcNow.AddDays(-30);
  55. try
  56. {
  57. DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress);
  58. }
  59. catch (DirectoryNotFoundException)
  60. {
  61. // No biggie here. Nothing to delete
  62. }
  63. progress.Report(90);
  64. try
  65. {
  66. DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.TempDirectory, DateTime.MaxValue, progress);
  67. }
  68. catch (DirectoryNotFoundException)
  69. {
  70. // No biggie here. Nothing to delete
  71. }
  72. return Task.FromResult(true);
  73. }
  74. /// <summary>
  75. /// Deletes the cache files from directory with a last write time less than a given date
  76. /// </summary>
  77. /// <param name="cancellationToken">The task cancellation token.</param>
  78. /// <param name="directory">The directory.</param>
  79. /// <param name="minDateModified">The min date modified.</param>
  80. /// <param name="progress">The progress.</param>
  81. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  82. {
  83. var filesToDelete = new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories)
  84. .Where(f => f.LastWriteTimeUtc < minDateModified)
  85. .ToList();
  86. var index = 0;
  87. foreach (var file in filesToDelete)
  88. {
  89. double percent = index;
  90. percent /= filesToDelete.Count;
  91. progress.Report(100 * percent);
  92. cancellationToken.ThrowIfCancellationRequested();
  93. DeleteFile(file.FullName);
  94. index++;
  95. }
  96. progress.Report(100);
  97. }
  98. private void DeleteFile(string path)
  99. {
  100. try
  101. {
  102. File.Delete(path);
  103. }
  104. catch (IOException ex)
  105. {
  106. _logger.ErrorException("Error deleting file {0}", ex, path);
  107. }
  108. }
  109. /// <summary>
  110. /// Gets the name of the task
  111. /// </summary>
  112. /// <value>The name.</value>
  113. public string Name
  114. {
  115. get { return "Cache file cleanup"; }
  116. }
  117. /// <summary>
  118. /// Gets the description.
  119. /// </summary>
  120. /// <value>The description.</value>
  121. public string Description
  122. {
  123. get { return "Deletes cache files no longer needed by the system"; }
  124. }
  125. /// <summary>
  126. /// Gets the category.
  127. /// </summary>
  128. /// <value>The category.</value>
  129. public string Category
  130. {
  131. get
  132. {
  133. return "Maintenance";
  134. }
  135. }
  136. }
  137. }