DeleteLogFileTask.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
  10. {
  11. /// <summary>
  12. /// Deletes old log files
  13. /// </summary>
  14. public class DeleteLogFileTask : IScheduledTask
  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. /// <summary>
  22. /// Initializes a new instance of the <see cref="DeleteLogFileTask" /> class.
  23. /// </summary>
  24. /// <param name="configurationManager">The configuration manager.</param>
  25. public DeleteLogFileTask(IConfigurationManager configurationManager)
  26. {
  27. ConfigurationManager = configurationManager;
  28. }
  29. /// <summary>
  30. /// Creates the triggers that define when the task will run
  31. /// </summary>
  32. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  33. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  34. {
  35. // Until we can vary these default triggers per server and MBT, we need something that makes sense for both
  36. return new ITaskTrigger[] {
  37. // At startup
  38. new StartupTrigger (),
  39. // Every so often
  40. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  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. var filesToDelete = new DirectoryInfo(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  54. .Where(f => f.LastWriteTimeUtc < minDateModified)
  55. .ToList();
  56. var index = 0;
  57. foreach (var file in filesToDelete)
  58. {
  59. double percent = index;
  60. percent /= filesToDelete.Count;
  61. progress.Report(100 * percent);
  62. cancellationToken.ThrowIfCancellationRequested();
  63. File.Delete(file.FullName);
  64. index++;
  65. }
  66. progress.Report(100);
  67. return Task.FromResult(true);
  68. }
  69. /// <summary>
  70. /// Gets the name of the task
  71. /// </summary>
  72. /// <value>The name.</value>
  73. public string Name
  74. {
  75. get { return "Log file cleanup"; }
  76. }
  77. /// <summary>
  78. /// Gets the description.
  79. /// </summary>
  80. /// <value>The description.</value>
  81. public string Description
  82. {
  83. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  84. }
  85. /// <summary>
  86. /// Gets the category.
  87. /// </summary>
  88. /// <value>The category.</value>
  89. public string Category
  90. {
  91. get
  92. {
  93. return "Maintenance";
  94. }
  95. }
  96. }
  97. }