MultiProviderSync.cs 2.7 KB

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