DeleteLogFileTask.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.Tasks;
  9. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  10. {
  11. /// <summary>
  12. /// Deletes old log files
  13. /// </summary>
  14. public class DeleteLogFileTask : IScheduledTask, IConfigurableScheduledTask
  15. {
  16. /// <summary>
  17. /// Gets or sets the configuration manager.
  18. /// </summary>
  19. /// <value>The configuration manager.</value>
  20. private IConfigurationManager ConfigurationManager { get; set; }
  21. private readonly IFileSystem _fileSystem;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="DeleteLogFileTask" /> class.
  24. /// </summary>
  25. /// <param name="configurationManager">The configuration manager.</param>
  26. public DeleteLogFileTask(IConfigurationManager configurationManager, IFileSystem fileSystem)
  27. {
  28. ConfigurationManager = configurationManager;
  29. _fileSystem = fileSystem;
  30. }
  31. /// <summary>
  32. /// Creates the triggers that define when the task will run
  33. /// </summary>
  34. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  35. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  36. {
  37. return new[] {
  38. // Every so often
  39. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  40. };
  41. }
  42. /// <summary>
  43. /// Returns the task to be executed
  44. /// </summary>
  45. /// <param name="cancellationToken">The cancellation token.</param>
  46. /// <param name="progress">The progress.</param>
  47. /// <returns>Task.</returns>
  48. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  49. {
  50. // Delete log files more than n days old
  51. var minDateModified = DateTime.UtcNow.AddDays(-ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  52. // Only delete the .txt log files, the *.log files created by serilog get managed by itself
  53. var filesToDelete = _fileSystem.GetFiles(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, new[] { ".txt" }, true, true)
  54. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  55. .ToList();
  56. var index = 0;
  57. foreach (var file in filesToDelete)
  58. {
  59. double percent = index / (double)filesToDelete.Count;
  60. progress.Report(100 * percent);
  61. cancellationToken.ThrowIfCancellationRequested();
  62. _fileSystem.DeleteFile(file.FullName);
  63. index++;
  64. }
  65. progress.Report(100);
  66. return Task.CompletedTask;
  67. }
  68. public string Name => "Log file cleanup";
  69. public string Description => string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  70. public string Category => "Maintenance";
  71. public string Key => "CleanLogFiles";
  72. public bool IsHidden => false;
  73. public bool IsEnabled => true;
  74. public bool IsLogged => true;
  75. }
  76. }