2
0

DeleteLogFileTask.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Key
  69. {
  70. get { return "CleanLogFiles"; }
  71. }
  72. /// <summary>
  73. /// Gets the name of the task
  74. /// </summary>
  75. /// <value>The name.</value>
  76. public string Name
  77. {
  78. get { return "Log file cleanup"; }
  79. }
  80. /// <summary>
  81. /// Gets the description.
  82. /// </summary>
  83. /// <value>The description.</value>
  84. public string Description
  85. {
  86. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  87. }
  88. /// <summary>
  89. /// Gets the category.
  90. /// </summary>
  91. /// <value>The category.</value>
  92. public string Category
  93. {
  94. get
  95. {
  96. return "Maintenance";
  97. }
  98. }
  99. /// <summary>
  100. /// Gets a value indicating whether this instance is hidden.
  101. /// </summary>
  102. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  103. public bool IsHidden
  104. {
  105. get { return true; }
  106. }
  107. public bool IsEnabled
  108. {
  109. get { return true; }
  110. }
  111. public bool IsLogged
  112. {
  113. get { return true; }
  114. }
  115. }
  116. }