PluginUpdateTask.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Updates;
  3. using MediaBrowser.Model.Net;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Common.Progress;
  11. using MediaBrowser.Model.Tasks;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.ScheduledTasks
  14. {
  15. /// <summary>
  16. /// Plugin Update Task
  17. /// </summary>
  18. public class PluginUpdateTask : IScheduledTask, IConfigurableScheduledTask
  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(typeof(PluginUpdateTask).Assembly.GetName().Version, true, cancellationToken).ConfigureAwait(false)).ToList();
  59. progress.Report(10);
  60. var numComplete = 0;
  61. foreach (var package in packagesToInstall)
  62. {
  63. cancellationToken.ThrowIfCancellationRequested();
  64. try
  65. {
  66. await _installationManager.InstallPackage(package, true, new SimpleProgress<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.LogError(ex, "Error downloading {0}", package.name);
  79. }
  80. catch (IOException ex)
  81. {
  82. _logger.LogError(ex, "Error updating {0}", package.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. progress.Report(100);
  94. }
  95. /// <summary>
  96. /// Gets the name of the task
  97. /// </summary>
  98. /// <value>The name.</value>
  99. public string Name
  100. {
  101. get { return "Check for plugin updates"; }
  102. }
  103. /// <summary>
  104. /// Gets the description.
  105. /// </summary>
  106. /// <value>The description.</value>
  107. public string Description
  108. {
  109. get { return "Downloads and installs updates for plugins that are configured to update automatically."; }
  110. }
  111. public string Category
  112. {
  113. get { return "Application"; }
  114. }
  115. public bool IsHidden => true;
  116. public bool IsEnabled => true;
  117. public bool IsLogged => true;
  118. }
  119. }