PluginUpdateTask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using MediaBrowser.Common.ScheduledTasks;
  2. using MediaBrowser.Common.Updates;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.Net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
  12. {
  13. /// <summary>
  14. /// Plugin Update Task
  15. /// </summary>
  16. public class PluginUpdateTask : IScheduledTask
  17. {
  18. /// <summary>
  19. /// The _logger
  20. /// </summary>
  21. private readonly ILogger _logger;
  22. private readonly IInstallationManager _installationManager;
  23. private readonly IApplicationHost _appHost;
  24. public PluginUpdateTask(ILogger logger, IInstallationManager installationManager, IApplicationHost appHost)
  25. {
  26. _logger = logger;
  27. _installationManager = installationManager;
  28. _appHost = appHost;
  29. }
  30. /// <summary>
  31. /// Creates the triggers that define when the task will run
  32. /// </summary>
  33. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  34. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  35. {
  36. return new ITaskTrigger[] {
  37. // At startup
  38. new StartupTrigger (),
  39. // Every so often
  40. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  41. };
  42. }
  43. /// <summary>
  44. /// Update installed plugins
  45. /// </summary>
  46. /// <param name="cancellationToken">The cancellation token.</param>
  47. /// <param name="progress">The progress.</param>
  48. /// <returns>Task.</returns>
  49. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  50. {
  51. progress.Report(0);
  52. var packagesToInstall = (await _installationManager.GetAvailablePluginUpdates(_appHost.ApplicationVersion, true, cancellationToken).ConfigureAwait(false)).ToList();
  53. progress.Report(10);
  54. var numComplete = 0;
  55. // Create tasks for each one
  56. var tasks = packagesToInstall.Select(i => Task.Run(async () =>
  57. {
  58. cancellationToken.ThrowIfCancellationRequested();
  59. try
  60. {
  61. await _installationManager.InstallPackage(i, new Progress<double>(), cancellationToken).ConfigureAwait(false);
  62. }
  63. catch (OperationCanceledException)
  64. {
  65. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  66. if (cancellationToken.IsCancellationRequested)
  67. {
  68. throw;
  69. }
  70. }
  71. catch (HttpException ex)
  72. {
  73. _logger.ErrorException("Error downloading {0}", ex, i.name);
  74. }
  75. catch (IOException ex)
  76. {
  77. _logger.ErrorException("Error updating {0}", ex, i.name);
  78. }
  79. // Update progress
  80. lock (progress)
  81. {
  82. numComplete++;
  83. double percent = numComplete;
  84. percent /= packagesToInstall.Count;
  85. progress.Report((90 * percent) + 10);
  86. }
  87. }));
  88. cancellationToken.ThrowIfCancellationRequested();
  89. await Task.WhenAll(tasks).ConfigureAwait(false);
  90. progress.Report(100);
  91. }
  92. /// <summary>
  93. /// Gets the name of the task
  94. /// </summary>
  95. /// <value>The name.</value>
  96. public string Name
  97. {
  98. get { return "Check for plugin updates"; }
  99. }
  100. /// <summary>
  101. /// Gets the description.
  102. /// </summary>
  103. /// <value>The description.</value>
  104. public string Description
  105. {
  106. get { return "Downloads and installs updates for plugins that are configured to update automatically."; }
  107. }
  108. public string Category
  109. {
  110. get { return "Application"; }
  111. }
  112. }
  113. }