PluginUpdateTask.cs 4.4 KB

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