MultiProviderSync.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Progress;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Sync;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Sync;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Common.IO;
  13. using MediaBrowser.Controller.IO;
  14. using MediaBrowser.Model.IO;
  15. namespace MediaBrowser.Server.Implementations.Sync
  16. {
  17. public class MultiProviderSync
  18. {
  19. private readonly SyncManager _syncManager;
  20. private readonly IServerApplicationHost _appHost;
  21. private readonly ILogger _logger;
  22. private readonly IFileSystem _fileSystem;
  23. private readonly IConfigurationManager _config;
  24. public MultiProviderSync(SyncManager syncManager, IServerApplicationHost appHost, ILogger logger, IFileSystem fileSystem, IConfigurationManager config)
  25. {
  26. _syncManager = syncManager;
  27. _appHost = appHost;
  28. _logger = logger;
  29. _fileSystem = fileSystem;
  30. _config = config;
  31. }
  32. public async Task Sync(IEnumerable<IServerSyncProvider> providers, IProgress<double> progress, CancellationToken cancellationToken)
  33. {
  34. var targets = providers
  35. .SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
  36. .ToList();
  37. var numComplete = 0;
  38. double startingPercent = 0;
  39. double percentPerItem = 1;
  40. if (targets.Count > 0)
  41. {
  42. percentPerItem /= targets.Count;
  43. }
  44. foreach (var target in targets)
  45. {
  46. cancellationToken.ThrowIfCancellationRequested();
  47. var currentPercent = startingPercent;
  48. var innerProgress = new ActionableProgress<double>();
  49. innerProgress.RegisterAction(pct =>
  50. {
  51. var totalProgress = pct * percentPerItem;
  52. totalProgress += currentPercent;
  53. progress.Report(totalProgress);
  54. });
  55. var dataProvider = _syncManager.GetDataProvider(target.Item1, target.Item2);
  56. await new MediaSync(_logger, _syncManager, _appHost, _fileSystem, _config)
  57. .Sync(target.Item1, dataProvider, target.Item2, innerProgress, cancellationToken)
  58. .ConfigureAwait(false);
  59. numComplete++;
  60. startingPercent = numComplete;
  61. startingPercent /= targets.Count;
  62. startingPercent *= 100;
  63. progress.Report(startingPercent);
  64. }
  65. }
  66. }
  67. }