DeleteCacheFileTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. var trigger = new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }; //2am
  36. return new[] { trigger };
  37. }
  38. /// <summary>
  39. /// Returns the task to be executed
  40. /// </summary>
  41. /// <param name="cancellationToken">The cancellation token.</param>
  42. /// <param name="progress">The progress.</param>
  43. /// <returns>Task.</returns>
  44. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  45. {
  46. return Task.Run(() =>
  47. {
  48. var minDateModified = DateTime.UtcNow.AddDays(-45);
  49. DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress);
  50. });
  51. }
  52. /// <summary>
  53. /// Deletes the cache files from directory with a last write time less than a given date
  54. /// </summary>
  55. /// <param name="cancellationToken">The task cancellation token.</param>
  56. /// <param name="directory">The directory.</param>
  57. /// <param name="minDateModified">The min date modified.</param>
  58. /// <param name="progress">The progress.</param>
  59. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  60. {
  61. var filesToDelete = new DirectoryInfo(directory).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  62. .Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && f.LastWriteTimeUtc < minDateModified)
  63. .ToList();
  64. var index = 0;
  65. foreach (var file in filesToDelete)
  66. {
  67. double percent = index;
  68. percent /= filesToDelete.Count;
  69. progress.Report(100 * percent);
  70. cancellationToken.ThrowIfCancellationRequested();
  71. File.Delete(file.FullName);
  72. index++;
  73. }
  74. progress.Report(100);
  75. }
  76. /// <summary>
  77. /// Gets the name of the task
  78. /// </summary>
  79. /// <value>The name.</value>
  80. public string Name
  81. {
  82. get { return "Cache file cleanup"; }
  83. }
  84. /// <summary>
  85. /// Gets the description.
  86. /// </summary>
  87. /// <value>The description.</value>
  88. public string Description
  89. {
  90. get { return "Deletes cache files no longer needed by the system"; }
  91. }
  92. /// <summary>
  93. /// Gets the category.
  94. /// </summary>
  95. /// <value>The category.</value>
  96. public string Category
  97. {
  98. get
  99. {
  100. return "Maintenance";
  101. }
  102. }
  103. }
  104. }