PluginUpdateTask.cs 4.2 KB

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