MultiProviderSync.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using MediaBrowser.Common.IO;
  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. namespace MediaBrowser.Server.Implementations.Sync
  13. {
  14. public class MultiProviderSync
  15. {
  16. private readonly ISyncManager _syncManager;
  17. private readonly IServerApplicationHost _appHost;
  18. private readonly ILogger _logger;
  19. private readonly IFileSystem _fileSystem;
  20. public MultiProviderSync(ISyncManager syncManager, IServerApplicationHost appHost, ILogger logger, IFileSystem fileSystem)
  21. {
  22. _syncManager = syncManager;
  23. _appHost = appHost;
  24. _logger = logger;
  25. _fileSystem = fileSystem;
  26. }
  27. public async Task Sync(IEnumerable<IServerSyncProvider> providers, IProgress<double> progress, CancellationToken cancellationToken)
  28. {
  29. var targets = providers
  30. .SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
  31. .ToList();
  32. var numComplete = 0;
  33. double startingPercent = 0;
  34. double percentPerItem = 1;
  35. if (targets.Count > 0)
  36. {
  37. percentPerItem /= targets.Count;
  38. }
  39. foreach (var target in targets)
  40. {
  41. cancellationToken.ThrowIfCancellationRequested();
  42. var currentPercent = startingPercent;
  43. var innerProgress = new ActionableProgress<double>();
  44. innerProgress.RegisterAction(pct =>
  45. {
  46. var totalProgress = pct * percentPerItem;
  47. totalProgress += currentPercent;
  48. progress.Report(totalProgress);
  49. });
  50. await new MediaSync(_logger, _syncManager, _appHost, _fileSystem)
  51. .Sync(target.Item1, target.Item1.GetDataProvider(), target.Item2, innerProgress, cancellationToken)
  52. .ConfigureAwait(false);
  53. numComplete++;
  54. startingPercent = numComplete;
  55. startingPercent /= targets.Count;
  56. startingPercent *= 100;
  57. progress.Report(startingPercent);
  58. }
  59. }
  60. }
  61. }