MultiProviderSync.cs 2.6 KB

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