DeleteCacheFileTask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
  10. {
  11. /// <summary>
  12. /// Deletes old cache files
  13. /// </summary>
  14. public class DeleteCacheFileTask : IScheduledTask
  15. {
  16. /// <summary>
  17. /// Gets or sets the application paths.
  18. /// </summary>
  19. /// <value>The application paths.</value>
  20. private IApplicationPaths ApplicationPaths { get; set; }
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
  23. /// </summary>
  24. /// <param name="appPaths">The app paths.</param>
  25. public DeleteCacheFileTask(IApplicationPaths appPaths)
  26. {
  27. ApplicationPaths = appPaths;
  28. }
  29. /// <summary>
  30. /// Creates the triggers that define when the task will run
  31. /// </summary>
  32. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  33. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  34. {
  35. // Until we can vary these default triggers per server and MBT, we need something that makes sense for both
  36. return new ITaskTrigger[] {
  37. // At startup
  38. new StartupTrigger (),
  39. // Every so often
  40. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  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. return Task.Run(() =>
  52. {
  53. var minDateModified = DateTime.UtcNow.AddDays(-30);
  54. DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress);
  55. });
  56. }
  57. /// <summary>
  58. /// Deletes the cache files from directory with a last write time less than a given date
  59. /// </summary>
  60. /// <param name="cancellationToken">The task cancellation token.</param>
  61. /// <param name="directory">The directory.</param>
  62. /// <param name="minDateModified">The min date modified.</param>
  63. /// <param name="progress">The progress.</param>
  64. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  65. {
  66. var filesToDelete = new DirectoryInfo(directory).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  67. .Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && f.LastWriteTimeUtc < minDateModified)
  68. .ToList();
  69. var index = 0;
  70. foreach (var file in filesToDelete)
  71. {
  72. double percent = index;
  73. percent /= filesToDelete.Count;
  74. progress.Report(100 * percent);
  75. cancellationToken.ThrowIfCancellationRequested();
  76. File.Delete(file.FullName);
  77. index++;
  78. }
  79. progress.Report(100);
  80. }
  81. /// <summary>
  82. /// Gets the name of the task
  83. /// </summary>
  84. /// <value>The name.</value>
  85. public string Name
  86. {
  87. get { return "Cache file cleanup"; }
  88. }
  89. /// <summary>
  90. /// Gets the description.
  91. /// </summary>
  92. /// <value>The description.</value>
  93. public string Description
  94. {
  95. get { return "Deletes cache files no longer needed by the system"; }
  96. }
  97. /// <summary>
  98. /// Gets the category.
  99. /// </summary>
  100. /// <value>The category.</value>
  101. public string Category
  102. {
  103. get
  104. {
  105. return "Maintenance";
  106. }
  107. }
  108. }
  109. }