DeleteLogFileTask.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Model.Tasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.Composition;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Common.ScheduledTasks.Tasks
  11. {
  12. /// <summary>
  13. /// Deletes old log files
  14. /// </summary>
  15. [Export(typeof(IScheduledTask))]
  16. public class DeleteLogFileTask : BaseScheduledTask<IKernel>
  17. {
  18. /// <summary>
  19. /// Creates the triggers that define when the task will run
  20. /// </summary>
  21. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  22. protected override IEnumerable<BaseTaskTrigger> GetDefaultTriggers()
  23. {
  24. var trigger = new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }; //2am
  25. return new[] { trigger };
  26. }
  27. /// <summary>
  28. /// Returns the task to be executed
  29. /// </summary>
  30. /// <param name="cancellationToken">The cancellation token.</param>
  31. /// <param name="progress">The progress.</param>
  32. /// <returns>Task.</returns>
  33. protected override Task ExecuteInternal(CancellationToken cancellationToken, IProgress<TaskProgress> progress)
  34. {
  35. return Task.Run(() =>
  36. {
  37. // Delete log files more than n days old
  38. var minDateModified = DateTime.UtcNow.AddDays(-(Kernel.Configuration.LogFileRetentionDays));
  39. var filesToDelete = new DirectoryInfo(Kernel.ApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
  40. .Where(f => f.LastWriteTimeUtc < minDateModified)
  41. .ToList();
  42. var index = 0;
  43. foreach (var file in filesToDelete)
  44. {
  45. double percent = index;
  46. percent /= filesToDelete.Count;
  47. progress.Report(new TaskProgress { Description = file.FullName, PercentComplete = 100 * percent });
  48. cancellationToken.ThrowIfCancellationRequested();
  49. File.Delete(file.FullName);
  50. index++;
  51. }
  52. progress.Report(new TaskProgress { PercentComplete = 100 });
  53. });
  54. }
  55. /// <summary>
  56. /// Gets the name of the task
  57. /// </summary>
  58. /// <value>The name.</value>
  59. public override string Name
  60. {
  61. get { return "Log file cleanup"; }
  62. }
  63. /// <summary>
  64. /// Gets the description.
  65. /// </summary>
  66. /// <value>The description.</value>
  67. public override string Description
  68. {
  69. get { return string.Format("Deletes log files that are more than {0} days old.", Kernel.Configuration.LogFileRetentionDays); }
  70. }
  71. /// <summary>
  72. /// Gets the category.
  73. /// </summary>
  74. /// <value>The category.</value>
  75. public override string Category
  76. {
  77. get
  78. {
  79. return "Maintenance";
  80. }
  81. }
  82. }
  83. }