PluginUpdateTask.cs 3.8 KB

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