2
0

DeleteCacheFileTask.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Model.Tasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.Composition;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Common.ScheduledTasks.Tasks
  11. {
  12. /// <summary>
  13. /// Deletes old cache files
  14. /// </summary>
  15. [Export(typeof(IScheduledTask))]
  16. public class DeleteCacheFileTask : BaseScheduledTask<IKernel>
  17. {
  18. /// <summary>
  19. /// Creates the triggers that define when the task will run
  20. /// </summary>
  21. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  22. protected override IEnumerable<BaseTaskTrigger> GetDefaultTriggers()
  23. {
  24. var trigger = new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }; //2am
  25. return new[] { trigger };
  26. }
  27. /// <summary>
  28. /// Returns the task to be executed
  29. /// </summary>
  30. /// <param name="cancellationToken">The cancellation token.</param>
  31. /// <param name="progress">The progress.</param>
  32. /// <returns>Task.</returns>
  33. protected override Task ExecuteInternal(CancellationToken cancellationToken, IProgress<TaskProgress> progress)
  34. {
  35. return Task.Run(() =>
  36. {
  37. var minDateModified = DateTime.UtcNow.AddMonths(-2);
  38. DeleteCacheFilesFromDirectory(cancellationToken, Kernel.ApplicationPaths.CachePath, minDateModified, progress);
  39. });
  40. }
  41. /// <summary>
  42. /// Deletes the cache files from directory with a last write time less than a given date
  43. /// </summary>
  44. /// <param name="cancellationToken">The task cancellation token.</param>
  45. /// <param name="directory">The directory.</param>
  46. /// <param name="minDateModified">The min date modified.</param>
  47. /// <param name="progress">The progress.</param>
  48. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<TaskProgress> progress)
  49. {
  50. var filesToDelete = new DirectoryInfo(directory).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  51. .Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && f.LastWriteTimeUtc < minDateModified)
  52. .ToList();
  53. var index = 0;
  54. foreach (var file in filesToDelete)
  55. {
  56. double percent = index;
  57. percent /= filesToDelete.Count;
  58. progress.Report(new TaskProgress { Description = file.FullName, PercentComplete = 100 * percent });
  59. cancellationToken.ThrowIfCancellationRequested();
  60. File.Delete(file.FullName);
  61. index++;
  62. }
  63. progress.Report(new TaskProgress { PercentComplete = 100 });
  64. }
  65. /// <summary>
  66. /// Gets the name of the task
  67. /// </summary>
  68. /// <value>The name.</value>
  69. public override string Name
  70. {
  71. get { return "Cache file cleanup"; }
  72. }
  73. /// <summary>
  74. /// Gets the description.
  75. /// </summary>
  76. /// <value>The description.</value>
  77. public override string Description
  78. {
  79. get { return "Deletes cache files no longer needed by the system"; }
  80. }
  81. /// <summary>
  82. /// Gets the category.
  83. /// </summary>
  84. /// <value>The category.</value>
  85. public override string Category
  86. {
  87. get
  88. {
  89. return "Maintenance";
  90. }
  91. }
  92. }
  93. }