DeleteLogFileTask.cs 3.6 KB

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