DeleteLogFileTask.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. return Task.Run(() =>
  52. {
  53. // Delete log files more than n days old
  54. var minDateModified = DateTime.UtcNow.AddDays(-(ConfigurationManager.CommonConfiguration.LogFileRetentionDays));
  55. var filesToDelete = new DirectoryInfo(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  56. .Where(f => f.LastWriteTimeUtc < minDateModified)
  57. .ToList();
  58. var index = 0;
  59. foreach (var file in filesToDelete)
  60. {
  61. double percent = index;
  62. percent /= filesToDelete.Count;
  63. progress.Report(100 * percent);
  64. cancellationToken.ThrowIfCancellationRequested();
  65. File.Delete(file.FullName);
  66. index++;
  67. }
  68. progress.Report(100);
  69. });
  70. }
  71. /// <summary>
  72. /// Gets the name of the task
  73. /// </summary>
  74. /// <value>The name.</value>
  75. public string Name
  76. {
  77. get { return "Log file cleanup"; }
  78. }
  79. /// <summary>
  80. /// Gets the description.
  81. /// </summary>
  82. /// <value>The description.</value>
  83. public string Description
  84. {
  85. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  86. }
  87. /// <summary>
  88. /// Gets the category.
  89. /// </summary>
  90. /// <value>The category.</value>
  91. public string Category
  92. {
  93. get
  94. {
  95. return "Maintenance";
  96. }
  97. }
  98. }
  99. }