PluginUpdateTask.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using MediaBrowser.Common.ScheduledTasks;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Updates;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Net;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.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. /// <summary>
  25. /// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
  26. /// </summary>
  27. /// <param name="logger">The logger.</param>
  28. /// <param name="installationManager">The installation manager.</param>
  29. public PluginUpdateTask(ILogger logger, IInstallationManager installationManager)
  30. {
  31. _logger = logger;
  32. _installationManager = installationManager;
  33. }
  34. /// <summary>
  35. /// Creates the triggers that define when the task will run
  36. /// </summary>
  37. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  38. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  39. {
  40. return new ITaskTrigger[] {
  41. // 1:30am
  42. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1.5) },
  43. new IntervalTrigger { Interval = TimeSpan.FromHours(2)}
  44. };
  45. }
  46. /// <summary>
  47. /// Update installed plugins
  48. /// </summary>
  49. /// <param name="cancellationToken">The cancellation token.</param>
  50. /// <param name="progress">The progress.</param>
  51. /// <returns>Task.</returns>
  52. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  53. {
  54. progress.Report(0);
  55. var packagesToInstall = (await _installationManager.GetAvailablePluginUpdatesStatic(true, cancellationToken).ConfigureAwait(false)).ToList();
  56. progress.Report(10);
  57. var numComplete = 0;
  58. // Create tasks for each one
  59. var tasks = packagesToInstall.Select(i => Task.Run(async () =>
  60. {
  61. cancellationToken.ThrowIfCancellationRequested();
  62. try
  63. {
  64. await _installationManager.InstallPackage(i, new Progress<double>(), cancellationToken).ConfigureAwait(false);
  65. }
  66. catch (OperationCanceledException)
  67. {
  68. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  69. if (cancellationToken.IsCancellationRequested)
  70. {
  71. throw;
  72. }
  73. }
  74. catch (HttpException ex)
  75. {
  76. _logger.ErrorException("Error downloading {0}", ex, i.name);
  77. }
  78. catch (IOException ex)
  79. {
  80. _logger.ErrorException("Error updating {0}", ex, i.name);
  81. }
  82. // Update progress
  83. lock (progress)
  84. {
  85. numComplete++;
  86. double percent = numComplete;
  87. percent /= packagesToInstall.Count;
  88. progress.Report((90 * percent) + 10);
  89. }
  90. }));
  91. cancellationToken.ThrowIfCancellationRequested();
  92. await Task.WhenAll(tasks).ConfigureAwait(false);
  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. }
  116. }