SortingScheduledTask.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using MediaBrowser.Common.ScheduledTasks;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Server.Implementations.FileSorting
  11. {
  12. public class SortingScheduledTask : IScheduledTask, IConfigurableScheduledTask
  13. {
  14. private readonly IServerConfigurationManager _config;
  15. private readonly ILogger _logger;
  16. private readonly ILibraryManager _libraryManager;
  17. public SortingScheduledTask(IServerConfigurationManager config, ILogger logger, ILibraryManager libraryManager)
  18. {
  19. _config = config;
  20. _logger = logger;
  21. _libraryManager = libraryManager;
  22. }
  23. public string Name
  24. {
  25. get { return "Sort new files"; }
  26. }
  27. public string Description
  28. {
  29. get { return "Processes new files available in the configured sorting location."; }
  30. }
  31. public string Category
  32. {
  33. get { return "Library"; }
  34. }
  35. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  36. {
  37. return Task.Run(() => SortFiles(cancellationToken, progress), cancellationToken);
  38. }
  39. private void SortFiles(CancellationToken cancellationToken, IProgress<double> progress)
  40. {
  41. var numComplete = 0;
  42. var paths = _config.Configuration.FileSortingOptions.TvWatchLocations.ToList();
  43. foreach (var path in paths)
  44. {
  45. cancellationToken.ThrowIfCancellationRequested();
  46. try
  47. {
  48. SortFiles(path);
  49. }
  50. catch (Exception ex)
  51. {
  52. _logger.ErrorException("Error sorting files from {0}", ex, path);
  53. }
  54. numComplete++;
  55. double percent = numComplete;
  56. percent /= paths.Count;
  57. progress.Report(100 * percent);
  58. }
  59. }
  60. private void SortFiles(string path)
  61. {
  62. new TvFileSorter(_libraryManager, _logger).Sort(path, _config.Configuration.FileSortingOptions);
  63. }
  64. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  65. {
  66. return new ITaskTrigger[]
  67. {
  68. new IntervalTrigger{ Interval = TimeSpan.FromMinutes(5)}
  69. };
  70. }
  71. public bool IsHidden
  72. {
  73. get { return !_config.Configuration.FileSortingOptions.IsEnabled; }
  74. }
  75. public bool IsEnabled
  76. {
  77. get { return _config.Configuration.FileSortingOptions.IsEnabled; }
  78. }
  79. }
  80. }