PluginUpdateTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /// <summary>
  46. /// Update installed plugins
  47. /// </summary>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. /// <param name="progress">The progress.</param>
  50. /// <returns>Task.</returns>
  51. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  52. {
  53. progress.Report(0);
  54. var packagesToInstall = (await _installationManager.GetAvailablePluginUpdates(typeof(PluginUpdateTask).Assembly.GetName().Version, true, cancellationToken).ConfigureAwait(false)).ToList();
  55. progress.Report(10);
  56. var numComplete = 0;
  57. foreach (var package in packagesToInstall)
  58. {
  59. cancellationToken.ThrowIfCancellationRequested();
  60. try
  61. {
  62. await _installationManager.InstallPackage(package, true, new SimpleProgress<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.LogError(ex, "Error downloading {0}", package.name);
  75. }
  76. catch (IOException ex)
  77. {
  78. _logger.LogError(ex, "Error updating {0}", package.name);
  79. }
  80. // Update progress
  81. lock (progress)
  82. {
  83. numComplete++;
  84. progress.Report(90.0 * numComplete / packagesToInstall.Count + 10);
  85. }
  86. }
  87. progress.Report(100);
  88. }
  89. public string Name => "Check for plugin updates";
  90. public string Description => "Downloads and installs updates for plugins that are configured to update automatically.";
  91. public string Category => "Application";
  92. public string Key => "PluginUpdates";
  93. public bool IsHidden => false;
  94. public bool IsEnabled => true;
  95. public bool IsLogged => true;
  96. }
  97. }