PluginUpdateTask.cs 4.3 KB

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