DeleteLogFileTask.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var trigger = new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }; //2am
  36. return new[] { trigger };
  37. }
  38. /// <summary>
  39. /// Returns the task to be executed
  40. /// </summary>
  41. /// <param name="cancellationToken">The cancellation token.</param>
  42. /// <param name="progress">The progress.</param>
  43. /// <returns>Task.</returns>
  44. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  45. {
  46. return Task.Run(() =>
  47. {
  48. // Delete log files more than n days old
  49. var minDateModified = DateTime.UtcNow.AddDays(-(ConfigurationManager.CommonConfiguration.LogFileRetentionDays));
  50. var filesToDelete = new DirectoryInfo(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  51. .Where(f => f.LastWriteTimeUtc < minDateModified)
  52. .ToList();
  53. var index = 0;
  54. foreach (var file in filesToDelete)
  55. {
  56. double percent = index;
  57. percent /= filesToDelete.Count;
  58. progress.Report(100 * percent);
  59. cancellationToken.ThrowIfCancellationRequested();
  60. File.Delete(file.FullName);
  61. index++;
  62. }
  63. progress.Report(100);
  64. });
  65. }
  66. /// <summary>
  67. /// Gets the name of the task
  68. /// </summary>
  69. /// <value>The name.</value>
  70. public string Name
  71. {
  72. get { return "Log file cleanup"; }
  73. }
  74. /// <summary>
  75. /// Gets the description.
  76. /// </summary>
  77. /// <value>The description.</value>
  78. public string Description
  79. {
  80. get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); }
  81. }
  82. /// <summary>
  83. /// Gets the category.
  84. /// </summary>
  85. /// <value>The category.</value>
  86. public string Category
  87. {
  88. get
  89. {
  90. return "Maintenance";
  91. }
  92. }
  93. }
  94. }