DeleteCacheFileTask.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Controller.IO;
  9. using MediaBrowser.Model.Globalization;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Tasks;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  14. {
  15. /// <summary>
  16. /// Deletes old cache files.
  17. /// </summary>
  18. public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask
  19. {
  20. /// <summary>
  21. /// Gets or sets the application paths.
  22. /// </summary>
  23. /// <value>The application paths.</value>
  24. private readonly IApplicationPaths _applicationPaths;
  25. private readonly ILogger<DeleteCacheFileTask> _logger;
  26. private readonly IFileSystem _fileSystem;
  27. private readonly ILocalizationManager _localization;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
  30. /// </summary>
  31. /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
  32. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  33. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  34. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  35. public DeleteCacheFileTask(
  36. IApplicationPaths appPaths,
  37. ILogger<DeleteCacheFileTask> logger,
  38. IFileSystem fileSystem,
  39. ILocalizationManager localization)
  40. {
  41. _applicationPaths = appPaths;
  42. _logger = logger;
  43. _fileSystem = fileSystem;
  44. _localization = localization;
  45. }
  46. /// <inheritdoc />
  47. public string Name => _localization.GetLocalizedString("TaskCleanCache");
  48. /// <inheritdoc />
  49. public string Description => _localization.GetLocalizedString("TaskCleanCacheDescription");
  50. /// <inheritdoc />
  51. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  52. /// <inheritdoc />
  53. public string Key => "DeleteCacheFiles";
  54. /// <inheritdoc />
  55. public bool IsHidden => false;
  56. /// <inheritdoc />
  57. public bool IsEnabled => true;
  58. /// <inheritdoc />
  59. public bool IsLogged => true;
  60. /// <summary>
  61. /// Creates the triggers that define when the task will run.
  62. /// </summary>
  63. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  64. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  65. {
  66. return new[]
  67. {
  68. // Every so often
  69. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks }
  70. };
  71. }
  72. /// <inheritdoc />
  73. public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  74. {
  75. var minDateModified = DateTime.UtcNow.AddDays(-30);
  76. try
  77. {
  78. DeleteCacheFilesFromDirectory(_applicationPaths.CachePath, minDateModified, progress, cancellationToken);
  79. }
  80. catch (DirectoryNotFoundException)
  81. {
  82. // No biggie here. Nothing to delete
  83. }
  84. progress.Report(90);
  85. minDateModified = DateTime.UtcNow.AddDays(-1);
  86. try
  87. {
  88. DeleteCacheFilesFromDirectory(_applicationPaths.TempDirectory, minDateModified, progress, cancellationToken);
  89. }
  90. catch (DirectoryNotFoundException)
  91. {
  92. // No biggie here. Nothing to delete
  93. }
  94. return Task.CompletedTask;
  95. }
  96. /// <summary>
  97. /// Deletes the cache files from directory with a last write time less than a given date.
  98. /// </summary>
  99. /// <param name="directory">The directory.</param>
  100. /// <param name="minDateModified">The min date modified.</param>
  101. /// <param name="progress">The progress.</param>
  102. /// <param name="cancellationToken">The task cancellation token.</param>
  103. private void DeleteCacheFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, CancellationToken cancellationToken)
  104. {
  105. var filesToDelete = _fileSystem.GetFiles(directory, true)
  106. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  107. .ToList();
  108. var index = 0;
  109. foreach (var file in filesToDelete)
  110. {
  111. double percent = index;
  112. percent /= filesToDelete.Count;
  113. progress.Report(100 * percent);
  114. cancellationToken.ThrowIfCancellationRequested();
  115. FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
  116. index++;
  117. }
  118. FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger);
  119. progress.Report(100);
  120. }
  121. }
  122. }