DeleteCacheFileTask.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /// <inheritdoc />
  61. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  62. {
  63. return
  64. [
  65. // Every so often
  66. new TaskTriggerInfo { Type = TaskTriggerInfoType.IntervalTrigger, IntervalTicks = TimeSpan.FromHours(24).Ticks }
  67. ];
  68. }
  69. /// <inheritdoc />
  70. public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  71. {
  72. var minDateModified = DateTime.UtcNow.AddDays(-30);
  73. try
  74. {
  75. DeleteCacheFilesFromDirectory(_applicationPaths.CachePath, minDateModified, progress, cancellationToken);
  76. }
  77. catch (DirectoryNotFoundException)
  78. {
  79. // No biggie here. Nothing to delete
  80. }
  81. progress.Report(90);
  82. minDateModified = DateTime.UtcNow.AddDays(-1);
  83. try
  84. {
  85. DeleteCacheFilesFromDirectory(_applicationPaths.TempDirectory, minDateModified, progress, cancellationToken);
  86. }
  87. catch (DirectoryNotFoundException)
  88. {
  89. // No biggie here. Nothing to delete
  90. }
  91. return Task.CompletedTask;
  92. }
  93. /// <summary>
  94. /// Deletes the cache files from directory with a last write time less than a given date.
  95. /// </summary>
  96. /// <param name="directory">The directory.</param>
  97. /// <param name="minDateModified">The min date modified.</param>
  98. /// <param name="progress">The progress.</param>
  99. /// <param name="cancellationToken">The task cancellation token.</param>
  100. private void DeleteCacheFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, CancellationToken cancellationToken)
  101. {
  102. var filesToDelete = _fileSystem.GetFiles(directory, true)
  103. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  104. .ToList();
  105. var index = 0;
  106. foreach (var file in filesToDelete)
  107. {
  108. double percent = index;
  109. percent /= filesToDelete.Count;
  110. progress.Report(100 * percent);
  111. cancellationToken.ThrowIfCancellationRequested();
  112. FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
  113. index++;
  114. }
  115. FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger);
  116. progress.Report(100);
  117. }
  118. }
  119. }