DeleteCacheFileTask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  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 kernel.
  19. /// </summary>
  20. /// <value>The kernel.</value>
  21. private IKernel Kernel { get; set; }
  22. /// <summary>
  23. /// Gets or sets the logger.
  24. /// </summary>
  25. /// <value>The logger.</value>
  26. private ILogger Logger { get; set; }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
  29. /// </summary>
  30. /// <param name="kernel">The kernel.</param>
  31. /// <param name="logger">The logger.</param>
  32. public DeleteCacheFileTask(IKernel kernel, ILogger logger)
  33. {
  34. Kernel = kernel;
  35. Logger = logger;
  36. }
  37. /// <summary>
  38. /// Creates the triggers that define when the task will run
  39. /// </summary>
  40. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  41. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  42. {
  43. var trigger = new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }; //2am
  44. return new[] { trigger };
  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. return Task.Run(() =>
  55. {
  56. var minDateModified = DateTime.UtcNow.AddMonths(-2);
  57. DeleteCacheFilesFromDirectory(cancellationToken, Kernel.ApplicationPaths.CachePath, minDateModified, progress);
  58. });
  59. }
  60. /// <summary>
  61. /// Deletes the cache files from directory with a last write time less than a given date
  62. /// </summary>
  63. /// <param name="cancellationToken">The task cancellation token.</param>
  64. /// <param name="directory">The directory.</param>
  65. /// <param name="minDateModified">The min date modified.</param>
  66. /// <param name="progress">The progress.</param>
  67. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  68. {
  69. var filesToDelete = new DirectoryInfo(directory).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  70. .Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && f.LastWriteTimeUtc < minDateModified)
  71. .ToList();
  72. var index = 0;
  73. foreach (var file in filesToDelete)
  74. {
  75. double percent = index;
  76. percent /= filesToDelete.Count;
  77. progress.Report(100 * percent);
  78. cancellationToken.ThrowIfCancellationRequested();
  79. File.Delete(file.FullName);
  80. index++;
  81. }
  82. progress.Report(100);
  83. }
  84. /// <summary>
  85. /// Gets the name of the task
  86. /// </summary>
  87. /// <value>The name.</value>
  88. public string Name
  89. {
  90. get { return "Cache file cleanup"; }
  91. }
  92. /// <summary>
  93. /// Gets the description.
  94. /// </summary>
  95. /// <value>The description.</value>
  96. public string Description
  97. {
  98. get { return "Deletes cache files no longer needed by the system"; }
  99. }
  100. /// <summary>
  101. /// Gets the category.
  102. /// </summary>
  103. /// <value>The category.</value>
  104. public string Category
  105. {
  106. get
  107. {
  108. return "Maintenance";
  109. }
  110. }
  111. }
  112. }