PluginUpdateTask.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using MediaBrowser.Common.Updates;
  2. using MediaBrowser.Model.Net;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Progress;
  10. using MediaBrowser.Model.Tasks;
  11. using Microsoft.Extensions.Logging;
  12. namespace Emby.Server.Implementations.ScheduledTasks
  13. {
  14. /// <summary>
  15. /// Plugin Update Task
  16. /// </summary>
  17. public class PluginUpdateTask : IScheduledTask, IConfigurableScheduledTask
  18. {
  19. /// <summary>
  20. /// The _logger
  21. /// </summary>
  22. private readonly ILogger _logger;
  23. private readonly IInstallationManager _installationManager;
  24. public PluginUpdateTask(ILogger logger, IInstallationManager installationManager)
  25. {
  26. _logger = logger;
  27. _installationManager = installationManager;
  28. }
  29. /// <summary>
  30. /// Creates the triggers that define when the task will run
  31. /// </summary>
  32. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  33. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  34. {
  35. // At startup
  36. yield return new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerStartup };
  37. // Every so often
  38. yield return new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks };
  39. }
  40. /// <summary>
  41. /// Update installed plugins
  42. /// </summary>
  43. /// <param name="cancellationToken">The cancellation token.</param>
  44. /// <param name="progress">The progress.</param>
  45. /// <returns>Task.</returns>
  46. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  47. {
  48. progress.Report(0);
  49. var packagesToInstall = (await _installationManager.GetAvailablePluginUpdates(typeof(PluginUpdateTask).Assembly.GetName().Version, true, cancellationToken).ConfigureAwait(false)).ToList();
  50. progress.Report(10);
  51. var numComplete = 0;
  52. foreach (var package in packagesToInstall)
  53. {
  54. cancellationToken.ThrowIfCancellationRequested();
  55. try
  56. {
  57. await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
  58. }
  59. catch (OperationCanceledException)
  60. {
  61. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  62. if (cancellationToken.IsCancellationRequested)
  63. {
  64. throw;
  65. }
  66. }
  67. catch (HttpException ex)
  68. {
  69. _logger.LogError(ex, "Error downloading {0}", package.name);
  70. }
  71. catch (IOException ex)
  72. {
  73. _logger.LogError(ex, "Error updating {0}", package.name);
  74. }
  75. // Update progress
  76. lock (progress)
  77. {
  78. progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
  79. }
  80. }
  81. progress.Report(100);
  82. }
  83. public string Name => "Check for plugin updates";
  84. public string Description => "Downloads and installs updates for plugins that are configured to update automatically.";
  85. public string Category => "Application";
  86. public string Key => "PluginUpdates";
  87. public bool IsHidden => false;
  88. public bool IsEnabled => true;
  89. public bool IsLogged => true;
  90. }
  91. }