DeleteLogFileTask.cs 3.5 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. 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)
  29. {
  30. ConfigurationManager = configurationManager;
  31. _fileSystem = fileSystem;
  32. }
  33. /// <summary>
  34. /// Creates the triggers that define when the task will run.
  35. /// </summary>
  36. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  37. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  38. {
  39. return new[] {
  40. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  41. };
  42. }
  43. /// <summary>
  44. /// Returns the task to be executed.
  45. /// </summary>
  46. /// <param name="cancellationToken">The cancellation token.</param>
  47. /// <param name="progress">The progress.</param>
  48. /// <returns>Task.</returns>
  49. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  50. {
  51. // Delete log files more than n days old
  52. var minDateModified = DateTime.UtcNow.AddDays(-ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  53. // Only delete the .txt log files, the *.log files created by serilog get managed by itself
  54. var filesToDelete = _fileSystem.GetFiles(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, new[] { ".txt" }, true, true)
  55. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  56. .ToList();
  57. var index = 0;
  58. foreach (var file in filesToDelete)
  59. {
  60. double percent = index / (double)filesToDelete.Count;
  61. progress.Report(100 * percent);
  62. cancellationToken.ThrowIfCancellationRequested();
  63. _fileSystem.DeleteFile(file.FullName);
  64. index++;
  65. }
  66. progress.Report(100);
  67. return Task.CompletedTask;
  68. }
  69. public string Name => _localization.GetLocalizedString("TaskCleanLogs");
  70. public string Description => string.Format(_localization.GetLocalizedString("TaskCleanLogsDescription"), ConfigurationManager.CommonConfiguration.LogFileRetentionDays);
  71. public string Category => _localization.GetLocalizedString("TasksMaintenance");
  72. public string Key => "CleanLogFiles";
  73. public bool IsHidden => false;
  74. public bool IsEnabled => true;
  75. public bool IsLogged => true;
  76. }
  77. }